christkv/node-mongodb-native — Issue #471: Infinite loop when reading GridFs file with no chunks
Author: mskott
Created: 2012-01-09 02:24:36+00:00
State: open

The script below will go into an infinite loop when attempting to read a GridFS file which doesn't have any chunks in fs.chunks. Monitoring db.currentOP(true) in the Mongo shell show operations attempting to read from fs.chunk with ever increasing opid's. Attempting to read the same file using mongofiles results in an assertion error.

The problem is the same on both Node v0.4 and 0.6.

```JavaScript
var FILE = "empty.test.file";
var mongodb = require('mongodb');
var mongo_server = new mongodb.Server('localhost', 27017,{"auto_reconnect":true,"poolSize":1});
var mongo_db = new mongodb.Db('test', mongo_server);
mongo_db.open(
function (err,db)
{
//Create a file with no chunks
mongo_db.collection('fs.files',
function (err, collection)
{
collection.insert(
{filename: FILE,
"contentType" : "application/json; charset=UTF-8",
"length" : 91,
"chunkSize" : 262144,
"aliases" : null,
"metadata" : {},
"md5" : "4e638392b289870da9291a242e474930"},
{safe:true},
readEmptyFile
);
}
);
}
);
function readEmptyFile (err, docs)
{
var gs = new mongodb.GridStore(mongo_db, FILE, "r");
gs.open(
function (err, gs)
{
console.log("File opened");
gs.read(function (err,data)
{
console.log("data read");
console.log(data);
gs.close(function (){console.log("File closed")});
});
}
);
}
```