HTTP/2 uses non-daemon threads
See original GitHub issueThe following one-line program does not close properly on Java 9:
public static void main(String[] args) throws IOException {
System.out.println(new OkHttpClient.Builder().build().newCall(new Request.Builder().url("https://google.de").build()).execute());
}
It hangs for a very significant amount of time after the request is already finished before it allows the program to quit.
This a little modified version works flawlessly and allows the program to quit immediately after the request is finished.
public static void main(String[] args) throws IOException {
System.out.println(new OkHttpClient.Builder().protocols(Collections.singletonList(Protocol.HTTP_1_1)).build().newCall(new Request.Builder().url("https://google.de").build()).execute());
}
Running on Java 8 also fixes the problem, as there HTTP 1.1 is used, not HTTP 2.0.
The problem is a hanging non-deamon thread called OkHttp google.de
.
Here you have the stacktrace of the thread when the program should quit:
"OkHttp google.de@1876" prio=5 tid=0x11 nid=NA runnable
java.lang.Thread.State: RUNNABLE
at java.net.SocketInputStream.socketRead0(SocketInputStream.java:-1)
at java.net.SocketInputStream.socketRead(SocketInputStream.java:116)
at java.net.SocketInputStream.read(SocketInputStream.java:171)
at java.net.SocketInputStream.read(SocketInputStream.java:141)
at sun.security.ssl.SSLSocketInputRecord.read(SSLSocketInputRecord.java:425)
at sun.security.ssl.SSLSocketInputRecord.bytesInCompletePacket(SSLSocketInputRecord.java:65)
at sun.security.ssl.SSLSocketImpl.bytesInCompletePacket(SSLSocketImpl.java:918)
- locked <0x792> (a java.lang.Object)
at sun.security.ssl.AppInputStream.read(AppInputStream.java:144)
- locked <0x793> (a sun.security.ssl.AppInputStream)
at okio.Okio$2.read(Okio.java:140)
at okio.AsyncTimeout$2.read(AsyncTimeout.java:237)
at okio.RealBufferedSource.request(RealBufferedSource.java:68)
at okio.RealBufferedSource.require(RealBufferedSource.java:61)
at okhttp3.internal.http2.Http2Reader.nextFrame(Http2Reader.java:95)
at okhttp3.internal.http2.Http2Connection$ReaderRunnable.execute(Http2Connection.java:608)
at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32)
at java.lang.Thread.run(Thread.java:844)
I find it quite strange that this thread is hanging on a socketRead
operation, while the request is already finished successfully.
Issue Analytics
- State:
- Created 5 years ago
- Reactions:10
- Comments:49 (25 by maintainers)
Top Results From Across the Web
LESS KNOWN FACTS ABOUT DAEMON AND NON-DAEMON ...
Non-daemon threads are also known as 'user' threads. JVM will not exit even if only 1 non-daemon (i.e. user) thread is alive.
Read more >What is a daemon thread in Java? - Stack Overflow
A daemon thread is a thread that does not prevent the JVM from exiting when the program finishes but the thread is still...
Read more >Python Daemon Threads - GeeksforGeeks
Here we use current_thread() method which simplifies which thread is currently executing, and we use it with isDaemon() and daemon method to ...
Read more >Python Multithreading Tutorial: daemon threads & join method
Using daemon threads is useful for services where there may not be an easy way to interrupt the thread ... The default setting...
Read more >Issue 40234: [subinterpreters] Disallow daemon threads in ...
Even if it does exit the thread, you can't just switch it to use a non daemon thread as that will not work....
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
We built a new TaskRunner abstraction that will allow HTTP/2 threads to be daemons in 4.3.
Will be fixed in 4.5.