Struct TerminateOnDrop

Source
pub struct TerminateOnDrop<O: OutputStream> { /* private fields */ }
Expand description

A wrapper that automatically terminates a process when dropped.

§Safety Requirements

WARNING: This type requires a multithreaded tokio runtime to function correctly!

§Usage Guidelines

This type should only be used when:

  • Your code is running in a multithreaded tokio runtime.
  • Automatic process cleanup on drop is absolutely necessary.

Instead of relying on automatic termination, prefer these safer approaches:

  1. Manual process termination using ProcessHandle::terminate
  2. Awaiting process completion using ProcessHandle::wait_for_completion
  3. Awaiting process completion or performing an explicit termination using ProcessHandle::wait_for_completion_or_terminate

§Implementation Details

The drop implementation tries to terminate the process if it was neither awaited nor terminated before being dropped. If termination fails, a panic is raised.

Methods from Deref<Target = ProcessHandle<O>>§

Source

pub fn id(&self) -> Option<u32>

Source

pub fn is_running(&mut self) -> RunningState

Source

pub fn stdout(&self) -> &O

Source

pub fn stdout_mut(&mut self) -> &mut O

Source

pub fn stderr(&self) -> &O

Source

pub fn stderr_mut(&mut self) -> &mut O

Source

pub fn must_be_terminated(&mut self)

Sets a panic-on-drop mechanism for this ProcessHandle.

This method enables a safeguard that ensures that the process represented by this ProcessHandle is properly terminated or awaited before being dropped. If must_be_terminated is set and the ProcessHandle is dropped without invoking terminate() or wait(), an intentional panic will occur to prevent silent failure-states, ensuring that system resources are handled correctly.

You typically do not need to call this, as every ProcessHandle is marked by default. Call must_not_be_terminated to clear this safeguard to explicitly allow dropping the process without terminating it.

§Panic

If the ProcessHandle is dropped without being awaited or terminated after calling this method, a panic will occur with a descriptive message to inform about the incorrect usage.

Source

pub fn must_not_be_terminated(&mut self)

Source

pub fn send_interrupt_signal(&mut self) -> Result<(), Error>

Manually sed a SIGINT on unix or equivalent on Windows to this process.

Prefer to call terminate instead, if you want to make sure this process is terminated.

Source

pub fn send_terminate_signal(&mut self) -> Result<(), Error>

Manually sed a SIGTERM on unix or equivalent on Windows to this process.

Prefer to call terminate instead, if you want to make sure this process is terminated.

Source

pub async fn terminate( &mut self, interrupt_timeout: Duration, terminate_timeout: Duration, ) -> Result<ExitStatus, TerminationError>

Terminates this process by sending a SIGINT, SIGTERM or even a SIGKILL if the process doesn’t run to completion after receiving any of the first two signals.

Source

pub async fn kill(&mut self) -> Result<()>

Source

pub async fn wait_for_completion( &mut self, timeout: Option<Duration>, ) -> Result<ExitStatus>

Wait for this process to run to completion. Within timeout, if set, or unbound otherwise.

Source

pub async fn wait_for_completion_or_terminate( &mut self, wait_timeout: Duration, interrupt_timeout: Duration, terminate_timeout: Duration, ) -> Result<ExitStatus, TerminationError>

Trait Implementations§

Source§

impl<O: Debug + OutputStream> Debug for TerminateOnDrop<O>

Source§

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

Formats the value using the given formatter. Read more
Source§

impl<O: OutputStream> Deref for TerminateOnDrop<O>

Source§

type Target = ProcessHandle<O>

The resulting type after dereferencing.
Source§

fn deref(&self) -> &Self::Target

Dereferences the value.
Source§

impl<O: OutputStream> DerefMut for TerminateOnDrop<O>

Source§

fn deref_mut(&mut self) -> &mut Self::Target

Mutably dereferences the value.
Source§

impl<O: OutputStream> Drop for TerminateOnDrop<O>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

§

impl<O> Freeze for TerminateOnDrop<O>
where O: Freeze,

§

impl<O> !RefUnwindSafe for TerminateOnDrop<O>

§

impl<O> Send for TerminateOnDrop<O>
where O: Send,

§

impl<O> Sync for TerminateOnDrop<O>
where O: Sync,

§

impl<O> Unpin for TerminateOnDrop<O>
where O: Unpin,

§

impl<O> !UnwindSafe for TerminateOnDrop<O>

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<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
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> Sink for T
where T: Debug + Send + Sync + 'static,