Skip to content

Commit

Permalink
[react-packager] Fix races
Browse files Browse the repository at this point in the history
Summary:
A few potential races to fix:

1. Multiple clients maybe racing to delete a zombie socket
2. Servers who should die because other servers are already listening are taking the socket with them (move `process.on('exit'` code to after the server is listening
3. Servers which are redundant should immediatly die
  • Loading branch information
Amjad Masad committed Aug 27, 2015
1 parent 1598cc6 commit 34b5aa2
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
10 changes: 7 additions & 3 deletions packager/react-packager/src/SocketInterface/SocketServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class SocketServer {
options
);
resolve(this);
process.on('exit', () => fs.unlinkSync(sockPath));
});
});
this._server.on('connection', (sock) => this._handleConnection(sock));
Expand All @@ -41,8 +42,6 @@ class SocketServer {
this._packagerServer = new Server(options);
this._jobs = 0;
this._dieEventually();

process.on('exit', () => fs.unlinkSync(sockPath));
}

onReady() {
Expand Down Expand Up @@ -138,12 +137,17 @@ class SocketServer {
process.send({ type: 'createdServer' });
},
error => {
debug('error creating server', error.code);
if (error.code === 'EADDRINUSE') {
// Server already listening, this may happen if multiple
// clients where started in quick succussion (buck).
process.send({ type: 'createdServer' });

// Kill this server because some other server with the same
// config and socket already started.
debug('server already started');
setImmediate(() => process.exit());
} else {
debug('error creating server', error.code);
throw error;
}
}
Expand Down
8 changes: 7 additions & 1 deletion packager/react-packager/src/SocketInterface/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const SocketClient = require('./SocketClient');
const SocketServer = require('./SocketServer');
const _ = require('underscore');
const crypto = require('crypto');
const debug = require('debug')('ReactPackager:SocketInterface');
const fs = require('fs');
const net = require('net');
const path = require('path');
Expand Down Expand Up @@ -45,7 +46,12 @@ const SocketInterface = {
resolve(SocketClient.create(sockPath));
});
sock.on('error', (e) => {
fs.unlinkSync(sockPath);
try {
debug('deleting socket for not responding', sockPath);
fs.unlinkSync(sockPath);
} catch (err) {
// Another client might have deleted it first.
}
createServer(resolve, reject, options, sockPath);
});
} else {
Expand Down

0 comments on commit 34b5aa2

Please sign in to comment.