Skip to content

fix(internal): do not restart any ThreadRestartTimer threads [backport 4.6]#17326

Merged
brettlangdon merged 1 commit into
4.6from
juanjux/brettlangdon/fix-potential-thread-leak-backport-4.6
Apr 6, 2026
Merged

fix(internal): do not restart any ThreadRestartTimer threads [backport 4.6]#17326
brettlangdon merged 1 commit into
4.6from
juanjux/brettlangdon/fix-potential-thread-leak-backport-4.6

Conversation

@juanjux

@juanjux juanjux commented Apr 6, 2026

Copy link
Copy Markdown
Collaborator

Description

Backport of #17312

## Description

We have a possible situation where we can end up with orphaned `ThreadRestartTimer` threads in fork-heavy applications.

If a fork occurs while the current `ThreadRestartTimer` thread is in the stopping state (after `cls._instance = None` has already been called) then it will appear in `periodic_threads` and be added to `_threads_to_restart_after_fork` and then force restarted.

## Testing

<!-- Describe your testing strategy or note what tests are included -->

## Risks

<!-- Note any risks associated with this change, or "None" if no risks -->

## Additional Notes

This bug is really really hard to reproduce and technically shouldn't be possible because the GIL should be held throughout the whole `os.fork()` call, but this is a code path which could cause some of the crashes we've seen in a fork-heavy application and is an easy defensive guard to add.

Co-authored-by: juanjux <juanjo.alvarezmartinez@datadoghq.com>
(cherry picked from commit 96352ec)
@juanjux juanjux requested review from a team as code owners April 6, 2026 09:33
@brettlangdon brettlangdon merged commit 906ceb5 into 4.6 Apr 6, 2026
328 checks passed
@brettlangdon brettlangdon deleted the juanjux/brettlangdon/fix-potential-thread-leak-backport-4.6 branch April 6, 2026 12:48
@cit-pr-commenter-54b7da

Copy link
Copy Markdown

Codeowners resolved as

ddtrace/internal/threads.py                                             @DataDog/apm-core-python
releasenotes/notes/fix-potential-thread-restart-timer-leak-6ba9eaa0cc4ca49a.yaml  @DataDog/apm-python

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.

2 participants