fix: ensure writer lock is released on exception in RefreshAuthToken#25
Open
mofiaboss wants to merge 1 commit into
Open
fix: ensure writer lock is released on exception in RefreshAuthToken#25mofiaboss wants to merge 1 commit into
mofiaboss wants to merge 1 commit into
Conversation
Fixes #24 - ReaderWriterLock deadlock when HTTP request fails Problem: When RefreshAuthToken() encounters an exception during the HTTP request (e.g., SSL failure, connection reset, timeout), the writer lock is never released. Since this is a static lock shared across all SDK instances, one failed refresh blocks ALL subsequent SDK calls until they timeout after 30 seconds. This caused production incidents where cascading lock timeouts amplified transient network failures into extended outages. Solution: Wrap the method body in try/finally to ensure ReleaseWriterLock() is always called, regardless of success or failure. Changes: - Add try/finally block to guarantee lock release - Remove redundant reader lock acquisition in success path - Add comprehensive unit tests proving lock is released after exceptions The tests verify: - Lock is released after connection failures - Multiple sequential calls complete quickly (not 30s each) - Lock can be immediately acquired after exception - Concurrent calls all resolve without permanent contention 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
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 #24 - ReaderWriterLock deadlock when HTTP request fails during token refresh
Problem
When
RefreshAuthToken()encounters an exception during the HTTP request (e.g., SSL failure, connection reset, timeout), the writer lock is never released. Since this is a static lock shared across all SDK instances, one failed refresh blocks ALL subsequent SDK calls until they timeout after 30 seconds.This caused production incidents where cascading lock timeouts amplified transient network failures into extended outages.
Stack trace from production:
Solution
Wrap the method body in
try/finallyto ensureReleaseWriterLock()is always called, regardless of success or failure:Changes
try/finallyblock to guarantee lock release inRefreshAuthToken()Test Coverage
Added 4 new tests in
RefreshAuthTokenLockReleaseTest.cs:RefreshAuthToken_WhenConnectionRefused_ReleasesLockForNextCallRefreshAuthToken_MultipleSequentialFailures_AllCompleteQuicklyRefreshAuthToken_AfterException_CanImmediatelyAcquireLockRefreshAuthToken_ConcurrentCalls_AllResolveWithinReasonableTimeAll tests pass:
Test Plan
🤖 Generated with Claude Code