pub struct SharedRuntime { /* private fields */ }Expand description
A shared runtime that manages PausableWorkers and provides fork safety hooks.
The SharedRuntime owns a tokio runtime (on native) and tracks PausableWorkers spawned on it. It provides methods to safely pause workers before forking and restart them after fork in both parent and child processes.
On wasm32, no tokio runtime is created. Workers are spawned via spawn_local
on the JS event loop.
§Mutex lock order
When locking both Self::runtime and Self::workers, the mutex must be locked in the order of the fields in the struct. When possible avoid holding both locks simultaneously.
Implementations§
Sourcepub fn runtime_handle(&self) -> Result<Handle, SharedRuntimeError>
pub fn runtime_handle(&self) -> Result<Handle, SharedRuntimeError>
Returns a clone of the tokio runtime handle managed by this SharedRuntime.
§Errors
Returns SharedRuntimeError::RuntimeUnavailable if the runtime has been shut down.
Sourcepub fn spawn_worker<T: Worker + Sync + 'static>(
&self,
worker: T,
restart_on_fork: bool,
) -> Result<WorkerHandle, SharedRuntimeError>
pub fn spawn_worker<T: Worker + Sync + 'static>( &self, worker: T, restart_on_fork: bool, ) -> Result<WorkerHandle, SharedRuntimeError>
Spawn a PausableWorker on this runtime.
The worker will be tracked by this SharedRuntime and will be paused/resumed
during fork operations (native only).
If restart_on_fork is true, the worker will be reset and restarted when calling
after_fork_child else the worker is dropped without calling Worker::shutdown.
§Errors
Returns an error if the worker cannot be started.
Sourcepub fn before_fork(&self)
pub fn before_fork(&self)
Hook to be called before forking.
This method pauses all workers and prepares the runtime for forking. It ensures that no background tasks are running when the fork occurs, preventing potential deadlocks in the child process.
Worker errors are logged but do not cause the function to fail. If the worker fails to pause it is dropped without calling shutdown.
Sourcepub fn after_fork_parent(&self) -> Result<(), SharedRuntimeError>
pub fn after_fork_parent(&self) -> Result<(), SharedRuntimeError>
Hook to be called in the parent process after forking.
This method restarts workers and resumes normal operation in the parent process. The runtime may need to be recreated if it was shut down in before_fork.
§Errors
Returns an error if workers cannot be restarted or the runtime cannot be recreated.
Sourcepub fn after_fork_child(&self) -> Result<(), SharedRuntimeError>
pub fn after_fork_child(&self) -> Result<(), SharedRuntimeError>
Hook to be called in the child process after forking.
This method reinitializes the runtime and workers in the child process. A new runtime must be created since tokio runtimes cannot be safely forked. Workers are reset and restarted to resume operations in the child.
§Errors
Returns an error if the runtime cannot be reinitialized or workers cannot be started.
Sourcepub fn block_on<F: Future>(&self, f: F) -> Result<F::Output, Error>
pub fn block_on<F: Future>(&self, f: F) -> Result<F::Output, Error>
Run a future to completion on the shared runtime, blocking the current thread.
If the runtime is not available (e.g. after calling before_fork), a temporary single-threaded runtime is used.
Not available on wasm32 – use async paths instead.
§Errors
Returns an error if it fails to create a fallback runtime.
Sourcepub fn shutdown(
&self,
timeout: Option<Duration>,
) -> Result<(), SharedRuntimeError>
pub fn shutdown( &self, timeout: Option<Duration>, ) -> Result<(), SharedRuntimeError>
Shutdown the runtime and all workers synchronously with optional timeout.
Not available on wasm32 – use shutdown_async instead.
Worker errors are logged but do not cause the function to fail.
§Errors
Returns an error only if shutdown times out.
Sourcepub fn new() -> Result<Self, SharedRuntimeError>
pub fn new() -> Result<Self, SharedRuntimeError>
Create a new SharedRuntime.
On native, this creates a tokio multi-thread runtime. On wasm32, no runtime
is created (workers are spawned on the JS event loop via spawn_local).
§Errors
Returns an error if the tokio runtime cannot be created (native only).
Sourcepub async fn shutdown_async(&self)
pub async fn shutdown_async(&self)
Shutdown all workers asynchronously.
This should be called during application shutdown to cleanly stop all background workers and the runtime.
Worker errors are logged but do not cause the function to fail.