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 d8b72f3 commit 718d4ed
Showing 1 changed file with 15 additions and 16 deletions.
31 changes: 15 additions & 16 deletions src/cli/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -268,36 +268,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 Expand Up @@ -385,7 +384,7 @@ config.init({
}
}).catch((err: Error) => {
reporter.verbose(err.stack);

if (err instanceof MessageError) {
reporter.error(err.message);
} else {
Expand Down

0 comments on commit 718d4ed

Please sign in to comment.