Fix race condition in lazy facilitator initialization#1812
Open
ayushozha wants to merge 1 commit intocoinbase:mainfrom
Open
Fix race condition in lazy facilitator initialization#1812ayushozha wants to merge 1 commit intocoinbase:mainfrom
ayushozha wants to merge 1 commit intocoinbase:mainfrom
Conversation
… Flask middleware Under concurrent requests, multiple coroutines/threads could see init_done=False simultaneously and all call http_server.initialize(), causing redundant init calls and inconsistent error propagation. - FastAPI: guard with asyncio.Lock using double-checked locking pattern - Flask: guard with threading.Lock using double-checked locking pattern - Add concurrency tests proving single-flight initialization for both frameworks - Add tests verifying failed init does not permanently block retries Closes coinbase#1584
🟡 Heimdall Review Status
|
|
@ayushozha is attempting to deploy a commit to the Coinbase Team on Vercel. A member of the Team first needs to authorize it. |
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.
Summary
Fixes #1584
init_doneflag, causing multiple simultaneoushttp_server.initialize()calls. Addedasyncio.Lockwith double-checked locking to ensure single-flight initialization.self._init_doneflag under threaded WSGI servers. Addedthreading.Lockwith double-checked locking for thread-safe initialization.init_done = True, so subsequent requests can retry.Changes
python/x402/http/middleware/fastapi.pyasyncio.Lockguard around lazy initpython/x402/http/middleware/flask.pythreading.Lockguard around lazy initpython/x402/tests/unit/http/middleware/test_fastapi.pypython/x402/tests/unit/http/middleware/test_flask.pypython/x402/changelog.d/1584.bugfix.mdTest plan
test_concurrent_requests_initialize_only_once— fires 3 concurrent async requests at FastAPI middleware, assertsinitialize()called exactly oncetest_concurrent_threads_initialize_only_once— fires 5 concurrent threads at Flask middleware, assertsinitialize()called exactly oncetest_init_error_does_not_block_subsequent_requests— verifies failed init allows retry on next request (both frameworks)cc @ethanoroshiba @CarsonRoscoe