formidable icon indicating copy to clipboard operation
formidable copied to clipboard

Race condition of some sort

Open publickeating opened this issue 16 years ago • 7 comments

Sorry I can't give better details for this, but I've discovered that I'm able to cause formidable to fail by using a database session store with Connect (Express actually). It seems that the extra time that it takes for the session middleware to return means that the request can complete before the form parsing begins. Does this make any sense? If I don't send any files, the 'data' event is never picked up by formidable. If I send a small file, I get:

Error: parser error, 0 of 12888 bytes parsed
at IncomingForm.write (.../formidable/incoming_form.js:128:17)

and if I send a large file, it works fine.

I've tried putting formidable (connect-form actually) at the top of my middleware chain, but then the parsing completes before my route handler is reached (again because of the session retrieval delay I believe). I've also tried using req.pause() & req.resume() around handling the session, but I think this doesn't actually stop the reading of data, only the emission of data events.

Again, sorry I can't be more specific. I've been trying to trace this for 6 hours now and have gone in circles a few hundred times. Is there something that formidable can do to retrieve the form data if it has already arrived? (assuming that is what's happening to me)

publickeating avatar Sep 09 '10 21:09 publickeating

Just an update. I thought I could simulate a slow session response by modifying Connect's MemoryStore:

MemoryStore.prototype.get = function(sid, fn){
  var self = this;
  if (sid in this.sessions) {
      setTimeout(function(){ fn(null, self.sessions[sid]); }, 200);
  } else {
      setTimeout(function(){ fn(); }), 200);
  }
};

I get the same problem where formidable doesn't receive any 'data' callbacks, but it's something related to having an additional callback in the get(), not the time delay, because even a 1 millisecond timeout causes the problem. But every database store will need to use callbacks. Maybe this is a Connect problem?

publickeating avatar Sep 10 '10 15:09 publickeating

somewhat similar problem here. when changing from memorystore to mongostore (CONNECT) parser doesn't work anymore. my guess is: both mongo and formidable use to emit "data" events. are events global? maybe there's a collision...

Robbie-Wilhelm avatar Sep 10 '10 16:09 Robbie-Wilhelm

sessions with nstore + formidable does'nt work either. streams messing up with each other? so basically no sessions w formidable or have only one node instance running :(

Robbie-Wilhelm avatar Sep 10 '10 17:09 Robbie-Wilhelm

Sorry for the late reply. From your description it sounds like the following is happening:

  • A request comes in
  • Connect/Express is doing some async operation (session, db, whatever ...)
  • Meanwhile: The upload requests 'data' events are starting to fire, but formidable isn't listening to them yet
  • After Connect/Express is done with whatever preparation, formidable is attached to the upload request, but by now it has already missed 1 or more packets and thous fails to parse the upload stream.

As far as a solution is concerned: You would need to pause() the incoming request right when it touches Connect, and resume() it when you are attaching formidable to it. However, I think Stream.pause() does not guarantee that it will pause right away, so you may still miss an event or two.

Long story short: To solve this issue you will probably need to:

  • Report a bug at the node mailing list if Stream.pause() does not guarantee to kick in immediately like I think it should
  • Figure out what the best way is to pause() an incoming request in connect, probably by talking to the authors.

I don't think there is any defect in formidable from what you're describing so. I might be able to help fixing Stream.pause() so.

felixge avatar Sep 12 '10 13:09 felixge

Thanks for the reply. I would have agreed with you about needing the pause, except for that I tried it with only a 1 millisecond delay retrieving the session and it still failed. However, I think I do agree that this isn't an issue with formidable. I think it's something in Connect. I'll leave it for a bit and come back to it once I squash some other bugs.

publickeating avatar Sep 13 '10 15:09 publickeating

I am having this issue too. It started to happen after I migrated to nodejs 0.4.3

c4milo avatar Mar 24 '11 14:03 c4milo

Would be great if this case was somehow detected up front and a more explanatory error message was provided. Took a while to figure out that my middlewear doing mongo queries was causing this.

jfstgermain avatar Dec 11 '12 19:12 jfstgermain