fix(web_tools): prevent TOCTOU races in _get_parallel_client / _get_async_parallel_client / _get_exa_client - #24741
Closed
wesleysimplicio wants to merge 1 commit into
Conversation
…sync_parallel_client / _get_exa_client Three module-level lazy-init singletons had no lock: two concurrent threads could each see None, both construct a client, and the loser's connection pool is orphaned. Adds import threading and per-singleton Lock. Applies double-checked locking (fast lock-free early-return + re-check inside lock) to all three functions. Adds TestParallelClientToctouRace, TestAsyncParallelClientToctouRace, TestExaClientToctouRace in test_web_tools_config.py — each with a 50-thread Barrier that asserts identity of returned client and that the underlying constructor is called exactly once. Closes NousResearch#24736
Contributor
Author
|
Closing — PR has merge conflicts that can't be auto-resolved. The codebase has evolved past this fix. Re-opening with a fresh rebase welcome if the issue is still open. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What does this PR do?
Three module-level lazy-init singletons in
tools/web_tools.pyhad no lock:Root cause
Three module-level lazy-init singletons in
tools/web_tools.pyhad no lock:_parallel_client(360)if _parallel_client is None:_get_parallel_client()_async_parallel_client(361)if _async_parallel_client is None:_get_async_parallel_client()_exa_client(1009)if _exa_client is None:_get_exa_client()Under concurrent tool dispatch (MoA, parallel tool calls) two threads can simultaneously see
None, both call the constructor, and the losing thread's client — with its underlying connection pool or API session — is orphaned and never closed.Fix
Added
import threadingand onethreading.Lock()per singleton. Applied double-checked locking to each function:Same pattern applied to
_get_async_parallel_client()and_get_exa_client().Why this shape
This shape mirrors #29640 so reviewers can quickly compare scope, root cause, fix, tests, and related context without having to decode a custom PR description.
Tests
Original body
Related PRs / issues
Closes #24736
Original body
Summary
Three module-level lazy-init singletons in
tools/web_tools.pyhad no lock:What Changed
Fluxo
A mudança continua seguindo o fluxo original descrito na seção preservada abaixo, sem ampliar o escopo funcional deste PR.
Visão
A padronização melhora a revisão, reduz ruído e evita deriva de formatação entre PRs abertos.
Test Plan
Original body
What does this PR do?
Problem
Three module-level lazy-init singletons in
tools/web_tools.pyhad no lock:_parallel_client(360)if _parallel_client is None:_get_parallel_client()_async_parallel_client(361)if _async_parallel_client is None:_get_async_parallel_client()_exa_client(1009)if _exa_client is None:_get_exa_client()Under concurrent tool dispatch (MoA, parallel tool calls) two threads can simultaneously see
None, both call the constructor, and the losing thread's client — with its underlying connection pool or API session — is orphaned and never closed.Fix
Added
import threadingand onethreading.Lock()per singleton. Applied double-checked locking to each function:Same pattern applied to
_get_async_parallel_client()and_get_exa_client().Tests
Added to
tests/tools/test_web_tools_config.py:TestParallelClientToctouRace— 50-thread Barrier; asserts all callers get the same object andParallel()is called exactly once.TestAsyncParallelClientToctouRace— same forAsyncParallel.TestExaClientToctouRace— same forExa.All 6 new tests pass (
uv run python -m pytest tests/tools/test_web_tools_config.py::TestParallelClientToctouRace tests/tools/test_web_tools_config.py::TestAsyncParallelClientToctouRace tests/tools/test_web_tools_config.py::TestExaClientToctouRace -v).Closes #24736
Solution Sketch
Related Issue
Closes #24736
Type of Change
Changes Made
.github/PULL_REQUEST_TEMPLATE.mdHow to Test
Checklist
Code
fix(scope):,feat(scope):, etc.)pytest tests/ -qand all tests passDocumentation & Housekeeping
docs/, docstrings) — or N/Acli-config.yaml.exampleif I added/changed config keys — or N/ACONTRIBUTING.mdorAGENTS.mdif I changed architecture or workflows — or N/AScreenshots / Logs
Generated by Hermes Turbo
Generated by Hermes Turbo