Skip to content

Commit 1942b43

Browse files
authored
Keep rerun viewer from dying on ctrl-c by setting sid on unix systems (#6260)
### What - Resolves: #6186 Testing: - [x] Works on linux - [x] Works on Mac - [x] Works on Windows ### Checklist * [x] I have read and agree to [Contributor Guide](https://github.com/rerun-io/rerun/blob/main/CONTRIBUTING.md) and the [Code of Conduct](https://github.com/rerun-io/rerun/blob/main/CODE_OF_CONDUCT.md) * [x] I've included a screenshot or gif (if applicable) * [x] I have tested the web demo (if applicable): * Using examples from latest `main` build: [rerun.io/viewer](https://rerun.io/viewer/pr/6260?manifest_url=https://app.rerun.io/version/main/examples_manifest.json) * Using full set of examples from `nightly` build: [rerun.io/viewer](https://rerun.io/viewer/pr/6260?manifest_url=https://app.rerun.io/version/nightly/examples_manifest.json) * [x] The PR title and labels are set such as to maximize their usefulness for the next release's CHANGELOG * [x] If applicable, add a new check to the [release checklist](https://github.com/rerun-io/rerun/blob/main/tests/python/release_checklist)! - [PR Build Summary](https://build.rerun.io/pr/6260) - [Recent benchmark results](https://build.rerun.io/graphs/crates.html) - [Wasm size tracking](https://build.rerun.io/graphs/sizes.html) To run all checks from `main`, comment on the PR with `@rerun-bot full-check`.
1 parent cf9904b commit 1942b43

File tree

3 files changed

+32
-9
lines changed

3 files changed

+32
-9
lines changed

Cargo.lock

Lines changed: 3 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/re_sdk/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,9 @@ re_web_viewer_server = { workspace = true, optional = true }
7676
anyhow = { workspace = true, optional = true }
7777
webbrowser = { workspace = true, optional = true }
7878

79+
# Native unix dependencies:
80+
[target.'cfg(target_family = "unix")'.dependencies]
81+
libc.workspace = true
7982

8083
[dev-dependencies]
8184
re_data_store.workspace = true

crates/re_sdk/src/spawn.rs

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,11 @@ impl std::fmt::Debug for SpawnError {
132132
///
133133
/// This only starts a Viewer process: if you'd like to connect to it and start sending data, refer
134134
/// to [`crate::RecordingStream::connect`] or use [`crate::RecordingStream::spawn`] directly.
135+
#[allow(unsafe_code)]
135136
pub fn spawn(opts: &SpawnOptions) -> Result<(), SpawnError> {
137+
#[cfg(target_family = "unix")]
138+
use std::os::unix::process::CommandExt as _;
139+
136140
use std::{net::TcpStream, process::Command, time::Duration};
137141

138142
// NOTE: These are indented on purpose, it just looks better and reads easier.
@@ -244,17 +248,32 @@ pub fn spawn(opts: &SpawnOptions) -> Result<(), SpawnError> {
244248
}
245249
}
246250

247-
let rerun_bin = Command::new(&executable_path)
248-
// By default stdin is inherited which may cause issues in some debugger setups.
249-
// Also, there's really no reason to forward stdin to the child process in this case.
250-
// `stdout`/`stderr` we leave at default inheritance because it can be useful to see the Viewer's output.
251+
let mut rerun_bin = Command::new(&executable_path);
252+
253+
// By default stdin is inherited which may cause issues in some debugger setups.
254+
// Also, there's really no reason to forward stdin to the child process in this case.
255+
// `stdout`/`stderr` we leave at default inheritance because it can be useful to see the Viewer's output.
256+
rerun_bin
251257
.stdin(std::process::Stdio::null())
252258
.arg(format!("--port={port}"))
253259
.arg(format!("--memory-limit={memory_limit}"))
254260
.arg("--expect-data-soon")
255-
.args(opts.extra_args.clone())
256-
.spawn()
257-
.map_err(map_err)?;
261+
.args(opts.extra_args.clone());
262+
263+
// SAFETY: This code is only run in the child fork, we are not modifying any memory
264+
// that is shared with the parent process.
265+
#[cfg(target_family = "unix")]
266+
unsafe {
267+
rerun_bin.pre_exec(|| {
268+
// On unix systems, we want to make sure that the child process becomes its
269+
// own session leader, so that it doesn't die if the parent process crashes
270+
// or is killed.
271+
libc::setsid();
272+
273+
Ok(())
274+
})
275+
};
276+
rerun_bin.spawn().map_err(map_err)?;
258277

259278
if opts.wait_for_bind {
260279
// Give the newly spawned Rerun Viewer some time to bind.

0 commit comments

Comments
 (0)