Skip to content

Race condition in ServiceObject.prototype.get #2027

Description

@jcking

Environment details

Steps to reproduce

This can be reproduced under specific conditions when using the "get or create" functionality described in ServiceObject.protoype.get() with { autoCreate: true }. I discovered it while using the pubsub API. If you run 2 ServiceObject.protoype.get() at the same time with the same arguments for a resource that does not exist prior, it is possible for one to succeed and the other to fail. This does not seem like intended behavior to me.

  1. Request 1 -> get
  2. Request 2 -> get
  3. Request 1 <- 404 Not Found
  4. Request 2 <- 404 Not Found
  5. Request 1 -> create
  6. Request 1 <- 200 OK
  7. Request 2 -> create
  8. Request 2 <- 409 Conflict

If these requests occur in different orders it is possible for both to pass or one to fail and the other pass.

Thoughts?

I have a proposed fix that modifies the ServiceObject.prototype.get() when { autoCreate: true } that wraps the call to ServiceObject.prototype.create() to re-run ServiceObject.prototype.get() with { autoCreate: false } if a 409 Already Exists error is detected on the call to ServiceObject.prototype.create().

Activity

  1. stephenplusplus commented on Feb 24, 2017

    @stephenplusplus
    Contributor

    Sounds good!

  2. jcking commented on Feb 24, 2017

    @jcking
    Author

    @stephenplusplus I also noticed service-object.js#L221 and service-object.js#L225 pass metadata as the third argument, however the documentation states the third argument to the callback should be the apiResponse. Are these equivalent or do you want me to modify it to be the apiResponse as well?

  3. stephenplusplus commented on Feb 25, 2017

    @stephenplusplus
    Contributor

    Yep, metadata is actually the apiResponse. Thanks for the help!

  4. danoscarmike commented on Feb 27, 2017

    @danoscarmike
    Contributor

    @stephenplusplus can this one be closed now?

  5. added
    priority: p2Moderately-important priority. Fix may not be included in next release.
    on Feb 27, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

corepriority: p2Moderately-important priority. Fix may not be included in next release.

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions