hermes - 💡(How to fix) Fix concurrency: agent/transports _discovered flag set before imports complete (TOCTOU) [1 pull requests]

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…

Root Cause

Race window: Thread A sets _discovered = True, then Thread B enters get_transport(), sees _discovered = True, and skips _discover_transports(). Thread B reads _REGISTRY.get(api_mode)None (Thread A has not yet registered the transport). Thread B falls to the miss-retry path and calls _discover_transports() again — this works because Python caches modules in sys.modules, so re-imports are fast and idempotent.

Fix Action

Fixed

Code Example

def _discover_transports() -> None:
    global _discovered
    _discovered = True          # ← flag set HERE (line 52)
    try:
        import agent.transports.anthropic   # ← imports happen AFTER
    ...
RAW_BUFFERClick to expand / collapse

Bug

agent/transports/__init__.py::_discover_transports() sets _discovered = True on line 52 before any of the four import agent.transports.* calls complete.

def _discover_transports() -> None:
    global _discovered
    _discovered = True          # ← flag set HERE (line 52)
    try:
        import agent.transports.anthropic   # ← imports happen AFTER
    ...

Race window: Thread A sets _discovered = True, then Thread B enters get_transport(), sees _discovered = True, and skips _discover_transports(). Thread B reads _REGISTRY.get(api_mode)None (Thread A has not yet registered the transport). Thread B falls to the miss-retry path and calls _discover_transports() again — this works because Python caches modules in sys.modules, so re-imports are fast and idempotent.

Current behaviour: The miss-retry path in get_transport() masks the bug. However, under non-GIL Python (3.13+ free-threaded), the window is wider and the retry path is hit more often. The flag-before-work pattern is a reliability anti-pattern that should be corrected.

Fix: Move _discovered = True to after all imports complete, protected by double-checked locking (threading.Lock()).

Affected file

  • agent/transports/__init__.py_discover_transports(), get_transport()

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING