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

Conversation

kentaromiura
Copy link
Contributor

Summary

Fixes a race condition we observed in production, to easily reproduce it, I changed the code in onServerEnd to wait any amount of time before resolving:

const onServerEnd = (): Promise<void> => {
  clients.forEach((client) => {
    client.write('closing. kthanx, bye.');
  });
  return new Promise(ok => {
    setTimeout( () => {
      server.close();
      ok();
    }, 20);
  }
};

This change makes it easier to spot the issue
Test plan
run:

./bin/yarn install --mutex network& ./bin/yarn install --mutex network&./bin/yarn install --mutex network&

IIRC The original code was written like that because I observed some issue in relying on the close event on my original implementation that was using a unix socket.
Since then I changed the strategy and we use the network.
I cannot recreate the problem observed with this implementation.
I believe using the correct events is also much cleaner :)

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.

@kentaromiura kentaromiura force-pushed the fix-race-condition-in-mutex-network branch from f398085 to dd1caf9 Compare December 1, 2016 12:04
@bestander bestander merged commit 718d4ed into yarnpkg:master Dec 1, 2016
bestander pushed a commit that referenced this pull request Dec 1, 2016
* Fixes race condition on yarn install mutex network

* Better comment on  as per @bestander request
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants