Skip to content

Commit

Permalink
Keep rerun viewer from dying on ctrl-c by setting sid on unix systems (
Browse files Browse the repository at this point in the history
…#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`.
  • Loading branch information
jleibs authored May 8, 2024
1 parent cf9904b commit 1942b43
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 9 deletions.
5 changes: 3 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions crates/re_sdk/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ re_web_viewer_server = { workspace = true, optional = true }
anyhow = { workspace = true, optional = true }
webbrowser = { workspace = true, optional = true }

# Native unix dependencies:
[target.'cfg(target_family = "unix")'.dependencies]
libc.workspace = true

[dev-dependencies]
re_data_store.workspace = true
Expand Down
33 changes: 26 additions & 7 deletions crates/re_sdk/src/spawn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,11 @@ impl std::fmt::Debug for SpawnError {
///
/// This only starts a Viewer process: if you'd like to connect to it and start sending data, refer
/// to [`crate::RecordingStream::connect`] or use [`crate::RecordingStream::spawn`] directly.
#[allow(unsafe_code)]
pub fn spawn(opts: &SpawnOptions) -> Result<(), SpawnError> {
#[cfg(target_family = "unix")]
use std::os::unix::process::CommandExt as _;

use std::{net::TcpStream, process::Command, time::Duration};

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

let rerun_bin = Command::new(&executable_path)
// By default stdin is inherited which may cause issues in some debugger setups.
// Also, there's really no reason to forward stdin to the child process in this case.
// `stdout`/`stderr` we leave at default inheritance because it can be useful to see the Viewer's output.
let mut rerun_bin = Command::new(&executable_path);

// By default stdin is inherited which may cause issues in some debugger setups.
// Also, there's really no reason to forward stdin to the child process in this case.
// `stdout`/`stderr` we leave at default inheritance because it can be useful to see the Viewer's output.
rerun_bin
.stdin(std::process::Stdio::null())
.arg(format!("--port={port}"))
.arg(format!("--memory-limit={memory_limit}"))
.arg("--expect-data-soon")
.args(opts.extra_args.clone())
.spawn()
.map_err(map_err)?;
.args(opts.extra_args.clone());

// SAFETY: This code is only run in the child fork, we are not modifying any memory
// that is shared with the parent process.
#[cfg(target_family = "unix")]
unsafe {
rerun_bin.pre_exec(|| {
// On unix systems, we want to make sure that the child process becomes its
// own session leader, so that it doesn't die if the parent process crashes
// or is killed.
libc::setsid();

Ok(())
})
};
rerun_bin.spawn().map_err(map_err)?;

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

0 comments on commit 1942b43

Please sign in to comment.