Source is not closed when a JSON tester read fails - #51399
Open
dlwldn30 wants to merge 2 commits into
Open
Conversation
AbstractJsonMarshalTester.read(Resource) and read(Reader) close their source only after readObject has returned. When the delegate throws, for example on malformed JSON, closeQuietly is skipped and the source is left open. Whether this leaks depends on the delegate: Jackson and JSON-B close the source themselves, but Gson does not, so GsonTester leaks a file handle for every failed read from a Resource, File or classpath path. Close in a finally block so the tester honours its own contract regardless of the delegate. See spring-projectsgh-51384 Signed-off-by: dlwldn30 <dlwldn30@naver.com>
Spy the source and verify close() with Mockito, as suggested in review. Verification is atLeastOnce() rather than the default times(1): Jackson, Jackson2 and JSON-B close the source themselves, so the finally block makes close() run a second time. That is a no-op by contract, since Closeable specifies that closing an already closed stream has no effect. The count is not the property under test; being closed at all is. See spring-projectsgh-51384 Signed-off-by: dlwldn30 <dlwldn30@naver.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.
Re-submitted from a user-account fork, replacing #51384. Carries your review: the
close is verified with a spy rather than an
AtomicBoolean.AbstractJsonMarshalTester.read(Resource)andread(Reader)take ownership of asource, hand it to
readObject(...)and close it afterwards, so the close isskipped when
readObjectthrows, most commonly on malformed JSON.Whether that leaks depends on the delegate, which the base class cannot control.
Measured rather than assumed:
JacksonTester,Jackson2TesterandJsonbTesterclose the source themselves,
GsonTesterdoes not, soGsonTester.read(...)leaks a file handle on every failed read. Three of its four public entry points
open a real file.
The fix closes the source in a
finallyblock in both methods. For the threedelegates that already close it,
close()runs a second time. That is a no-op bycontract, since
Closeablespecifies that closing an already closed stream hasno effect, so the tests verify with
atLeastOnce()rather than pinning thecount. If you would rather the tester close exactly once, that needs a wrapper
tracking what the delegate already did, and I am happy to go that way instead.
Verified: both new tests fail for
GsonTesterTestswithout the production changeand pass for all four testers with it,
:core:spring-boot-test:test755 testswith 0 failures, and
checkFormatMain,checkFormatTest,checkstyleMainandcheckstyleTestpass.Contributed on behalf of Goatshave.