matklad / xshell

Apache License 2.0
675 stars 28 forks source link

Add `_timeout` methods for cmd to execute #92

Open Binlogo opened 1 month ago

Binlogo commented 1 month ago

Follow suggestion in https://github.com/matklad/xshell/pull/91#issuecomment-2228324022

At the same time, adding a timeout/deadline method, while preserving blocking API, would be great. I think it's OK to burn a thread just for that:

  • When we spawn a timeout-enabled process, we also spawn a timeout thread which also gets a reference to the Child's handle
  • That thread blocks in https://doc.rust-lang.org/stable/std/sync/mpsc/struct.Receiver.html#method.recv_timeout
  • if timeout elapses, it kills the process, which unblocks the main thread.
  • otherwise, it unblocks when the sender side of the channel is blocked (it's important that we don't leave hanging threads around)

Add timeout relative methods: run_timeout / read_timeout / read_stderr_timeout / output_timeout.