[react-packager] Socket Server should not die while there active connections

Summary:
Saw an issue with a build because of an ENONT error: https://fb.facebook.com/groups/716936458354972/permalink/923628747685741/

My hypothesis:

1. We issue a ping to the socket (in SocketInterface/index.js) a decides if the available socket is alive
2. We see that it's alive but by the time we actually connect to it the server would've died

Solution:

1. The server shouldn't die as long as there are clients connected to it (currently it only stay alive as long as there are jobs)
2. The "ping" should only disconnect once the client is connected
3. Finally, have a better error message than ENOENT
This commit is contained in:
Amjad Masad 2015-09-03 14:42:47 -07:00
parent e0505fe43e
commit 54f91bd951
3 changed files with 21 additions and 4 deletions

View File

@ -29,7 +29,11 @@ class SocketClient {
this._sock = net.connect(sockPath); this._sock = net.connect(sockPath);
this._ready = new Promise((resolve, reject) => { this._ready = new Promise((resolve, reject) => {
this._sock.on('connect', () => resolve(this)); this._sock.on('connect', () => resolve(this));
this._sock.on('error', (e) => reject(e)); this._sock.on('error', (e) => {
e.message = `Error connecting to server on ${sockPath}` +
`with error: ${e.message}`;
reject(e);
});
}); });
this._resolvers = Object.create(null); this._resolvers = Object.create(null);

View File

@ -35,6 +35,8 @@ class SocketServer {
process.on('exit', () => fs.unlinkSync(sockPath)); process.on('exit', () => fs.unlinkSync(sockPath));
}); });
}); });
this._numConnections = 0;
this._server.on('connection', (sock) => this._handleConnection(sock)); this._server.on('connection', (sock) => this._handleConnection(sock));
// Disable the file watcher. // Disable the file watcher.
@ -50,10 +52,13 @@ class SocketServer {
_handleConnection(sock) { _handleConnection(sock) {
debug('connection to server', process.pid); debug('connection to server', process.pid);
this._numConnections++;
sock.on('close', () => this._numConnections--);
const bunser = new bser.BunserBuf(); const bunser = new bser.BunserBuf();
sock.on('data', (buf) => bunser.append(buf)); sock.on('data', (buf) => bunser.append(buf));
bunser.on('value', (m) => this._handleMessage(sock, m)); bunser.on('value', (m) => this._handleMessage(sock, m));
bunser.on('error', (e) => console.error(e));
} }
_handleMessage(sock, m) { _handleMessage(sock, m) {
@ -116,7 +121,7 @@ class SocketServer {
_dieEventually() { _dieEventually() {
clearTimeout(this._deathTimer); clearTimeout(this._deathTimer);
this._deathTimer = setTimeout(() => { this._deathTimer = setTimeout(() => {
if (this._jobs <= 0) { if (this._jobs <= 0 && this._numConnections <= 0) {
debug('server dying', process.pid); debug('server dying', process.pid);
process.exit(); process.exit();
} }

View File

@ -42,8 +42,16 @@ const SocketInterface = {
if (fs.existsSync(sockPath)) { if (fs.existsSync(sockPath)) {
var sock = net.connect(sockPath); var sock = net.connect(sockPath);
sock.on('connect', () => { sock.on('connect', () => {
sock.end(); SocketClient.create(sockPath).then(
resolve(SocketClient.create(sockPath)); client => {
sock.end();
resolve(client);
},
error => {
sock.end();
reject(error);
}
);
}); });
sock.on('error', (e) => { sock.on('error', (e) => {
try { try {