fix: race condition in Retry function#603
Open
deblasis wants to merge 2 commits into
Open
Conversation
This test demonstrates the data race bug when context is cancelled during a retry operation. The race occurs between: - Line 871: Goroutine writes to shared 'response' variable - Line 907: Main thread reads 'response' when ctx.Done() To reproduce: go test -race -run TestRetry_RaceCondition ./common/ Expected result (BEFORE fix): WARNING: DATA RACE Found 1 data race(s) exit status 66 FAIL Issue discovered in getsops/sops integration tests. Related: https://github.com/getsops/sops Signed-off-by: Alessandro De Blasis <alex@deblasis.net>
This commit fixes the data race by ensuring the retry goroutine checks for context cancellation and exits promptly, eliminating the race between the goroutine writing to 'response' and the main thread reading it. Changes: 1. Check ctx.Done() before each retry attempt 2. Make sleep interruptible by checking ctx.Done() during wait 3. Remove racing select statement - now always wait for channel result Benefits: - No data race: All 'response' access synchronized through channel - No goroutine leak: Goroutine exits promptly on cancellation - Preserves response: Returns last OCIResponse even when cancelled - Deterministic behavior: No timing races Before this fix: go test -race -run TestRetry_RaceCondition ./common/ WARNING: DATA RACE Found 1 data race(s) exit status 66 FAIL After this fix: go test -race -run TestRetry_RaceCondition ./common/ PASS ok (no race detected) Fixes: Data race between retry.go:871 (write) and retry.go:907 (read) Related: https://github.com/getsops/sops Signed-off-by: Alessandro De Blasis <alex@deblasis.net>
|
Thank you for your pull request and welcome to our community! To contribute, please sign the Oracle Contributor Agreement (OCA).
To sign the OCA, please create an Oracle account and sign the OCA in Oracle's Contributor Agreement Application. When signing the OCA, please provide your GitHub username. After signing the OCA and getting an OCA approval from Oracle, this PR will be automatically updated. If you are an Oracle employee, please make sure that you are a member of the main Oracle GitHub organization, and your membership in this organization is public. |
|
Thank you for signing the OCA. |
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.
This PR introduces a test to repro the data race issue and also the fix for it in a separate commit.
Issue discovered in getsops/sops integration tests.
Related: getsops/sops#1226
Failed job: https://github.com/getsops/sops/actions/runs/18238383955/job/51962367579?pr=1226
How to Review
Step 1: Reproduce the Bug (Commit f38c967)
Step 2: Verify the Fix (Commit 7770835)
Step 3: Run Full Test Suite
Problem Description
Root Cause
The
Retryfunction had a data race when the context was cancelled:Impact
nil pointer dereferencewhen accessing response fieldsOriginal Error
Solution
Changes Made
The fix ensures the retry goroutine checks for context cancellation and exits promptly:
1. Added context check before each retry attempt
2. Made sleep interruptible
3. Removed the racing select statement
Signed-off-by: Alessandro De Blasis alex@deblasis.net