Skip to content

Client and server leave some threads running after close() #580

@pcdv

Description

@pcdv

I've been wanting to submit (or fix) this one for a long time.

My test runs the following code several times in a row

		WebSocketServer ws = new DummyWebSocketServer( port );

		ws.start();
		Thread.sleep( 100 );

		DummyWebSocket clt = new DummyWebSocket( port );
		clt.send( "foo" );
		Thread.sleep( 100 );
		clt.close();
		ws.stop();

then checks that after a while, no new threads are still alive.

Expected Behavior

When both the server and the client are closed, all created threads should die (at least after a given delay).

Current Behavior

Most of the time, some threads are still running after the test. Here is the output of the junit I wrote:


Thread-24
    at java.net.SocketInputStream.socketRead0(Native Method)
    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 java.net.SocketInputStream.read(SocketInputStream.java:127)
    at org.java_websocket.client.WebSocketClient.run(WebSocketClient.java:277)
    at java.lang.Thread.run(Thread.java:748)

WebsocketWriteThread
    at sun.misc.Unsafe.park(Native Method)
    at java.util.concurrent.locks.LockSupport.park(LockSupport.java:175)
    at java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.await(AbstractQueuedSynchronizer.java:2039)
    at java.util.concurrent.LinkedBlockingQueue.take(LinkedBlockingQueue.java:442)
    at org.java_websocket.client.WebSocketClient$WebsocketWriteThread.run(WebSocketClient.java:463)
    at java.lang.Thread.run(Thread.java:748)

Timer-5
    at java.lang.Object.wait(Native Method)
    at java.util.TimerThread.mainLoop(Timer.java:552)
    at java.util.TimerThread.run(Timer.java:505)

java.lang.AssertionError: Found 3 zombie thread(s) 

	at org.junit.Assert.fail(Assert.java:88)
	at org.java_websocket.util.ThreadCheck.checkZombies(ThreadCheck.java:44)

Possible Solution

I haven't looked in the code for a while but probably some cleanup is needed regarding
stop conditions for both client and server. I will try to find some time and look into the
issue if no one is interested.

Steps to Reproduce (for bugs)

See above. I can submit my test if needed.

Debug log (for bugs)

Connection lost timer started
write(154): {GET / HTTP/1.1
Connection: Upgrade
Host: localhost:58319
Sec-WebSocket-Key: u+COwPc+gIlMjkyWhCnAgw==
Sec-WebSocket-Version: 13
Upgrade: websocket

}
process(154): {GET / HTTP/1.1
Connection: Upgrade
Host: localhost:58319
Sec-WebSocket-Key: u+COwPc+gIlMjkyWhCnAgw==
Sec-WebSocket-Version: 13
Upgrade: websocket

}
write(212): {HTTP/1.1 101 Web Socket Protocol Handshake
Connection: Upgrade
Date: Sat, 21 Oct 2017 15:44:42 GMT
Sec-WebSocket-Accept: XS0GMBzGIPxJXuFs680DlBjLYvQ=
Server: TooTallNate Java-WebSocket
Upgrade: websocket

}
open using draft: Draft_6455 extension: DefaultExtension
process(212): {HTTP/1.1 101 Web Socket Protocol Handshake
Connection: Upgrade
Date: Sat, 21 Oct 2017 15:44:42 GMT
Sec-WebSocket-Accept: XS0GMBzGIPxJXuFs680DlBjLYvQ=
Server: TooTallNate Java-WebSocket
Upgrade: websocket

}
open using draft: Draft_6455 extension: DefaultExtension
Connection lost timer started
send frame: Framedata{ optcode:TEXT, fin:true, rsv1:false, rsv2:false, rsv3:false, payloadlength:[pos:0, len:3], payload:foo}
afterEnconding(3): {foo}
write(9): {����-.��B}
process(9): {����-.��B}
afterDecoding(3): {foo}
matched frame: Framedata{ optcode:TEXT, fin:true, rsv1:false, rsv2:false, rsv3:false, payloadlength:[pos:0, len:3], payload:foo}
send frame: Framedata{ optcode:CLOSING, fin:true, rsv1:false, rsv2:false, rsv3:false, payloadlength:[pos:0, len:2], payload:��}code: 1000
afterEnconding(2): {��}
write(8): {���k$�ك}
send frame: Framedata{ optcode:CLOSING, fin:true, rsv1:false, rsv2:false, rsv3:false, payloadlength:[pos:0, len:2], payload:��}code: 1001
afterEnconding(2): {��}
write(4): {����}
Connection lost timer stopped
process(8): {���k$�ك}

Context

This issue is mainly important because I run a lot of tests that involve starting a WS server and eventually closing it. The bug generates the creation of a log of zombie threads in big test suites.

Your Environment

Tested on linux and macos with Java 8.

Metadata

Metadata

Assignees

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions