kryonet icon indicating copy to clipboard operation
kryonet copied to clipboard

Server.close() hangs for several seconds/minutes

Open luisfonsivevo opened this issue 7 years ago • 9 comments

When calling Server.close(), the thread will hang anywhere from a few seconds to 10+ minutes. On older, slower computers, the hang can last over half an hour. Client.close() has a similar problem but will only hang up to a few seconds. Occasionally, the hang ends with a ClosedSelectorException. This problem was introduced in a7e966136798b8e8cd8898bc461e014ed4029d09, which was a fix to #93. Tested on Linux (Lubuntu) using the ChatServer example.

luisfonsivevo avatar Apr 01 '18 19:04 luisfonsivevo

Can you provide example code that shows this problem?

NathanSweet avatar Apr 05 '18 04:04 NathanSweet

Simply running new ChatServer() (from the examples folder) and closing the window causes a long delay before the JVM finally exits. All 3 linux machines I tested it on had the same problem; not sure about Windows.

luisfonsivevo avatar Apr 05 '18 13:04 luisfonsivevo

I can confirm this problem. It also happens sometimes when trying to connect to the server. I tried to reproduce it. I'm not sure if it works on your machine.

// Client#connect calls Client#close() internally. This leads to the process getting stuck (sometimes).
// See Client#close(), selector.selectNow().

Server server = new Server();
server.start();
server.bind(54555, 54777);

// Required. Works otherwise.
new Thread(new Runnable() {
	public void run() {
		while (true) {
			System.out.println("work");
		}
	}
}).start();

// Wait for the worker to start.
Thread.sleep(1000);

Client client = new Client();
client.start();
// It may be necessary to increase the timeout.
client.connect(5000, "localhost", 54555, 54777);

quarante-2 avatar Aug 31 '18 04:08 quarante-2

I don't recall ever having an issue with connecting (at least not to the same extreme as Server.close()) but I can see how that could be related to the same problem with the internal call to close() in connect().

luisfonsivevo avatar Aug 31 '18 05:08 luisfonsivevo

I've occasionally had minor issues while connecting, and definitely have had that issue while disconnecting.

I'm on Windows 10, using libGDX, Java 14 with feature preview, on IntelliJ 2020.1.2.

@NathanSweet any updates regarding the provided piece of code?

payne911 avatar Jun 12 '20 15:06 payne911

@luisfonsivevo Do you happen to call server.dispose() on your server?

See Issue https://github.com/EsotericSoftware/kryonet/issues/82. When I commented the call to my client.dispose(), my problem disappeared.

With client.dispose()

Hanging thread, with non-responding libGDX app not actually closing after closing the window. However, I do get the following log:

00:10  INFO: [kryonet] Connection 10 disconnected.
Exception in thread "Client" java.nio.channels.ClosedSelectorException
	at java.base/sun.nio.ch.SelectorImpl.ensureOpen(SelectorImpl.java:80)
	at java.base/sun.nio.ch.SelectorImpl.lockAndDoSelect(SelectorImpl.java:123)
	at java.base/sun.nio.ch.SelectorImpl.select(SelectorImpl.java:141)
	at com.esotericsoftware.kryonet.Client.update(Client.java:244)
	at com.esotericsoftware.kryonet.Client.run(Client.java:356)
	at java.base/java.lang.Thread.run(Thread.java:832)

Without client.dispose()

No Exception logged, but also nothing mentioning that the Client was disconnected.

Server-side

In both cases, while the logs differed on the client-side, the server-side always mentioned how the Client had disconnected.

I'm not sure what to think of this.


EDIT

I tested with a simple Listener and then with a ThreadedListener. I get the same behavior in terms of logging, however, the simple Listener hangs for much less time.

        this.client = new Client();
        Register.registerClasses(client);
        this.addr = isLocalServer
                ? InetAddress.getLocalHost()
                : InetAddress.getByName(REMOTE_SERVER);

//        client.addListener(new Listener.ThreadedListener(onReceiveCallback));
        client.addListener(onReceiveCallback);
        client.start();
        client.connect(TIMEOUT, addr, PORT);

payne911 avatar Jun 12 '20 23:06 payne911

It looks like it's related to commit a7e9661 Fixed deadlock in EndPoint#close(). When I rolled that back it got working immediately. No more delays.

Hotshot5000 avatar Nov 09 '21 15:11 Hotshot5000

+1

I've been experiencing this on the client side too (I don't close the server nearly as often nor the same way, I just kill it, so I don't know if the same problem happens server side).

doriancransac avatar Nov 16 '21 08:11 doriancransac