close the stream parse(Path) and parse(URL) open on construction failure - #630
close the stream parse(Path) and parse(URL) open on construction failure#630rootvector2 wants to merge 1 commit into
Conversation
|
Hello @rootvector2 This one feels wrong to me. In git master, please find the new
It is the responsibility of the code that allocates a resource to close it. If the the resource is managed with a try-with-resources block (see the new tests), the resource is closed for this use case. I switched the PR to draft for this reason. Please let me know how you see it. TY! |
parse(Path) and parse(URL) allocate the input stream themselves, so when createHeaders() rejects the header the caller never receives a parser and has nothing to close. Close the stream in the method that opened it and attach a close failure as suppressed. Caller-supplied readers and streams are left to the caller, matching testTryWithResourcesParse*.
52b48e3 to
c37b2b7
Compare
|
Agreed on caller-supplied readers; the new The case I was after is narrower: |
garydgregory
left a comment
There was a problem hiding this comment.
@rootvector2 On macOS, the test testParsePathClosesInputStreamWhenHeaderIsInvalid passes without changes to main, so it doesn't show anything to fix.
|
@rootvector2 |
parse(Path)andparse(URL)allocate the input stream themselves (Files.newInputStream(path),url.openStream()) and hand it to theCSVParserconstructor. WhencreateHeaders()rejects the header, the constructor throws before the caller receives a parser, so there is noCloseableto manage with try-with-resources and the stream the library opened is unreachable. Found while checking what happens to the handle when a header fails validation.Reworked per review: the caller owns what the caller allocates, and the
testTryWithResourcesParse*tests in master cover that case, so this no longer touches caller-supplied readers or streams. Only the two methods that open a stream internally now close it on failure, with a close failure attached as suppressed so the original error still surfaces.testParseUrlClosesInputStreamWhenHeaderIsInvalidfails on master (expected: <true> but was: <false>) and passes with this change; thetestTryWithResourcesParse*tests still pass. Full defaultmvnbuild is green.mvn; that'smvnon the command line by itself.