-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: use signal handler in shim (#4313)
### Description I believe this fixes #3711 #4196 added graceful shutting down when calling `go-turbo`, this does the same but for when we need to invoke the correct Rust binary in `shim.rs`. I extracted the logic added in #4196 to a function that can be used throughout the codebase. ### Testing Instructions Currently only doing manual via the reproduction provided in #3711. Edit: I originally wanted to add integration test to cover these cases, this has proven to be a challenge. Will still look at trying to orchestrate a test for this, but considering how much traction the related issues have, I don't want to block on getting an integration test.
- Loading branch information
1 parent
29c71c6
commit b34f2e2
Showing
13 changed files
with
107 additions
and
35 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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,31 @@ | ||
Setup | ||
$ . ${TESTDIR}/../setup.sh | ||
$ . ${TESTDIR}/setup.sh $(pwd) | ||
|
||
Run script with INT handler and verify that INT gets passed to script | ||
|
||
Start turbo in the background | ||
$ ${TURBO} trap & | ||
Save the PID of turbo | ||
$ TURBO_PID=$! | ||
We send INT to turbo, but with a delay to give us time to bring turbo back to | ||
the foreground. | ||
$ sh -c "sleep 1 && kill -2 ${TURBO_PID}" & | ||
Bring turbo back to the foreground | ||
$ fg 1 | ||
${TURBO} trap | ||
\xe2\x80\xa2 Packages in scope: test (esc) | ||
\xe2\x80\xa2 Running trap in 1 packages (esc) | ||
\xe2\x80\xa2 Remote caching disabled (esc) | ||
test:trap: cache miss, executing d25759ee0a8e12ae | ||
test:trap: | ||
test:trap: > trap | ||
test:trap: > trap 'echo trap hit; sleep 1; echo trap finish' INT; sleep 5 && echo 'script finish' | ||
test:trap: | ||
test:trap: trap hit | ||
test:trap: trap finish | ||
test:trap: npm ERR! Lifecycle script `trap` failed with error: | ||
test:trap: npm ERR! Error: command failed | ||
test:trap: npm ERR! in workspace: test | ||
test:trap: npm ERR! at location: .*ctrlc.t/apps/test (re) | ||
[1] |
This file contains 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,8 @@ | ||
#!/bin/bash | ||
# Enable jobcontrol as we need it for fg to work | ||
set -m | ||
|
||
SCRIPT_DIR=$(dirname ${BASH_SOURCE[0]}) | ||
TARGET_DIR=$1 | ||
cp -a ${SCRIPT_DIR}/test_repo/. ${TARGET_DIR}/ | ||
${SCRIPT_DIR}/../setup_git.sh ${TARGET_DIR} |
6 changes: 6 additions & 0 deletions
6
cli/integration_tests/signal_handling/test_repo/apps/test/package.json
This file contains 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,6 @@ | ||
{ | ||
"name": "test", | ||
"scripts": { | ||
"trap": "trap 'echo trap hit; sleep 1; echo trap finish' INT; sleep 5 && echo 'script finish'" | ||
} | ||
} |
This file contains 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,7 @@ | ||
{ | ||
"name": "signal-test", | ||
"workspaces": [ | ||
"apps/*" | ||
], | ||
"packageManager": "[email protected]" | ||
} |
This file contains 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,6 @@ | ||
{ | ||
"$schema": "https://turbo.build/schema.json", | ||
"pipeline": { | ||
"trap": {} | ||
} | ||
} |
Binary file not shown.
This file contains 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 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,29 @@ | ||
use std::{process::Command, sync::Arc}; | ||
|
||
use anyhow::Result; | ||
use shared_child::SharedChild; | ||
|
||
/// Spawns a child in a way where SIGINT is correctly forwarded to the child | ||
pub fn spawn_child(mut command: Command) -> Result<Arc<SharedChild>> { | ||
let shared_child = Arc::new(SharedChild::spawn(&mut command)?); | ||
let handler_shared_child = shared_child.clone(); | ||
|
||
ctrlc::set_handler(move || { | ||
// on windows, we can't send signals so just kill | ||
// we are quiting anyways so just ignore | ||
#[cfg(target_os = "windows")] | ||
handler_shared_child.kill().ok(); | ||
|
||
// on unix, we should send a SIGTERM to the child | ||
// so that go can gracefully shut down process groups | ||
// SAFETY: we could pull in the nix crate to handle this | ||
// 'safely' but nix::sys::signal::kill just calls libc::kill | ||
#[cfg(not(target_os = "windows"))] | ||
unsafe { | ||
libc::kill(handler_shared_child.id() as i32, libc::SIGTERM); | ||
} | ||
}) | ||
.expect("handler set"); | ||
|
||
Ok(shared_child) | ||
} |
This file contains 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 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 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 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