Skip to main content

SharedRuntime

Struct SharedRuntime 

Source
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§

Source§

impl SharedRuntime

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source§

impl SharedRuntime

Source

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).

Source

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.

Trait Implementations§

Source§

impl Debug for SharedRuntime

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<T> MaybeSend for T
where T: Send,