Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes race condition on yarn install mutex network #2092

Merged
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 13 additions & 15 deletions src/cli/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -268,36 +268,34 @@ 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™.
.on('connect', () => {
socket.unref(); // let it die
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do you socket.unref right after connecting?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After a chat: unref just makes the Node program not to hang if socket is the only thing left in the event loop.

process.nextTick(() => {
ok(runEventuallyWithNetwork(mutexPort));
});
})
.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 +383,7 @@ config.init({
}
}).catch((err: Error) => {
reporter.verbose(err.stack);

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