--
Job Board: http://jobs.nodejs.org/
Posting guidelines: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to nod...@googlegroups.com
To unsubscribe from this group, send email to
nodejs+un...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en
> Hey, could anyone educate me on, or does anyone have any tips
> regarding race conditions?
>
> My understanding so is that a race condition occurs when you have two
> (or more) asychronous things happening, and one of these processes
> relies on something happening in the other first, and because they are
> async it may or may not happen,
...in the expected / needed order...
> in which case whether or not it works
> is conditional upon which one "wins the race".
>
> Is there a fool proof way to debug these, or is it just when you're
> noticing your code is temperamental, sometimes working sometimes not?
>
> Are they always a bad thing? What I mean is, can you rather than
> fixing the race condition, can you mitigate against it, e.g. if the
> condition isn't met, roll on anyway and fix it later on down the line.
> Is that a bad idea, due to the whole DRY thing?
While not an authoritative source, Wikipedia defines a race condition as "a flaw in [a] process whereby the output and/or result of the process is unexpectedly and critically dependent on the sequence or timing of other events."
http://en.wikipedia.org/wiki/Race_condition#Computing
So yes, I would say flaws are always a bad thing.
Once you become aware of the race condition in your code, you can correct it. We don't know what your code looks like so we can't propose methods to fix it. But either your code has a race condition (the order in which events fire could cause things in your app to go wrong), or it does not have a race condition (any order of events will result in a successful outcome); there's no in-between.
- Ben
> --
> Job Board: http://jobs.nodejs.org/
> Posting guidelines: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
> You received this message because you are subscribed to the Google
> Groups "nodejs" group.
> To post to this group, send email to nod...@googlegroups.com
> To unsubscribe from this group, send email to
> nodejs+un...@googlegroups.com
var lastId = 0;
exports.saveObj = function(obj, cb) {
lastId++;
openDatabase(dbname, function (db){
obj.id = lastId; // you LOSE!
db.saveItem(obj, cb);
})
You have a race condition because lastId is incremented from one
event, and read from another event.
In the lastid example you can simply avoid that behavior by passing a copy of lastid to the callback. Simple trick that you may already use for loop.
> I don't see a race condition here. lastId++ is always incremented before
> being saved to obj.id.
There IS a race condition!
Let us suppose that you have 2 HTTP requests R1 and R2 that come in
and that they both try to save a new object to the db with this
implementation.