question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

After closing web socket connection not all OkHttp's threads are terminated

See original GitHub issue

Hi,

I just wrote simple app like this:

final OkHttpClient httpClient = new OkHttpClient();

final WebSocket webSocket = httpClient.newWebSocket(new Request.Builder().url("ws://demos.kaazing.com/echo").build(), new WebSocketListener() {
    @Override
    public void onOpen(WebSocket webSocket, Response response) {
        System.out.println("STARTED!");
    }

    @Override
    public void onClosed(WebSocket webSocket, int code, String reason) {
        System.out.println("CLOSED!");
    }
});

Thread.sleep(1000);

webSocket.close(1000, "Closed.");

Thread.sleep(1000);

Thread.getAllStackTraces().keySet().stream()
        .filter(Thread::isAlive)
        .forEach(System.out::println);

System output:

STARTED!
CLOSED!
Thread[Signal Dispatcher,9,system]
Thread[Okio Watchdog,5,main]
Thread[OkHttp ConnectionPool,5,main]
Thread[main,5,main]
Thread[Finalizer,8,system]
Thread[OkHttp Dispatcher,5,main]
Thread[Reference Handler,10,system]
Thread[Monitor Ctrl-Break,5,main]

It looks like the OkHttp Dispatcher is waiting for something.

After calling:

httpClient.dispatcher().executorService().shutdown();

All OkHttp’s threads are terminated and java process is finally down.

Is it expected behaviour?

I don’t have similar problems when I call http request, the problem occurs only with web sockets.

Best, Adrian

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
yaycommented, Apr 6, 2018

I’m calling

client.dispatcher().executorService().shutdown()
client.connectionPool().evictAll()

on all OkHttpClient instances, yet the application does not exit until after the Okio Watchdog thread is done, around 30 seconds after the shutdown has been initiated.

Is there a way to manually shutdown that thread too?

0reactions
kevin4dhdcommented, May 9, 2020

Alguna solución?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Websocket connection wasn't closed after the app killed
To solve this issue I manually close socket and remove all idle connections: fun clear() { webSocket?.close(1001, "Android: User exited the ...
Read more >
Concurrency - OkHttp - Square Open Source
Its job is to maintain a reference to all open connections. When an HTTP request is started, OkHttp will attempt to reuse an...
Read more >
Unidirectional Server-Client Communication using SSE in ...
After closing the connection by either of the client and server, the connection is terminated from both ends. This is suitable for use-cases ......
Read more >
Replicator pull continous stop after network error in ...
I/CouchbaseLite/NETWORK: WebSocket CLOSED … ... The replicator does not start again when re-establishing a connection.
Read more >
Heroku Error Codes
This error is thrown when a process in your web dyno accepts a connection but then closes the socket without writing anything to...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found