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

Hang when using torch.multiprocessing after having called rr.init #6223

Closed
jleibs opened this issue May 4, 2024 · 1 comment · Fixed by #6271
Closed

Hang when using torch.multiprocessing after having called rr.init #6223

jleibs opened this issue May 4, 2024 · 1 comment · Fixed by #6271
Assignees
Labels
🪳 bug Something isn't working 🐍 Python API Python logging API
Milestone

Comments

@jleibs
Copy link
Member

jleibs commented May 4, 2024

Minimal repro:

import gc

import rerun as rr
from torch import multiprocessing


def task() -> None:
    gc.collect()


def main() -> None:
    rr.init("foo")

    proc = multiprocessing.Process(
        target=task,
    )
    proc.start()
    proc.join


if __name__ == "__main__":
    main()

The hang only happens if:

  1. We are using torch.multiprocessing
  2. We are running on linux, where the default multiprocess behavior is fork
  3. Something forces a gc to happen in the subprocess

Explanation

Although we already have a fork-handler that cleans up our global/thread-local recording streams, it's apparently still possible for an allocated PyRecordingStream to leak into the subprocess via fork (at least based on how pytorch multiprocessing works).

During __del__, we make one last call to a non-blocking flush.

While previously this was fine, we added an internal blocking batcher flush to our non-blocking sink flush, which still hangs for the same reason (the batcher processing thread is gone in the fork).

@jleibs jleibs added 🪳 bug Something isn't working 🐍 Python API Python logging API labels May 4, 2024
@jleibs jleibs added this to the 0.16 milestone May 4, 2024
@jleibs jleibs self-assigned this May 6, 2024
@jleibs jleibs changed the title Hang during multiprocess when using hugging face torch datasets with num_workers > 0 Hang when using torch.multiprocessing after having called rr.init May 8, 2024
@jleibs
Copy link
Member Author

jleibs commented May 8, 2024

Prior to 0.16, the easiest option to work around this is to call gc.collect() any time after rr.init() and before starting the multiprocessing job.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🪳 bug Something isn't working 🐍 Python API Python logging API
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant