fix: close non-success responses in error handler - #879
Open
sylvesterkaczmarek wants to merge 2 commits into
Open
fix: close non-success responses in error handler#879sylvesterkaczmarek wants to merge 2 commits into
sylvesterkaczmarek wants to merge 2 commits into
Conversation
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
Make the shared HTTP error handler close every non-2xx
HttpResponsebefore propagating a typed service exception or an error-body parsing failure.Fixes #745.
Problem
Generated service methods generally call the shared
errorHandler.handle(response)before entering their laterresponse.use { ... }parsing block.For 2xx responses, that is correct: the handler returns the response and the service owns its eventual close.
For non-2xx responses, however,
errorHandlercurrently reads the status/headers/body and throws a typed exception without closing the response. Because the exception is thrown before the service reaches its ownuseblock, the non-success response can remain open.The same leak can occur if the supplied error-body handler itself throws while parsing the response.
Fix
Keep successful response ownership unchanged, but put the complete non-success path inside
response.use { ... }:response.use;usecloses the response whether typed exception construction succeeds or error-body parsing throws first.The status-to-exception mapping is unchanged:
BadRequestExceptionUnauthorizedExceptionPermissionDeniedExceptionNotFoundExceptionUnprocessableEntityExceptionRateLimitExceptionInternalServerExceptionUnexpectedStatusCodeExceptionRegression coverage
Added focused tests that verify:
Validation
mainat6a46d024ed67e2be889a4c729837a664d5e7902c;Full repository validation is left to GitHub Actions because this environment does not have a complete local checkout/toolchain for the repository.
Risk
Low. The ownership change applies only to responses that are already being rejected. Successful responses remain open exactly as before, and callers still receive the same typed exceptions for each status class.