Commit Graph

43 Commits

Author SHA1 Message Date
cowlicks 76badb6964
Fix typo in ChildStdin docs (#77) 2024-04-19 10:34:15 +09:00
kennytm 06fb10ac23
bugfix: Use AtomicUsize instead of U64 to count zombies for 32-bit compatibility
Fix #74.
2024-04-11 17:03:29 -07:00
John Nunley bbd42b56f8 feat: Allow fallback to signal backend
As pidfd isn't available in older versions of Linux that Rust still
supports, this is necessary for running on older Linux. In addition,
signals tests are still kept in CI.

Signed-off-by: John Nunley <dev@notgull.net>
2024-03-29 20:37:31 -07:00
John Nunley 1e0751fe65 feat: Add a waitable process backend for Linux
This commit adds a new backend for the process reaper. Rather than
waiting on a signal, it instead registers the process's pidfd into
async-io and waits on that instead.

I've coded this backend to also allow for other systems to be registered
here as well.

Signed-off-by: John Nunley <dev@notgull.net>
2024-03-29 20:37:31 -07:00
John Nunley 02699a4f04 m: Move reaper-related code to another module
Signed-off-by: John Nunley <dev@notgull.net>
2024-03-29 20:37:31 -07:00
John Nunley 35a77ff266
m: Port to event-listener v5.0.0
cc smol-rs/event-listener#104

Signed-off-by: John Nunley <dev@notgull.net>
2024-02-12 06:26:10 -08:00
John Nunley d0993f8d0b
m: Bump to event-listener v4.0.0
Signed-off-by: John Nunley <dev@notgull.net>
2023-11-19 12:51:15 -08:00
John Nunley 65cde366d4
Bump async-io to v2.0.0 and async-channel to v2.0.0
Signed-off-by: John Nunley <dev@notgull.net>
2023-10-28 19:04:54 -07:00
John Nunley f733a83c22
feat: Add a way to run without the async-process thread
I know I said that I wouldn't add any more features, but I
think this is important enough.

Right now, a thread called "async-process" is responsible for listening
for SIGCHLD and reaping zombie processes. This listens for the SIGCHLD
signal in Unix and uses a channel connected to the waitable handle on
Windows. While this works, we can do better. Through async-signal, the
signal was already asynchronous on Unix; we were already just using
async_io::block_on to wait on the signal. After swapping out the channel
used on Windows with async-channel, the process reaping function "reap"
can be reimplemented as a fully asynchronous future.

From here we must make sure this future is being polled at all times. To
facilitate this, a function named "driver()" is added to the public API.
This future acquires a lock on the reaper structure and calls the
"reap()" future indefinitely. Multiple drivers can be created at once;
they will just wait forever on this lock. This future is intended to be
spawned onto an executor and left to run forever, making sure all child
processes are signalled whenever necessary. If no tasks are running the
driver future, the "async-process" thread is spawned and runs the
"reap()" future itself.

I've added the following controls to make sure that this system is
robust:

- If a "driver" task is dropped, another "driver" task will acquire the
  lock and keep the reaper active.
- Before being dropped, the task checks to see if it is the last driver.
  If it is, it will spawn the "async-process" thread to be the driver.
- When a Child is being created, it checks if there are any active
  drivers. If there are none, it spawns the "async-process" thread
  itself.
- One concern is that the driver future wil try to spawn the
  "async-process" thread as the application exits and the task is being
  dropped, which will be unnecessary and lead to slower shutdowns. To
  prevent this, the future checks to see if there are any extant `Child`
  instances (a new refcount is added to Reaper to facilitate this). If
  there are none, and if there are no zombie processes, it does not
  spawn the additional thread.
- Someone can still `mem::forget()` the driver thread. This does not
  lead to undefined behavior and just leads to processes being left
  dangling. At this point they're asking for wacky behavior.

This strategy might also be viable for `async-io`, if we want to try to
avoid needing to spawn the additional thread there as well.

Closes #7
cc smol-rs/async-io#40

Signed-off-by: John Nunley <dev@notgull.net>
2023-10-10 17:47:46 -07:00
Taiki Endo 9f9351bc52
Migrate to Rust 2021 (#58) 2023-10-08 14:56:48 +09:00
John Nunley b29af2b72c
breaking: Remove the pre-exec extension function
The purpose for removing this function is twofold:

- It is the only unsafe code in this crate that can't be realistically
  replaced with safe code.
- It is a footgun anyways, and can be done anyways with Into::into() if
   users really want it.

This is a breaking change.

Signed-off-by: John Nunley <dev@notgull.net>
2023-10-07 21:06:27 -07:00
John Nunley 52a693e4dd
m: Centralize all global state into a single structure
Signed-off-by: John Nunley <dev@notgull.net>
2023-09-10 16:00:56 -07:00
John Nunley 5e8e0b7c7b
Upgrade to event-listener v3.0.0 (#43)
Signed-off-by: John Nunley <dev@notgull.net>
2023-09-10 12:57:24 -07:00
John Nunley 07165c72f5
Add smol-rs logo (#48) 2023-07-17 14:41:47 +09:00
John Nunley d45d6f1094
Use async-signal instead of signal-hook (#42) 2023-06-11 11:21:40 -07:00
Taiki Endo d9d97d0299 Fix clippy::needless_borrow warning
```
error: the borrowed expression implements the required traits
   --> src/lib.rs:212:57
    |
212 |                     signal_hook::iterator::Signals::new(&[signal_hook::consts::SIGCHLD])
    |                                                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: change this to: `[signal_hook::consts::SIGCHLD]`
    |
    = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_borrow
    = note: `-D clippy::needless-borrow` implied by `-D warnings`

error: the borrowed expression implements the required traits
  --> tests/std.rs:14:38
   |
14 |             Command::new("cmd").args(&["/C", "exit 0"]).spawn()
   |                                      ^^^^^^^^^^^^^^^^^ help: change this to: `["/C", "exit 0"]`
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_borrow
   = note: `-D clippy::needless-borrow` implied by `-D warnings`

error: the borrowed expression implements the required traits
  --> tests/std.rs:35:38
   |
35 |             Command::new("cmd").args(&["/C", "exit 1"]).spawn()
   |                                      ^^^^^^^^^^^^^^^^^ help: change this to: `["/C", "exit 1"]`
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_borrow

error: the borrowed expression implements the required traits
  --> tests/std.rs:87:22
   |
87 |             cmd.args(&["/C", "echo foobar"]).stdout(Stdio::piped());
   |                      ^^^^^^^^^^^^^^^^^^^^^^ help: change this to: `["/C", "echo foobar"]`
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_borrow

error: the borrowed expression implements the required traits
   --> tests/std.rs:145:23
    |
145 |                 .args(&["/C", "exit 1"])
    |                       ^^^^^^^^^^^^^^^^^ help: change this to: `["/C", "exit 1"]`
    |
    = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_borrow

error: the borrowed expression implements the required traits
   --> tests/std.rs:156:23
    |
156 |                 .args(&["/C", "exit 0"])
    |                       ^^^^^^^^^^^^^^^^^ help: change this to: `["/C", "exit 0"]`
    |
    = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_borrow

error: the borrowed expression implements the required traits
   --> tests/std.rs:189:23
    |
189 |                 .args(&["/C", "echo hello"])
    |                       ^^^^^^^^^^^^^^^^^^^^^ help: change this to: `["/C", "echo hello"]`
    |
    = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_borrow

error: the borrowed expression implements the required traits
   --> tests/std.rs:213:23
    |
213 |                 .args(&["/C", "mkdir ."])
    |                       ^^^^^^^^^^^^^^^^^^ help: change this to: `["/C", "mkdir ."]`
    |
    = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_borrow

error: the borrowed expression implements the required traits
   --> tests/std.rs:231:38
    |
231 |             Command::new("cmd").args(&["/C", "exit 1"]).spawn().unwrap()
    |                                      ^^^^^^^^^^^^^^^^^ help: change this to: `["/C", "exit 1"]`
    |
    = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_borrow

error: the borrowed expression implements the required traits
   --> tests/std.rs:243:38
    |
243 |             Command::new("cmd").args(&["/C", "exit 1"]).spawn().unwrap()
    |                                      ^^^^^^^^^^^^^^^^^ help: change this to: `["/C", "exit 1"]`
    |
    = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_borrow

error: the borrowed expression implements the required traits
   --> tests/std.rs:257:23
    |
257 |                 .args(&["/C", "echo hello"])
    |                       ^^^^^^^^^^^^^^^^^^^^^ help: change this to: `["/C", "echo hello"]`
    |
    = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_borrow
```
2023-06-11 22:26:44 +09:00
Taiki Endo 7eb60b1025 Bump MSRV to 1.63 2023-06-11 22:26:44 +09:00
Taiki Endo 01e36f4abe Update windows-sys to 0.48 2023-04-04 03:46:36 +09:00
Taiki Endo e086897e53 Add windows::CommandExt::raw_arg on Rust 1.62+ 2022-12-30 14:31:53 +09:00
Taiki Endo f76d325959 Seal CommandExt trait
This is technically a breaking change, but we follow the standard
library's decision that sealing CommandExt is fine.
bfd1ccfb27
2022-12-30 14:31:53 +09:00
Taiki Endo 7980b4696a Reduce syscalls in blocking_fd 2022-12-30 13:43:50 +09:00
Taiki Endo 93be5c2506 Replace direct dependency on libc with rustix 2022-12-30 13:43:50 +09:00
John Nunley de1071784e
Port to windows-sys (#27) 2022-11-26 21:14:01 -08:00
John Nunley a44d0b418a
Port from once-cell to async-lock (#26) 2022-11-27 12:43:59 +09:00
John Nunley 8dda23e06b
Add `AsRawFd`/`AsFd` to the `ChildStd*` types (#23)
* add asrawfd/asfd impls

* fmt
2022-08-17 17:42:17 -07:00
Alexander Berger 870cd2dc1f Impl From<std::process::Command> for Command
Allow creating Command from std::process::Command, similar to
what tokio::process::Command supports.
2022-01-27 20:46:09 +01:00
Jesse Luehrs afff8c533d don't unconfigure stdio streams on spawn 2021-12-30 22:13:17 -05:00
Paul Colomiets 378b8ac11a Improve debug implementation of `Command`
In normal mode `Debug` now prints just the command-line (similarly to
`std::process::Command`). This is useful for logging commands and
formatting error messages that includes command-line.

In "alternate" mode full internals of this structure are still printed.
2021-10-11 16:54:12 +03:00
Félix Saparelli d91b557db3 Implement AsRawHandle on Child on Windows 2021-07-31 18:45:29 +12:00
Taiki Endo 4831e3cbb9 Fix format of syscall macro 2021-04-24 17:45:09 +09:00
BinCheng 7d9dc0b914
Adding the `into_stdio` make async-process more flexible. (#13) 2021-04-24 17:39:11 +09:00
Jayce Fayne afbc34d070 fix clippy warnings 2021-04-17 14:18:59 +02:00
Alexandru Macovei 6ea203d787 Fix Child::output to avoid killing the subprocess early when kill_on_drop is given 2021-01-31 22:43:19 +02:00
Taiki Endo 6a0916d1d1 Fix Command::output example 2021-01-16 19:29:23 +09:00
Wesley Merkel ae865efb27 Update signal-hook to version 0.3.0
The dependency on version v0.1.16 of signal-hook makes this library
impossible to use in a project alongside signal-hook version v0.3.0.
This is caused by an arguably incorrect dependency from signal-hook
v0.1.16 on `~1.2` of signal-hook-registry (I think it should just depend
on `^1.2` instead), however it seemed more beneficial overall to upgrade
this crate's dependency instead.
2020-12-25 05:05:12 -06:00
Stjepan Glavina 974c3cc080 Update futures-lite 2020-10-09 14:37:51 +02:00
Stjepan Glavina 3965add776 Remove let _ pattern 2020-09-13 13:52:26 +02:00
Stjepan Glavina 3acf5fcafa Bump to v0.1.3 2020-08-26 23:27:37 +02:00
Stjepan Glavina 056879ddf5 Add platform-specific extensions 2020-08-22 16:32:49 +00:00
Stjepan Glavina e1ab1d31c1 Add reap_on_drop and kill_on_drop 2020-08-22 16:12:15 +00:00
Stjepan Glavina 377e658288 Don't reap the child while there's a Child instance 2020-08-20 19:47:23 +00:00
Stjepan Glavina 71d3dfcbab Initial version 2020-08-19 13:34:03 +00:00
Stjepan Glavina 251ab32f59 Initial commit 2020-08-18 20:19:25 +00:00