Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix request context cancel race when reading response #130

Merged
merged 1 commit into from
Nov 13, 2024

Conversation

eamonnotoole
Copy link
Contributor

@eamonnotoole eamonnotoole commented Nov 13, 2024

We had a race between request context cancellation and the reading of the corresponding response. In some circumstances the request context was cancelled before the corresponding response body was read, which resulted in a "context canceled" error.

To fix this we create a []context.CancelFunc slice in the routines that call into token-util/DoRetries and we pass-in a pointer to this slice. Then within DoRetries we append each created cancel to this slice. When we return from DoRetries we defer a function to call the cancel functions. In this way we guarantee that we read the response before the corresponding request context is cancelled which fixes the race.

We pass-in a slice to ensure that we still comply with the httpclient Do "interface".

We've adapted the relevant unit-tests, and also incresed the base httpclient timeout to 120s to allow sufficient time for all retries to be executed.

We had to fix the trivy scan GH action to pull the latest version of the
action as opposed to master.

Signed-off-by: Eamonn O'Toole [email protected]

@eamonnotoole eamonnotoole force-pushed the fix-context-cancel-race branch 3 times, most recently from 99ac698 to ea9fa39 Compare November 13, 2024 12:35
We had a race between request context cancellation and the reading of
the corresponding response.  In some circumstances the request context
was cancelled before the corresponding response body was read, which
resulted in a "context canceled" error.

To fix this we create a []context.CancelFunc slice in the routines that
call into token-util/DoRetries and we pass-in a pointer to this slice.
Then within DoRetries we append each created cancel to this slice.  When
we return from DoRetries we defer a function to call the cancel
functions.  In this way we guarantee that we read the response before
the corresponding request context is cancelled which fixes the race.

We pass-in a slice to ensure that we still comply with the httpclient Do
"interface".

We've adapted the relevant unit-tests, and also incresed the base
httpclient timeout to 120s to allow sufficient time for all retries to
be executed.

We had to fix the trivy scan GH action to pull the latest version of the
action as opposed to master.

Signed-off-by: Eamonn O'Toole <[email protected]>
@eamonnotoole eamonnotoole force-pushed the fix-context-cancel-race branch from ea9fa39 to 585c440 Compare November 13, 2024 12:37
@eamonnotoole eamonnotoole merged commit 62299cb into main Nov 13, 2024
4 checks passed
@eamonnotoole eamonnotoole deleted the fix-context-cancel-race branch November 13, 2024 13:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants