mz_ore/
process.rs

1// Copyright Materialize, Inc. and contributors. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License in the LICENSE file at the
6// root of this repository, or online at
7//
8//     http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15
16//! Process utilities.
17
18/// Halts the process.
19///
20/// `halt` forwards the provided arguments to the [`tracing::warn`] macro, then
21/// terminates the process with exit code 166.
22///
23/// Halting a process is a middle ground between a graceful shutdown and a
24/// panic. Use halts for errors that are severe enough to require shutting down
25/// the process, but not severe enough to be considered a crash. Halts are not
26/// intended to be reported to error tracking tools (e.g., Sentry).
27///
28/// There are two common classes of errors that trigger a halt:
29///
30///   * An error that indicates a new process has taken over (e.g., an error
31///     indicating that the process's leader epoch has expired).
32///
33///   * A retriable error where granular retry logic would be tricky enough to
34///     write that it has not yet been worth it. Since restarting from a fresh
35///     process must *always* be correct, forcing a restart is an easy way to
36///     tap into the existing whole-process retry logic. Use halt judiciously in
37///     these cases, as restarting a process can be inefficient (e.g., mandatory
38///     restart backoff, rehydrating expensive state).
39#[cfg_attr(nightly_doc_features, doc(cfg(feature = "tracing")))]
40#[cfg(feature = "tracing")]
41#[macro_export]
42macro_rules! halt {
43    ($($arg:expr),* $(,)?) => {{
44        $crate::__private::tracing::warn!("halting process: {}", format!($($arg),*));
45        $crate::process::exit_thread_safe(166);
46    }}
47}
48
49/// Prints the given message and exits the process.
50///
51/// `exit!` forwards the provided arguments to the [`tracing::info`] macro, then
52/// terminates the process with given exit code.
53#[cfg_attr(nightly_doc_features, doc(cfg(feature = "tracing")))]
54#[cfg(feature = "tracing")]
55#[macro_export]
56macro_rules! exit {
57    ($exit_code:literal, $($arg:expr),* $(,)?) => {{
58        $crate::__private::tracing::info!("exiting process (0): {}", format!($($arg),*));
59        $crate::process::exit_thread_safe($exit_code);
60    }};
61}
62
63/// Helper for exiting macros.
64///
65/// This function exists to avoid that all callers of `halt!` have to explicitly depend on `libc`.
66pub fn exit_thread_safe(error_code: i32) -> ! {
67    // Using `std::process::exit` here would be unsound as that function invokes libc's `exit`
68    // function, which is not thread-safe [1]. There are two viable work arounds for this:
69    //
70    //  * Calling libc's `_exit` function instead, which is thread-safe by virtue of not performing
71    //    any cleanup work.
72    //  * Introducing a global lock to ensure only a single thread gets to call `exit` at a time.
73    //
74    // We chose the former approach because it's simpler and has the additional benefit of
75    // protecting us from third-party code that installs thread-unsafe exit handlers.
76    //
77    // Note that we are fine with not running any cleanup code here. This behaves just like an
78    // abort we'd do in response to a panic. Any code that relies on cleanup code to run before
79    // process termination is already unsound in the face of panics and hardware failure.
80    //
81    // [1]: https://github.com/rust-lang/rust/issues/126600
82    unsafe { libc::_exit(error_code) };
83}