Skip to content
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

Cleanup internal data-structures when process has been forked #2676

Merged
merged 7 commits into from
Jul 12, 2023
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
9 changes: 4 additions & 5 deletions examples/python/multiprocessing/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@
import rerun as rr # pip install rerun-sdk


# Python does not guarantee that the normal atexit-handlers will be called at the
# termination of a multiprocessing.Process. Explicitly add the `shutdown_at_exit`
# decorator to ensure data is flushed when the task completes.
@rr.shutdown_at_exit
def task(child_index: int) -> None:
# All processes spawned with `multiprocessing` will automatically
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That comment (which predates the PR) doesn't make sense to me and I vote to remote it.

All processes spawned with multiprocessing will automatically be assigned the same default recording_id.

No they won't, that's why we need to call init in all children processes too 🤔

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They do have the same default recording-id... that's why calling init works instead of creating a new recording. But we still have to call init to create new sinks.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh right I confused the RecordingId with the app name... If i'm getting confused by this comment, I don't imagine it's better for end users though :|

# be assigned the same default recording_id.
Expand Down Expand Up @@ -37,11 +41,6 @@ def main() -> None:

task(0)

# Using multiprocessing with "fork" results in a hang on shutdown so
# always use "spawn"
# TODO(https://github.com/rerun-io/rerun/issues/1921)
multiprocessing.set_start_method("spawn")

for i in [1, 2, 3]:
p = multiprocessing.Process(target=task, args=(i,))
p.start()
Expand Down
28 changes: 28 additions & 0 deletions rerun_py/rerun_sdk/rerun/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
"""The Rerun Python SDK, which is a wrapper around the re_sdk crate."""
from __future__ import annotations

import functools
from typing import Any, Callable, TypeVar, cast

# NOTE: The imports determine what is public API. Avoid importing globally anything that is not public API. Use
# (private) function and local import if needed.
import rerun_bindings as bindings # type: ignore[attr-defined]
Expand Down Expand Up @@ -331,6 +334,31 @@ def _register_on_fork() -> None:

_register_on_fork()


_TFunc = TypeVar("_TFunc", bound=Callable[..., Any])


def shutdown_at_exit(func: _TFunc) -> _TFunc:
"""
Decorator to shutdown Rerun cleanly when this function exits.

Normally, Rerun installs an atexit-handler that attempts to shutdown cleanly and
flush all outgoing data before terminating. However, some cases, such as forked
processes will always skip this at-exit handler. In these cases, you can use this
decorator on the entry-point to your subprocess to ensure cleanup happens as
expected without losing data.
"""

@functools.wraps(func)
def wrapper(*args: Any, **kwargs: Any) -> Any:
try:
return func(*args, **kwargs)
finally:
rerun_shutdown()

return cast(_TFunc, wrapper)


# ---


Expand Down