Skip to content

Add detach_process option to spawn #9400

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

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 20 additions & 13 deletions crates/top/re_sdk/src/spawn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ pub struct SpawnOptions {

/// Hide the welcome screen.
pub hide_welcome_screen: bool,

/// Detach Rerun Viewer process from the application process.
pub detach_process: bool,
}

// NOTE: No need for .exe extension on windows.
Expand All @@ -66,6 +69,7 @@ impl Default for SpawnOptions {
extra_args: Vec::new(),
extra_env: Vec::new(),
hide_welcome_screen: false,
detach_process: true,
}
}
}
Expand Down Expand Up @@ -283,19 +287,22 @@ pub fn spawn(opts: &SpawnOptions) -> Result<(), SpawnError> {
rerun_bin.args(opts.extra_args.clone());
rerun_bin.envs(opts.extra_env.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(())
})
};
if opts.detach_process {
// 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 {
Expand Down
5 changes: 5 additions & 0 deletions crates/top/rerun/src/commands/entrypoint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,10 @@ If no arguments are given, a server will be hosted which a Rerun SDK can connect
#[clap(long)]
hide_welcome_screen: bool,

/// Detach Rerun Viewer process from the application process.
#[clap(long)]
detach_process: bool,

/// Set the screen resolution (in logical points), e.g. "1920x1080".
/// Useful together with `--screenshot-to`.
#[clap(long)]
Expand Down Expand Up @@ -655,6 +659,7 @@ fn run_impl(

re_viewer::StartupOptions {
hide_welcome_screen: args.hide_welcome_screen,
detach_process: args.detach_process,
memory_limit: re_memory::MemoryLimit::parse(&args.memory_limit)
.map_err(|err| anyhow::format_err!("Bad --memory-limit: {err}"))?,
persist_state: args.persist_state,
Expand Down
2 changes: 2 additions & 0 deletions crates/top/rerun_c/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ pub struct CSpawnOptions {
pub port: u16,
pub memory_limit: CStringView,
pub hide_welcome_screen: bool,
pub detach_process: bool,
pub executable_name: CStringView,
pub executable_path: CStringView,
}
Expand All @@ -116,6 +117,7 @@ impl CSpawnOptions {
}

spawn_opts.hide_welcome_screen = self.hide_welcome_screen;
spawn_opts.detach_process = self.detach_process;

if !self.executable_name.is_empty() {
spawn_opts.executable_name = self.executable_name.as_str("executable_name")?.to_owned();
Expand Down
4 changes: 4 additions & 0 deletions crates/viewer/re_viewer/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ pub struct StartupOptions {
/// A user has specifically requested the welcome screen be hidden.
pub hide_welcome_screen: bool,

/// Detach Rerun Viewer process from the application process.
pub detach_process: bool,

/// Set the screen resolution in logical points.
#[cfg(not(target_arch = "wasm32"))]
pub resolution_in_points: Option<[f32; 2]>,
Expand Down Expand Up @@ -128,6 +131,7 @@ impl Default for StartupOptions {
screenshot_to_path_then_quit: None,

hide_welcome_screen: false,
detach_process: true,

#[cfg(not(target_arch = "wasm32"))]
resolution_in_points: None,
Expand Down
5 changes: 5 additions & 0 deletions docs/content/reference/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,11 @@ The Rerun command-line interface:
>
> [Default: `false`]

* `--detach-process <DETACH_PROCESS>`
> Detach Rerun Viewer process from the application process.
>
> [Default: `true`]

* `--window-size <WINDOW_SIZE>`
> Set the screen resolution (in logical points), e.g. "1920x1080". Useful together with `--screenshot-to`.

Expand Down
3 changes: 3 additions & 0 deletions rerun_cpp/src/rerun/c/rerun.h

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

1 change: 1 addition & 0 deletions rerun_cpp/src/rerun/spawn_options.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ namespace rerun {
spawn_opts.port = port;
spawn_opts.memory_limit = detail::to_rr_string(memory_limit);
spawn_opts.hide_welcome_screen = hide_welcome_screen;
spawn_opts.detach_process = detach_process;
spawn_opts.executable_name = detail::to_rr_string(executable_name);
spawn_opts.executable_path = detail::to_rr_string(executable_path);
}
Expand Down
5 changes: 5 additions & 0 deletions rerun_cpp/src/rerun/spawn_options.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ namespace rerun {
/// Defaults to `false` if unset.
bool hide_welcome_screen = false;

/// Detach Rerun Viewer process from the application process.
///
/// Defaults to `true` if unset.
bool detach_process = true;

/// Specifies the name of the Rerun executable.
///
/// You can omit the `.exe` suffix on Windows.
Expand Down
1 change: 1 addition & 0 deletions rerun_py/rerun_bindings/rerun_bindings.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -879,6 +879,7 @@ def spawn(
port: int = 9876,
memory_limit: str = ...,
hide_welcome_screen: bool = False,
detach_process: bool = True,
executable_name: str = ...,
executable_path: Optional[str] = None,
extra_args: list[str] = ...,
Expand Down
7 changes: 6 additions & 1 deletion rerun_py/rerun_sdk/rerun/_spawn.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ def _spawn_viewer(
port: int = 9876,
memory_limit: str = "75%",
hide_welcome_screen: bool = False,
detach_process: bool = True,
) -> None:
"""
Internal helper to spawn a Rerun Viewer, listening on the given port.
Expand All @@ -26,6 +27,8 @@ def _spawn_viewer(
Example: `16GB` or `50%` (of system total).
hide_welcome_screen:
Hide the normal Rerun welcome screen.
detach_process:
Detach Rerun Viewer process from the application process.

"""

Expand All @@ -39,4 +42,6 @@ def _spawn_viewer(
return
new_env["RERUN_APP_ONLY"] = "true"

rerun_bindings.spawn(port=port, memory_limit=memory_limit, hide_welcome_screen=hide_welcome_screen)
rerun_bindings.spawn(
port=port, memory_limit=memory_limit, hide_welcome_screen=hide_welcome_screen, detach_process=detach_process
)
7 changes: 6 additions & 1 deletion rerun_py/rerun_sdk/rerun/blueprint/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -662,6 +662,7 @@ def spawn(
port: int = 9876,
memory_limit: str = "75%",
hide_welcome_screen: bool = False,
detach_process: bool = True,
) -> None:
"""
Spawn a Rerun viewer with this blueprint.
Expand All @@ -679,9 +680,13 @@ def spawn(
Example: `16GB` or `50%` (of system total).
hide_welcome_screen:
Hide the normal Rerun welcome screen.
detach_process:
Detach Rerun Viewer process from the application process.

"""
_spawn_viewer(port=port, memory_limit=memory_limit, hide_welcome_screen=hide_welcome_screen)
_spawn_viewer(
port=port, memory_limit=memory_limit, hide_welcome_screen=hide_welcome_screen, detach_process=detach_process
)
self.connect_grpc(application_id=application_id, url=f"rerun+http://127.0.0.1:{port}/proxy")


Expand Down
4 changes: 4 additions & 0 deletions rerun_py/rerun_sdk/rerun/recording_stream.py
Original file line number Diff line number Diff line change
Expand Up @@ -687,6 +687,7 @@ def spawn(
connect: bool = True,
memory_limit: str = "75%",
hide_welcome_screen: bool = False,
detach_process: bool = True,
default_blueprint: BlueprintLike | None = None,
) -> None:
"""
Expand All @@ -709,6 +710,8 @@ def spawn(
Example: `16GB` or `50%` (of system total).
hide_welcome_screen:
Hide the normal Rerun welcome screen.
detach_process:
Detach Rerun Viewer process from the application process.
default_blueprint
Optionally set a default blueprint to use for this application. If the application
already has an active blueprint, the new blueprint won't become active until the user
Expand All @@ -724,6 +727,7 @@ def spawn(
connect=connect,
memory_limit=memory_limit,
hide_welcome_screen=hide_welcome_screen,
detach_process=detach_process,
default_blueprint=default_blueprint,
recording=self,
)
Expand Down
7 changes: 6 additions & 1 deletion rerun_py/rerun_sdk/rerun/sinks.py
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,7 @@ def spawn(
connect: bool = True,
memory_limit: str = "75%",
hide_welcome_screen: bool = False,
detach_process: bool = True,
default_blueprint: BlueprintLike | None = None,
recording: RecordingStream | None = None,
) -> None:
Expand All @@ -363,6 +364,8 @@ def spawn(
Example: `16GB` or `50%` (of system total).
hide_welcome_screen:
Hide the normal Rerun welcome screen.
detach_process:
Detach Rerun Viewer process from the application process.
recording:
Specifies the [`rerun.RecordingStream`][] to use if `connect = True`.
If left unspecified, defaults to the current active data recording, if there is one.
Expand All @@ -379,7 +382,9 @@ def spawn(
logging.warning("Rerun is disabled - spawn() call ignored.")
return

_spawn_viewer(port=port, memory_limit=memory_limit, hide_welcome_screen=hide_welcome_screen)
_spawn_viewer(
port=port, memory_limit=memory_limit, hide_welcome_screen=hide_welcome_screen, detach_process=detach_process
)

if connect:
connect_grpc(
Expand Down
4 changes: 3 additions & 1 deletion rerun_py/src/python_bridge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -569,11 +569,12 @@ fn send_mem_sink_as_default_blueprint(

/// Spawn a new viewer.
#[pyfunction]
#[pyo3(signature = (port = 9876, memory_limit = "75%".to_owned(), hide_welcome_screen = false, executable_name = "rerun".to_owned(), executable_path = None, extra_args = vec![], extra_env = vec![]))]
#[pyo3(signature = (port = 9876, memory_limit = "75%".to_owned(), hide_welcome_screen = false, detach_process = true, executable_name = "rerun".to_owned(), executable_path = None, extra_args = vec![], extra_env = vec![]))]
fn spawn(
port: u16,
memory_limit: String,
hide_welcome_screen: bool,
detach_process: bool,
executable_name: String,
executable_path: Option<String>,
extra_args: Vec<String>,
Expand All @@ -584,6 +585,7 @@ fn spawn(
wait_for_bind: true,
memory_limit,
hide_welcome_screen,
detach_process,
executable_name,
executable_path,
extra_args,
Expand Down
Loading