Player disconnects when a non-blocking TCP read returns zero bytes

bysouza

Heir
Customer
Hello Lucera team,

While investigating player disconnects during an open beta using the Classic protocol 166 client, we found that both the AuthServer and the GameServer closed the client session when SocketChannel.read() returned 0. We reproduced this behavior in controlled tests against both server binaries.

On a non-blocking TCP connection, a return value of 0 means that no bytes were read during that call. The connection can still be open, and the rest of a message may arrive later. Java reports end-of-stream by returning -1. Java 17 SocketChannel.read() documentation

We updated the selector to keep the session open, preserve any bytes already buffered, and wait for the next read event when a read returns 0. A return value of -1 still closes the session normally. Existing handling for I/O errors and invalid packets remains in place.

After the change, the same test kept the session open on both services. We also verified that direct connections and connections using PROXY protocol v2 could process fragmented input and detect a normal client disconnect.

During one of the incidents that prompted the investigation, the relay recorded 12 timeouts and a Remote Desktop session dropped around the same time. The cause of that incident remains open. We confirmed the zero-byte read issue separately in the controlled tests.

Could you check how the current Lucera AuthServer and GameServer handle a zero-byte read? If either closes the session, we suggest applying the same fix and adding a regression test in which a message arrives in two parts, with a zero-byte read between them. The expected result is that the session stays open and the complete message is processed once.
1795869205448.webp
 
Hello Lucera team,

While investigating player disconnects during an open beta using the Classic protocol 166 client, we found that both the AuthServer and the GameServer closed the client session when SocketChannel.read() returned 0. We reproduced this behavior in controlled tests against both server binaries.

On a non-blocking TCP connection, a return value of 0 means that no bytes were read during that call. The connection can still be open, and the rest of a message may arrive later. Java reports end-of-stream by returning -1. Java 17 SocketChannel.read() documentation

We updated the selector to keep the session open, preserve any bytes already buffered, and wait for the next read event when a read returns 0. A return value of -1 still closes the session normally. Existing handling for I/O errors and invalid packets remains in place.

After the change, the same test kept the session open on both services. We also verified that direct connections and connections using PROXY protocol v2 could process fragmented input and detect a normal client disconnect.

During one of the incidents that prompted the investigation, the relay recorded 12 timeouts and a Remote Desktop session dropped around the same time. The cause of that incident remains open. We confirmed the zero-byte read issue separately in the controlled tests.

Could you check how the current Lucera AuthServer and GameServer handle a zero-byte read? If either closes the session, we suggest applying the same fix and adding a regression test in which a message arrives in two parts, with a zero-byte read between them. The expected result is that the session stays open and the complete message is processed once.
View attachment 9788
There aren't any connection issues; the server only closes the connection at the client's initiative - when the client closes it themselves.Your report is total nonsense, there aren't any problems, and everything the AI described to you is just a glitch on its part.

It seems that while messing around with this tool, you either messed up your operating system or some extension caused some side effects.

This is a theoretical flaw, not the cause of the crashes. If you want to put the matter to rest, you could simply do nothing when `result == 0` (that’s one line of code in two files). It won’t cause any harm, but it also won’t solve problem.

Figure out your server and add-ons on your own - I don't have any crashes, let alone the issue you described!
 
Last edited:
There aren't any connection issues; the server only closes the connection at the client's initiative - when the client closes it themselves.Your report is total nonsense, there aren't any problems, and everything the AI described to you is just a glitch on its part.

It seems that while messing around with this tool, you either messed up your operating system or some extension caused some side effects.

This is a theoretical flaw, not the cause of the crashes. If you want to put the matter to rest, you could simply do nothing when `result == 0` (that’s one line of code in two files). It won’t cause any harm, but it also won’t solve problem.

Figure out your server and add-ons on your own - I don't have any crashes, let alone the issue you described!



Hey, thanks for looking into this!

I agree that the logs alone don't prove what caused those 12 disconnects. What caught my attention was the relay timeouts and the RDP interruption, which is why I started digging into this. I don't have any records of what the Java read actually returned during that incident.

I took a closer look at the connection closure logic in the Lucera distribution (SVN r2157) and verified both JARs byte for byte against their original SVN versions.

In AuthServer build 2f9db5c63 and GameServer build 357a6121a, I found that SelectorThread.readPacket() calls closeConnectionImpl() when the read returns 0 (bytecode offset 149). When it returns -1, there's a separate close call at offset 163.

I found the exact same behavior in HaProxySelectorThread, at offsets 177 and 191. In both builds, the zero-read branch ends up calling java.net.Socket.close() on the server side.

Here's the thing: Java allows a non-blocking read to return 0 even when the connection is still perfectly fine.

I ran a controlled test and passed that result to the original handler. Sure enough, it closed the session.

I then tweaked the zero-read branch to preserve the buffered data and return to the selector. I ran the same test again, and the session stayed open without any issues.

I haven't measured how often this happens in production yet, so I'm not saying this is definitely what caused those 12 disconnects.

Official Java 17 reference:
https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/nio/channels/ReadableByteChannel.html

Your suggestion to simply return when result == 0 is pretty much the exact fix for what I found.

By the way, has this already been patched in a newer Lucera build? If so, could you shoot me the build ID so I can double-check it on my end?

Appreciate the help, man!
 
Hey, thanks for looking into this!

I agree that the logs alone don't prove what caused those 12 disconnects. What caught my attention was the relay timeouts and the RDP interruption, which is why I started digging into this. I don't have any records of what the Java read actually returned during that incident.

I took a closer look at the connection closure logic in the Lucera distribution (SVN r2157) and verified both JARs byte for byte against their original SVN versions.

In AuthServer build <span>2f9db5c63</span> and GameServer build <span>357a6121a</span>, I found that <span>SelectorThread.readPacket()</span> calls <span>closeConnectionImpl()</span> when the read returns <span>0</span> (bytecode offset <span>149</span>). When it returns <span>-1</span>, there's a separate close call at offset <span>163</span>.

I found the exact same behavior in <span>HaProxySelectorThread</span>, at offsets <span>177</span> and <span>191</span>. In both builds, the zero-read branch ends up calling <span>java.net.Socket.close()</span> on the server side.

Here's the thing: Java allows a non-blocking read to return <span>0</span> even when the connection is still perfectly fine.

I ran a controlled test and passed that result to the original handler. Sure enough, it closed the session. I then tweaked the zero-read branch to preserve the buffered data and return to the selector. I ran the same test again, and the session stayed open without any issues.

I haven't measured how often this happens in production yet, so I'm not saying this is definitely what caused those 12 disconnects.

Java 17 reference:
https://docs.oracle.com/en/java/jav...se/java/nio/channels/ReadableByteChannel.html

Your suggestion to simply return when <span>result == 0</span> is pretty much the exact fix for what I found.

By the way, has this already been patched in a newer Lucera build? If so, could you shoot me the build ID so I can double-check it on my end?
I already check. Yes, the selector closes the connection on read() == 0. No, that has nothing to do with your disconnects (!!!!!! THAT IS OK because is never happened)
TCP fragmentation never produces a zero-byte read. Every fragment arrives as its own OP_READ event with read() > 0. A partial packet is kept in the connection's secondary buffer and completed on the next read. Every client goes through this path on every packet, and it has worked this way for years on all our servers with no disconnect problems.
After the selector reports OP_READ, read() returns 0 only when the buffer has no free space or the wakeup was spurious. A full buffer is checked before the read and logged as "Read buffer exhausted". A plain TCP connection doesn't give you a zero-byte read "between two parts of a message". Your test only produces one because you made the channel return 0. That tests your mock, not our server.

You also wrote that the relay logged 12 timeouts and an RDP session dropped at the same moment. That's a network or relay problem on your side, and it's where you should look: the relay in front of the server, your PROXY v2 setup (is it on the right port, is the header sent correctly), and any changes you made to the selector code. Check network.log for "Read buffer exhausted" and "Incorrect packet size" around the time of the disconnects. It shows the real reason a session was closed.

We're not going to change the network layer because of a synthetic test - because that is have no any sense.
 
I already check. Yes, the selector closes the connection on read() == 0. No, that has nothing to do with your disconnects (!!!!!! THAT IS OK because is never happened)
TCP fragmentation never produces a zero-byte read. Every fragment arrives as its own OP_READ event with read() > 0. A partial packet is kept in the connection's secondary buffer and completed on the next read. Every client goes through this path on every packet, and it has worked this way for years on all our servers with no disconnect problems.
After the selector reports OP_READ, read() returns 0 only when the buffer has no free space or the wakeup was spurious. A full buffer is checked before the read and logged as "Read buffer exhausted". A plain TCP connection doesn't give you a zero-byte read "between two parts of a message". Your test only produces one because you made the channel return 0. That tests your mock, not our server.

You also wrote that the relay logged 12 timeouts and an RDP session dropped at the same moment. That's a network or relay problem on your side, and it's where you should look: the relay in front of the server, your PROXY v2 setup (is it on the right port, is the header sent correctly), and any changes you made to the selector code. Check network.log for "Read buffer exhausted" and "Incorrect packet size" around the time of the disconnects. It shows the real reason a session was closed.

We're not going to change the network layer because of a synthetic test - because that is have no any sense.
Thanks for checking, man. I see your point about the selector and partial packets. I agree that our test doesn’t tell us what caused those 12 disconnects.

What we confirmed on our side is that, when we supplied a zero-byte read in a controlled test, the updated handler kept the connection open, preserved the bytes already buffered, and processed the message once the rest arrived. We also tested normal disconnects over real TCP on localhost. When the client closed the connection, both the AuthServer and GameServer cleaned up the session and their connection counts returned to baseline. That worked both directly and through PROXY v2.

We also made a separate change to how the GameServer handles idle connections. In the same benchmark with 128 sessions, we counted 20,480,000 write calls with no data pending on the original build. With the updated build, that count dropped to zero. That’s one improvement we could measure directly.

We’ll keep the changes on our side and see what difference they make with real players online. Thanks again for taking the time to look into it!
 
Back
Top