defeat race condition when replying to client

Summary: @​public

The server dies after 30 seconds if it has no jobs on it's queue. The problem is that the jobs counter gets decreased before returning the bytes to the client. As a consequence, it's possible that the server dies while it's returning the bytes to the client, or just after it finished returning the bytes to the client.

To avoid both issues lets move the counter decrease a few lines below and bump the timer to make sure we have time to fully write the bytes on the socket and let the client close the connection before the server dies.

Reviewed By: @vjeux

Differential Revision: D2445264
This commit is contained in:
Martín Bigio 2015-09-15 16:18:17 -07:00 committed by facebook-github-bot-7
parent c4c36116cf
commit c1c61a4e88
1 changed files with 5 additions and 1 deletions

View File

@ -114,7 +114,6 @@ class SocketServer {
_reply(sock, id, type, data) { _reply(sock, id, type, data) {
debug('request finished', type); debug('request finished', type);
this._jobs--;
data = toJSON(data); data = toJSON(data);
sock.write(bser.dumpToBuffer({ sock.write(bser.dumpToBuffer({
@ -122,6 +121,11 @@ class SocketServer {
type, type,
data, data,
})); }));
// Debounce the kill timer to make sure all the bytes are sent through
// the socket and the client has time to fully finish and disconnect.
this._dieEventually();
this._jobs--;
} }
_dieEventually(delay = MAX_IDLE_TIME) { _dieEventually(delay = MAX_IDLE_TIME) {