fix(gevent): don't let ddtrace threads wait for gevent logging locks (SIGN-52) #21789
All Vouch review confidence checks passed
Residual risk 0.25 = Change Risk (0.47) × (1 − Review Confidence (0.47)).
Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.47 · 491 lines diff across 6 files
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.02 · 1 substantive comment(s)
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size
diff_size
0.371
Shallow Review Depth
shallow_review_depth
0.344
Real PR Conversation Timeline
1 comments
"Review active on #21789"
general
weight: 0.35
wconti27
Description
Fixes #21770.
What
ddtrace background threads no longer wait for gevent locks when they log under gevent with module cloning. Their records go to the main thread's gevent hub, which handles them.
Why
Module cloning keeps
loggingshared with the application. Aftergevent.monkey.patch_all(), the logging lock and the handler locks are gevent locks. A native ddtrace thread that waits for one can only continue when the main hub runs, and the main thread can be waiting for that thread: joining it before a fork (_before_fork), or waiting for_forking_lockwhileThreadRestartTimerlogs under it. That is the startup hang in the issue. Fixing it at the logging boundary covers every call site.Design
ddtrace/internal/logger.pyis unchanged. All logging code is inddtrace/contrib/internal/gevent/logger.py.patch()creates aMainHubLoggingobject and calls itsinstall().unpatch()callsuninstall(). The object holds all the state.install()wrapslogging.Manager.getLogger, the single entry point that creates loggers. For ddtrace loggers only, it setshandleandisEnabledForon the instance. It does the same for the ddtrace loggers that exist already. No logger changes class, and application loggers are not touched.uninstall()removes the instance overrides and restoresgetLogger.install()also wrapsHub.__init__(and picks up a hub that exists already), registers the fork reset and registers themultiprocessingflush.handleputs the record on a bounded queue. The record keeps its shape:msg,exc_infoand a shallow copy ofargs. One drainer greenlet, scheduled withrun_callback_threadsafe, handles the records in order. Records stay inline on the main thread, without patchedthreading, before the hub exists, and when the queue is full.isEnabledForreads the level cache first. Only a cache miss off the main thread skips the logging lock.forksafehook resets the state, and the child logs inline until its hub runs. Amultiprocessingchild emits its queued records with amultiprocessing.util.Finalize, since it exits withos._exit.ddtrace.internal.threadsowns one exit function. It stops and joins the periodic threads, and then runs the functions given toregister_after_threads_stopped. The gevent integration registers its flush on theExitHooksobject there. The object replaces the native exit hook once.Testing
tests/contrib/gevent/test_main_hub_logging.py: subprocess tests with timeouts, one per behavior above, including per-instance patching andunpatch.tests/internal/test_threads.py: the exit function runs the after-threads functions after the threads are joined.geventcontrib suite, theinternalthread and atexit tests andtests/tracer/test_tracer.py::test_early_exitpass.Reviewer notes and risks
MainHubLogging.put,_patch_loggerandinstallinddtrace/contrib/internal/gevent/logger.py, then atExitHooksinddtrace/internal/threads.py.argsare a shallow copy, so changes to the logged objects after the call are visible.os.forkchild that exits withos._exitafter its hub ran can lose the records still queued.multiprocessingchildren are covered by theFinalize.DD_TRACE_GEVENT_ENABLED=false, ddtrace threads log inline, as before this change.🤖 Generated with Claude Code