← Back to Pull requests

fix(gevent): don't let ddtrace threads wait for gevent logging locks (SIGN-52) #21789

Open
unknown opened this pull request with 491 lines diff across 6 files
View on GitHub
unknown unknown commented on Nov 13, 2026 22:34 UTC
Author

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 logging shared with the application. After gevent.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_lock while ThreadRestartTimer logs under it. That is the startup hang in the issue. Fixing it at the logging boundary covers every call site.

Design

  • ddtrace/internal/logger.py is unchanged. All logging code is in ddtrace/contrib/internal/gevent/logger.py. patch() creates a MainHubLogging object and calls its install(). unpatch() calls uninstall(). The object holds all the state.

  • install() wraps logging.Manager.getLogger, the single entry point that creates loggers. For ddtrace loggers only, it sets handle and isEnabledFor on 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 restores getLogger.

  • install() also wraps Hub.__init__ (and picks up a hub that exists already), registers the fork reset and registers the multiprocessing flush.

  • Off the main thread, handle puts the record on a bounded queue. The record keeps its shape: msg, exc_info and a shallow copy of args. One drainer greenlet, scheduled with run_callback_threadsafe, handles the records in order. Records stay inline on the main thread, without patched threading, before the hub exists, and when the queue is full.

  • isEnabledFor reads the level cache first. Only a cache miss off the main thread skips the logging lock.

  • Fork child: a forksafe hook resets the state, and the child logs inline until its hub runs. A multiprocessing child emits its queued records with a multiprocessing.util.Finalize, since it exits with os._exit.

  • Exit: ddtrace.internal.threads owns one exit function. It stops and joins the periodic threads, and then runs the functions given to register_after_threads_stopped. The gevent integration registers its flush on the ExitHooks object 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 and unpatch.

  • tests/internal/test_threads.py: the exit function runs the after-threads functions after the threads are joined.

  • The gevent contrib suite, the internal thread and atexit tests and tests/tracer/test_tracer.py::test_early_exit pass.
  • Reviewer notes and risks

  • Look at MainHubLogging.put, _patch_logger and install in ddtrace/contrib/internal/gevent/logger.py, then at ExitHooks in ddtrace/internal/threads.py.

  • Records logged by ddtrace threads are emitted later, when the main hub runs. Their args are a shallow copy, so changes to the logged objects after the call are visible.

  • A fork child logs inline until its hub runs. If a greenlet holds a handler lock before that, a ddtrace thread that logs can still wait for it.

  • A plain os.fork child that exits with os._exit after its hub ran can lose the records still queued. multiprocessing children are covered by the Finalize.

  • The fix is active only when the gevent integration patches gevent. With DD_TRACE_GEVENT_ENABLED=false, ddtrace threads log inline, as before this change.

  • If the main hub doesn't run for a long time, at most 1000 records wait in the queue; past that, threads log inline as before.
  • 🤖 Generated with Claude Code

    Diff: 491 lines (+368 / -123) across 6 files Reviewer: wconti27 Review duration: 24993m
    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
    0.47
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.02 · 1 substantive comment(s)
    0.02
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    46%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    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
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 46.7% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 47.4% (MEDIUM) · Higher = larger blast radius
    47.4%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 46.7% · Lower = rushed or shallow review
    46.7%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 25.3% · Safe to merge
    25.3%