openhoat/mongodb-fs — pull request #8 by vladlosev (2014-08-22 18:24:33+00:00)
Title: Fixes deadlocks when running in-process.

When mongodb-fs is run in the same process (fork: false) with the client code using Mongoose, any insert/delete/update operations hang. The reason is that mongodb-fs processes only one command per network event. For mutation operations Mongoose sends a getlasterror cmdQuery after each such op (unless the write concern is unsafe). This results in two network packets read from the socket. When mongodb-fs is run in a separate process it is able to start processing the first packet immediately. When the second packet arrives, it will generate a separate network event and mongodb-fs will process it, too.

When mongodb-fs is run in the same process with the client, it has no chance to process the first packet until the client finishes its turn in the run loop. By that time, both packets sent by the client will have arrived on the socket and the buffer supplied with the network event will have contain them both. As mongodb-fs only processes one, it will not process the getlasterror command and the client will wait forever for the response to it.

With this change, mongodb-fs will keep reading commands from the buffer until the buffer is exhausted and will be able to respond to the followup commands. The accompanying test cases all pass with the fix applied but hang without.

P.S. Thanks for this great facility! I have found it super useful.