Skip to content

fix(internal): do not restart any ThreadRestartTimer threads [backport 4.5]#17325

Merged
KowalskiThomas merged 7 commits into
4.5from
juanjux/brettlangdon/fix-potential-thread-leak-backport-4.5
Apr 7, 2026
Merged

fix(internal): do not restart any ThreadRestartTimer threads [backport 4.5]#17325
KowalskiThomas merged 7 commits into
4.5from
juanjux/brettlangdon/fix-potential-thread-leak-backport-4.5

Conversation

@juanjux

@juanjux juanjux commented Apr 6, 2026

Copy link
Copy Markdown
Collaborator

Description

Backport of #17312

brettlangdon and others added 2 commits April 6, 2026 11:27
## 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:29
@juanjux juanjux changed the base branch from main to 4.5 April 6, 2026 09:29
@juanjux juanjux removed request for a team, mabdinur and taegyunkim April 6, 2026 09:30
@juanjux juanjux closed this Apr 6, 2026
@juanjux juanjux reopened this Apr 6, 2026
@juanjux

juanjux commented Apr 6, 2026

Copy link
Copy Markdown
Collaborator Author

Closed and reopen to fix the RUN_ID thing.

@KowalskiThomas

KowalskiThomas commented Apr 7, 2026

Copy link
Copy Markdown
Contributor

Bumping CI.

EDIT: Ah I guess this is actually blocked on #17342

@juanjux juanjux closed this Apr 7, 2026
@juanjux juanjux reopened this Apr 7, 2026
@KowalskiThomas KowalskiThomas enabled auto-merge (squash) April 7, 2026 09:08
@cit-pr-commenter-54b7da

cit-pr-commenter-54b7da Bot commented Apr 7, 2026

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

@KowalskiThomas KowalskiThomas merged commit 46d947b into 4.5 Apr 7, 2026
359 checks passed
@KowalskiThomas KowalskiThomas deleted the juanjux/brettlangdon/fix-potential-thread-leak-backport-4.5 branch April 7, 2026 12:07
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.

3 participants