PHP sockets - client socket freezes - bug?
Posted: Fri Apr 24, 2009 5:16 pm
I am experiencing a rather strange occurence when working with sockets.
A client socket "freezes" for no aparent reason, and the server socket refuses to read from it, even though the client socket is trying to write to the server socket. On the other hand, the server socket has no problem writing to the client socket and the client reads it, so that means the connection is still established.
What bothers me the most is, that this ACTUALLY happens on localhost connections.
Let me elaborate.
I have a chatserver written in php and a flash client that connects to the php socketserver through the xml socket. I also have a "robot" client written in php.
After some time, for no aparent reason, the server stops reading from a random client socket, and it doesn't matter if it's a php client or flash client. It doesn't even matter if a connection is from a WAN or LAN or localhost.
The server stops reading from it, and all I can do is pinpoint exactly WHERE this happens:
socket_select should return an array of sockets that are ready to read from (1st parameter) but the freezed socket isn't in the array, although it actually is writing to the server (but if the server writes to the frozen socket it works, which is even more weird, because the connection didn't actually drop). So, socket_select after a random period of time ignores a random client socket that is trying to write to the server socket.
Sometimes it takes minutes for this to occur, sometimes days (the time that the client is connected to the server).
After further testing, I discovered that the longer the time span between the server start and client start, the more frequently and quickly this occurs.
For example, if a client is connected to the server almost immediately after the server is started (eg. php server.php & ; php client.php &) this almost never happens. But if a client connects to the server if a server is running for a few days or so, the client socket will be ignored by the server after a few hours or even minutes.
So now i'm wondering - is this a PHP bug or something else?
The "bug" is easily reproduced IMHO, just write a server and client, connect a bunch of clients to it, let them write to the server at a given interval, wait some time and see if anything fails.
<tl;dr>
I'm sorry for the long post, but I'm trying to give as much information as I can, because the problem seems bizzare and insoluble.
</tl;dr>
A client socket "freezes" for no aparent reason, and the server socket refuses to read from it, even though the client socket is trying to write to the server socket. On the other hand, the server socket has no problem writing to the client socket and the client reads it, so that means the connection is still established.
What bothers me the most is, that this ACTUALLY happens on localhost connections.
Let me elaborate.
I have a chatserver written in php and a flash client that connects to the php socketserver through the xml socket. I also have a "robot" client written in php.
After some time, for no aparent reason, the server stops reading from a random client socket, and it doesn't matter if it's a php client or flash client. It doesn't even matter if a connection is from a WAN or LAN or localhost.
The server stops reading from it, and all I can do is pinpoint exactly WHERE this happens:
socket_select should return an array of sockets that are ready to read from (1st parameter) but the freezed socket isn't in the array, although it actually is writing to the server (but if the server writes to the frozen socket it works, which is even more weird, because the connection didn't actually drop). So, socket_select after a random period of time ignores a random client socket that is trying to write to the server socket.
Sometimes it takes minutes for this to occur, sometimes days (the time that the client is connected to the server).
After further testing, I discovered that the longer the time span between the server start and client start, the more frequently and quickly this occurs.
For example, if a client is connected to the server almost immediately after the server is started (eg. php server.php & ; php client.php &) this almost never happens. But if a client connects to the server if a server is running for a few days or so, the client socket will be ignored by the server after a few hours or even minutes.
So now i'm wondering - is this a PHP bug or something else?
The "bug" is easily reproduced IMHO, just write a server and client, connect a bunch of clients to it, let them write to the server at a given interval, wait some time and see if anything fails.
<tl;dr>
I'm sorry for the long post, but I'm trying to give as much information as I can, because the problem seems bizzare and insoluble.
</tl;dr>