Skip to content

Commit

Permalink
Fixes race condition on yarn install mutex network (#2092)
Browse files Browse the repository at this point in the history
* Fixes race condition on yarn install mutex network

* Better comment on  as per @bestander request
  • Loading branch information
kentaromiura authored and bestander committed Dec 1, 2016
1 parent 12d0d08 commit 10a539e
Showing 1 changed file with 14 additions and 15 deletions.
29 changes: 14 additions & 15 deletions src/cli/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -265,36 +265,35 @@ const runEventuallyWithNetwork = (mutexPort: ?string): Promise<void> => {
port: +mutexPort || constants.SINGLE_INSTANCE_PORT,
};

const clients = [];
const server = net.createServer((client: net$Socket) => {
clients.push(client);
});
const server = net.createServer();

server.on('error', () => {
// another yarnn instance exists, let's connect to it to know when it dies.
// another Yarn instance exists, let's connect to it to know when it dies.
reporter.warn(reporter.lang('waitingInstance'));
const socket = net.createConnection(connectionOptions);

socket
.on('data', () => {
// the server has informed us he's going to die soon™.
socket.unref(); // let it die
process.nextTick(() => {
ok(runEventuallyWithNetwork(mutexPort));
});
.on('connect', () => {
// Allow the program to exit if this is the only active server in the event system.
socket.unref();
})
.on('close', (hadError?: boolean) => {
// the `close` event gets always called after the `error` event
if (!hadError) {
process.nextTick(() => {
ok(runEventuallyWithNetwork(mutexPort));
});
}
})
.on('error', () => {
// No server to listen to ? :O let's retry to become the next server then.
// No server to listen to ? Let's retry to become the next server then.
process.nextTick(() => {
ok(runEventuallyWithNetwork(mutexPort));
});
});
});

const onServerEnd = (): Promise<void> => {
clients.forEach((client) => {
client.write('closing. kthanx, bye.');
});
server.close();
return Promise.resolve();
};
Expand Down

0 comments on commit 10a539e

Please sign in to comment.