Skip to content

Clear worker thread cache after forking #3200

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

Merged
merged 7 commits into from
Feb 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions newsfragments/2764.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Clear Trio's cache of worker threads upon `os.fork`.
45 changes: 45 additions & 0 deletions src/trio/_core/_tests/test_thread_cache.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

import os
import threading
import time
from contextlib import contextmanager
Expand Down Expand Up @@ -195,3 +196,47 @@ def deliver(_: object) -> NoReturn:
err = capfd.readouterr().err
assert "don't do this" in err
assert "delivering result" in err


@pytest.mark.skipif(not hasattr(os, "fork"), reason="os.fork isn't supported")
def test_clear_thread_cache_after_fork() -> None:
assert hasattr(os, "fork")

def foo() -> None:
pass

# ensure the thread cache exists
done = threading.Event()
start_thread_soon(foo, lambda _: done.set())
done.wait()

child_pid = os.fork()

# try using it
done = threading.Event()
start_thread_soon(foo, lambda _: done.set())
done.wait()

if child_pid != 0:
# if this test fails, this will hang, triggering a timeout.
os.waitpid(child_pid, 0)
else:
# this is necessary because os._exit doesn't unwind the stack,
# so coverage doesn't get to automatically stop and save
# coverage information.
try:
import coverage

cov = coverage.Coverage.current()
# the following pragmas are necessary because if coverage:
# - isn't running, then it can't record the branch not
# taken
# - isn't installed, then it can't record the ImportError

if cov: # pragma: no branch
cov.stop()
cov.save()
except ImportError: # pragma: no cover
pass

os._exit(0) # pragma: no cover # coverage was stopped above.
13 changes: 13 additions & 0 deletions src/trio/_core/_thread_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import ctypes
import ctypes.util
import os
import sys
import traceback
from functools import partial
Expand Down Expand Up @@ -301,3 +302,15 @@ def start_thread_soon(

"""
THREAD_CACHE.start_thread_soon(fn, deliver, name)


def clear_worker_threads() -> None:
# This is OK because the child process does not actually have any
# worker threads. Additionally, while WorkerThread keeps a strong
# reference and so would get affected, the only place those are
# stored is here.
THREAD_CACHE._idle_workers.clear()


if hasattr(os, "register_at_fork"):
os.register_at_fork(after_in_child=clear_worker_threads)