Skip to content

Race condition issue? #44

Open
Open
@jamesplease

Description

@jamesplease

Hi there – awesome project!

I'm wondering if there's a small bug around this code.

When the if statement matches, an attempt is made to update the user before passing it along to done. However, the call to done immediately after the if statement will likely always resolve before the update to the user completes.

This probably wouldn't affect some applications, because the update call is getting instantiated. But the middleware is likely moving onto to the next middleware a little earlier than expected.

Another consequence of this is that done will get called twice, which I'm pretty sure goes against the contract of how done is intended to be used.

I'm thinking it should instead be:

if (!user.facebook.token) {
  user.facebook.token = token;
  user.facebook.name  = profile.name.givenName + ' ' + profile.name.familyName;
  user.facebook.email = profile.emails[0].value;

  user.save(function(err) {
    if (err)
      throw err;
    return done(null, user);
  });
} else {
  return done(null, user); // user found, return that user
}

This issue likely affects the other services' code, too. What do you think?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions