Skip to content

Instantly share code, notes, and snippets.

@piscisaureus
Created March 18, 2011 08:45
Show Gist options
  • Save piscisaureus/875782 to your computer and use it in GitHub Desktop.
Save piscisaureus/875782 to your computer and use it in GitHub Desktop.
node.js bug
var FILENAME = "/some/pretty/big/file",
BSIZE = 1024 * 1024;
var fs = require('fs');
var foo = [];
fs.stat(FILENAME, function(err, stats) {
if (err) throw err;
var size = stats.size;
if (size < BSIZE * 50) {
throw new Error("You should provide a bigger file");
}
fs.open(FILENAME, "r", function(err, fd) {
if (err) throw err;
function doReads() {
for (var i = 0; i < 100; i++) {
var buffer = new Buffer(BSIZE);
/* Uncomment this line to avoid the bug */
//foo.push(buffer);
/* The random offset does not cause the bug but ensures the reads are slow */
var offset = Math.floor(Math.random() * (size - BSIZE));
fs.read(fd, buffer, 0, BSIZE, offset, afterRead);
}
}
function afterRead(err, bytes) {
if (err) console.log('read error: ' + err.message);
else console.log('read ok: ' + bytes);
}
doReads();
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment