I'm working on a large multiplayer game, client-server, home made protocol, lots of Client threads inside the server. And 1 scheduler to avoid troubles.
But.. My scheduler often gets blocked. Thanks to jstack, I was able to discover where the block happens. Here's the stack:
"WorldScheduler" prio=10 tid=0x00007ff1f83b0800 nid=0x2eb4 runnable [0x00007ff1e2d91000]
at java.net.SocketOutputStream.socketWrite0(Native Method)
Here's Client.receiveMessage, totally innocent except that it's called the scheduler thread... :
What the hell? How is a write supposed to block? Or at least lag for a very long time? And it happens so rarely, maybe 1 time over 100.000.
I missed something I guess. When it happens, I'm forced to kill and restart the server ASAP because there are tons of players blocked.
Anyone has an explanation on this? I'm adding a log to make sure the string I try to write isn't infinetly long...
Thanks a lot for your time guys.
EDIT: if write can be a blocking function, or can take long, then I should rewrite my Client class.
The scheduler thread would have to delegate the write to Client thread. But currently Client threads are used ONLY for reading, yes read is blocking which is natural.
Will I have to use an additional thread, thus 2 per Client, one for the blocking read, and one for the blocking write? Or is there anyway to code that with a single thread?
Designer and developer on Faëria: Strategy Card Game (www.faeria.net). The server is 100% Java.
@Jiem_ on Twitter (http://twitter.com/Jiem_)
There is no reason to think that writing over a socket would not be blocking. Indeed your data shows that it does in fact block. The problem is that the OS will only be able to buffer so much TCP data. If you have a lot of traffic and writing takes time to send or confirm then yes, you will fill that buffer and should expect to block the thread trying to write the data.