Skip to content

fix(profiler): reset ThreadContinuousScheduler lock after fork - #7275

Open
BetterAndBetterII wants to merge 1 commit into
getsentry:masterfrom
BetterAndBetterII:fix/thread-continuous-scheduler-fork-safety
Open

fix(profiler): reset ThreadContinuousScheduler lock after fork#7275
BetterAndBetterII wants to merge 1 commit into
getsentry:masterfrom
BetterAndBetterII:fix/thread-continuous-scheduler-fork-safety

Conversation

@BetterAndBetterII

Copy link
Copy Markdown

Summary

ThreadContinuousScheduler.lock can be inherited locked across os.fork(). The holding thread does not exist in the child, so ensure_running can deadlock forever when it tries to acquire the lock.

This mirrors the Monitor fix from #6159: register an os.register_at_fork(after_in_child=...) hook (via weakref.WeakMethod) that reinitialises the lock and clears the inherited thread / pid / running / buffer so the child starts clean.

Scope is deliberately limited to ThreadContinuousScheduler (#6165). SessionFlusher / BackgroundWorker (#6164) are left alone.

Test plan

  • python -m pytest tests/profiler/test_continuous_profiler.py::test_thread_continuous_scheduler_lock_reset_in_child_after_fork -v
  • RED without the hook (child exits 1), GREEN with it (child exits 0)

Fixes #6165

If os.fork() runs while ThreadContinuousScheduler.lock is held, the child
inherits a locked lock with no owner and ensure_running can deadlock.
Mirror the Monitor getsentry#6159 pattern: register_at_fork after_in_child reset.

Fixes getsentry#6165
@BetterAndBetterII
BetterAndBetterII requested a review from a team as a code owner August 27, 2026 19:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Introduce fork-safety to ThreadContinuousScheduler

1 participant