Expand description
A lightweight crate for detecting process forks.
let mut guard = forkguard::new();
// Some time later...
if guard.detected_fork() {
// Handle the fork (e.g., re-initialize state)
}This crate provides Guard types that can detect if the current process has
been forked since the last check. This is useful for resetting state (like
random number generators or connection pools) that should not be shared between
a parent and its forked child.
Depending on the platform and enabled features, new() returns one of three
specialized Guard flavors:
atfork::Guard(Unix withatforkcrate feature): Usespthread_atfork()to update the global state on fork. This is the most efficient detection mechanism for supported Unix-like systems, though the usual caveats of fork handlers apply.pid::Guard(Unix default): Tracks the process ID and detects changes. This is a simple and sufficiently efficient option for Unix-like platforms unlessgetpid()overhead is a concern.noop::Guard(Non-Unix): Provides a no-op implementation that always returnsfalse, used on platforms where fork detection is not required.
The top-level forkguard::Guard resolves to the active flavor.
§Crate features
atfork(optional): See the above description.
This crate lets downstream users opt-in to the atfork feature even when the
intermediate dependencies do not. To do this, add the following to your
Cargo.toml no matter if your crate uses forkguard directly:
[dependencies]
forkguard = { version = "0.1", features = ["atfork"] }Alternatively, add the following to Cargo.toml:
[dependencies]
forkguard = "0.1"And then enable the feature in your build command:
cargo build --features forkguard/atforkRe-exports§
pub use atfork::Guard;Unix and atfork
Modules§
- atfork
Unix and atfork - A fork detector implementation using
pthread_atfork(). - noop
- A no-op fork detector implementation.
- pid
- A fork detector implementation based on the process ID.
Enums§
- Flavor
- An enumeration of the different fork detection flavors provided by this crate.
Functions§
- new
- Creates a new fork
Guardinstance.