Skip to content

Race condition in ServiceObject.prototype.get #2027

@jcking

Description

@jcking

Environment details

  • OS: Ubuntu 14.04
  • Node.js version: v6.9.5
  • npm version: 3.10.10
  • google-cloud-node version: google-cloud@0.46.1 @google-cloud/pubsub@0.8.0

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().

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