SECURITY: Fix token refresh race condition (nb-6yd)#3
Merged
Conversation
Added Monitor-based synchronization to prevent multiple threads from simultaneously refreshing the same OAuth token, which was causing authentication failures in multi-threaded environments (Puma, Sidekiq). Implementation: - Added Monitor (reentrant mutex) to HttpClient for thread-safe token refresh - Implemented double-check locking pattern for optimal performance: - Fast path: Check token expiry without locking (most common case) - Slow path: Acquire lock and recheck before refreshing - Only one thread can refresh a token at a time per HttpClient instance Testing: - Added comprehensive concurrency test suite (http_client_concurrency_spec.rb) - Verifies mutex prevents race condition (10 threads → 1 refresh call) - Verifies normal requests remain concurrent when token is fresh - All tests passing Security Impact: - Prevents authentication failures in production multi-threaded apps - Eliminates wasted OAuth refresh requests - Reduces risk of rate limiting due to duplicate refreshes Fixes: nb-6yd 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
956aee8 to
dd09632
Compare
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
Problem
The
ensure_fresh_token!method inHttpClientwas not thread-safe. In multi-threaded environments (Puma, Sidekiq), multiple threads could detect an expired token simultaneously and all attempt to refresh it, causing:Solution
Added
Monitor(reentrant mutex) synchronization with double-check locking:Fast Path (no lock)
Slow Path (with lock)
Implementation Details
Testing
New Concurrency Test Suite
Created
spec/nationbuilder_api/http_client_concurrency_spec.rb:Test 1: Verifies mutex prevents race condition
Test 2: Verifies normal requests remain concurrent
All tests passing: 2 new examples, 0 failures
Security Impact
Before: Race condition could cause authentication failures in production
After: Thread-safe token refresh, reliable in multi-threaded environments
Performance Impact
References
🤖 Generated with Claude Code