fix(google_chat): double-checked locking for _load_google_modules() TOCTOU - #24679
Closed
wesleysimplicio wants to merge 1 commit into
Closed
wesleysimplicio wants to merge 1 commit into
wesleysimplicio wants to merge 1 commit into
Conversation
_google_modules_loaded was set True before the actual imports completed and before module globals (httplib2, pubsub_v1, etc.) were assigned. A concurrent thread on the fast path could read stale None globals and GOOGLE_CHAT_AVAILABLE=False. Fix: add _google_modules_lock (threading.Lock) and apply double-checked locking — fast lock-free path after init, slow path acquires lock and re-checks, all globals assigned before flag is set. Also sets _google_modules_loaded=True in the ImportError branch so a missing-deps environment doesn't retry the import on every call. Closes NousResearch#24673 Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This was referenced May 13, 2026
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?
Fixes a TOCTOU race condition in
plugins/platforms/google_chat/adapter.py::_load_google_modules().Root cause
The detailed rationale from the original PR body is preserved below. This template update keeps the review structure consistent with #29640.
Fix
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 #24673
Original body
Summary
Fixes a TOCTOU race condition in
plugins/platforms/google_chat/adapter.py::_load_google_modules().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?
Summary
Fixes a TOCTOU race condition in
plugins/platforms/google_chat/adapter.py::_load_google_modules().Bug:
_google_modules_loaded = Truewas set on line 95 before any imports ran and before any module globals (httplib2,pubsub_v1,service_account, etc.) were assigned. A concurrent thread entering on the fast path (if _google_modules_loaded: return GOOGLE_CHAT_AVAILABLE) would see the flag asTruebut read staleNoneglobals andGOOGLE_CHAT_AVAILABLE=False, causing downstreamNoneTypeerrors or silently degraded behaviour.Fix: Added
_google_modules_lock = threading.Lock()and applied the standard double-checked locking pattern:GOOGLE_CHAT_AVAILABLE = True_google_modules_loaded = Trueis set last, after all writes are visibleImportErrorpath also sets_google_modules_loaded = Trueso missing-deps environments don't retry the import on every callTest plan
tests/gateway/test_google_chat.py::TestLoadGoogleModulesToctouRace::test_concurrent_calls_return_consistent_result— 50 threads race on first call, all must see same return valuetests/gateway/test_google_chat.py::TestLoadGoogleModulesToctouRace::test_flag_set_after_globals_assigned— ImportError path still sets flag (no infinite retry)tests/gateway/test_google_chat.pysuite: 159 passed, 0 failedCloses #24673
🤖 Generated with Claude Code
Solution Sketch
Related Issue
Closes #24673
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