Repository navigation
fix(cli): stop global vp dev on SIGTERM #3521
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
fengmk2
wants to merge
1
commit into
codex/issue-3106-local-dev-signal
from
issue-3106-global-dev-signal
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
...pshots/fixtures/command_dev_local_signal/snapshots/command_dev_global_signal.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| # command_dev_global_signal | ||
|
|
||
| SIGTERM sent to the global vp dev PID passes through each wrapper and stops the listening Vite server. | ||
|
|
||
| ## `node probe.mjs --global` | ||
|
|
||
| ``` | ||
| Vite served the project with the client under /probe/ | ||
| SIGTERM to the global vp PID closed the port with exit code 143 | ||
| ``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| //! Forward termination signals from a wrapper process to the child it waits for. | ||
|
|
||
| use std::{ | ||
| mem::MaybeUninit, | ||
| sync::atomic::{AtomicI32, Ordering}, | ||
| }; | ||
|
|
||
| use nix::{ | ||
| libc, | ||
| sys::signal::{SaFlags, SigAction, SigHandler, SigSet, Signal, sigaction}, | ||
| }; | ||
|
|
||
| /// The child that receives forwarded signals, or 0 when no guard is alive. | ||
| static FORWARD_PID: AtomicI32 = AtomicI32::new(0); | ||
|
|
||
| /// While alive, sends SIGTERM and SIGHUP that reach this process on to the child. | ||
| /// | ||
| /// A process manager or `kill <pid>` signals only this process. Without this | ||
| /// guard, this process exits and the child keeps running. SIGINT is not | ||
| /// forwarded, because the terminal sends Ctrl+C to the whole process group. | ||
| pub(crate) struct SignalForwardGuard { | ||
| previous: Vec<(Signal, SigAction)>, | ||
| } | ||
|
|
||
| impl SignalForwardGuard { | ||
| /// Returns `None` when another guard already forwards to a child. | ||
| pub(crate) fn install(pid: u32) -> Option<Self> { | ||
| let pid = i32::try_from(pid).ok()?; | ||
| FORWARD_PID.compare_exchange(0, pid, Ordering::SeqCst, Ordering::SeqCst).ok()?; | ||
| let action = | ||
| SigAction::new(SigHandler::Handler(forward), SaFlags::SA_RESTART, SigSet::empty()); | ||
| let previous = [Signal::SIGTERM, Signal::SIGHUP] | ||
| .into_iter() | ||
| // Keep a signal that the caller ignores, such as SIGHUP under `nohup`. | ||
| .filter(|&signal| !is_ignored(signal)) | ||
| .filter_map(|signal| { | ||
| // SAFETY: `forward` only calls async-signal-safe functions. | ||
| let previous = unsafe { sigaction(signal, &action) }.ok()?; | ||
|
fengmk2 marked this conversation as resolved.
Resolved
|
||
| Some((signal, previous)) | ||
| }) | ||
| .collect(); | ||
| Some(Self { previous }) | ||
| } | ||
| } | ||
|
|
||
| impl Drop for SignalForwardGuard { | ||
| fn drop(&mut self) { | ||
| // The child has exited. Stop forwarding before its PID can be reused. | ||
| FORWARD_PID.store(0, Ordering::SeqCst); | ||
| for (signal, previous) in self.previous.drain(..) { | ||
| // SAFETY: reinstalls the disposition captured by `install`. | ||
| let _ = unsafe { sigaction(signal, &previous) }; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /// Reads the disposition without replacing it. | ||
| fn is_ignored(signal: Signal) -> bool { | ||
| let mut current = MaybeUninit::<libc::sigaction>::uninit(); | ||
| // SAFETY: a null action only writes the current disposition to `current`. | ||
| unsafe { | ||
| libc::sigaction(signal as libc::c_int, std::ptr::null(), current.as_mut_ptr()) == 0 | ||
| && current.assume_init().sa_sigaction == libc::SIG_IGN | ||
| } | ||
| } | ||
|
|
||
| extern "C" fn forward(signal: libc::c_int) { | ||
| let pid = FORWARD_PID.load(Ordering::SeqCst); | ||
| if pid > 0 { | ||
| // SAFETY: `kill` is async-signal-safe. | ||
| unsafe { libc::kill(pid, signal) }; | ||
|
fengmk2 marked this conversation as resolved.
Resolved
|
||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.