RFR: 8257736: InputStream from BodyPublishers.ofInputStream() leaks when IOE happens [v3]
Yasumasa Suenaga
ysuenaga at openjdk.java.net
Sat Dec 5 04:54:27 UTC 2020
> `InputStream` from `BodyPublishers.ofInputStream()` is usually closed when the stream reaches EOF. However IOE handler returns without closing.
>
> I confirmed this problem in `BodyPublishers.ofInputStream()`, but I think `BodyPublishers.ofFile()`has same problem because it also use `StreamIterator`as well as `ofInputStream()`.
>
>
> # How to reproduce:
>
> Following code (Test.java) attempts to post body from `TestInputStream` which throws IOE in `read()`. "close called" is shown on the console if `close()` is called.
>
> import java.io.*;
> import java.net.*;
> import java.net.http.*;
>
> public class Test{
>
> private static class TestInputStream extends InputStream{
>
> public TestInputStream(){
> super();
> System.out.println("test c'tor");
> }
>
> @Override
> public int read() throws IOException{
> System.out.println("read called");
> throw new IOException("test");
> }
>
> @Override
> public void close() throws IOException{
> System.out.println("close called");
> super.close();
> }
>
> }
>
> public static void main(String[] args) throws Exception{
> var http = HttpClient.newHttpClient();
> var request = HttpRequest.newBuilder()
> .uri(URI.create("http://httpbin.org/post"))
> .POST(HttpRequest.BodyPublishers.ofInputStream(() -> new TestInputStream()))
> .build();
> http.send(request, HttpResponse.BodyHandlers.discarding());
> System.out.println("Press any key to exit...");
> System.in.read();
> }
> }
Yasumasa Suenaga has updated the pull request incrementally with one additional commit since the last revision:
Throw UIOE when stream throws IOE
-------------
Changes:
- all: https://git.openjdk.java.net/jdk/pull/1614/files
- new: https://git.openjdk.java.net/jdk/pull/1614/files/7c04e983..d9628c8e
Webrevs:
- full: https://webrevs.openjdk.java.net/?repo=jdk&pr=1614&range=02
- incr: https://webrevs.openjdk.java.net/?repo=jdk&pr=1614&range=01-02
Stats: 7 lines in 2 files changed: 6 ins; 0 del; 1 mod
Patch: https://git.openjdk.java.net/jdk/pull/1614.diff
Fetch: git fetch https://git.openjdk.java.net/jdk pull/1614/head:pull/1614
PR: https://git.openjdk.java.net/jdk/pull/1614
More information about the net-dev
mailing list