Skip to content

Instantly share code, notes, and snippets.

@bobrik
Created September 4, 2012 06:24
Show Gist options
  • Save bobrik/3617558 to your computer and use it in GitHub Desktop.
Save bobrik/3617558 to your computer and use it in GitHub Desktop.
// for some reason from time to time it may output WTFs
// the way i run it: trace -f node buggy.js 2>&1 | grep '\(pwrite\|pread\|WTF\)'
// all reads come after writes
var fs = require("fs"),
file = fs.openSync("aa", "a+"),
bufs = {},
off = fs.fstatSync(file).size,
offs = {};
offs = { '4890': 140278,
'7928': 51093,
'8731': 0,
'11806': 59021,
'14394': 125884,
'17800': 108084,
'18028': 70827,
'19229': 88855,
'21116': 29977,
'21246': 8731 }
Object.keys(offs).forEach(function(o) {
offs[o] += off;
});
function buf(size) {
if (!bufs[size]) {
bufs[size] = new Buffer(size);
}
return bufs[size];
}
function write(size, callback) {
var offset = off;
offs[size] = offset;
off += size;
fs.write(file, buf(size), 0, buf(size).length, offset, callback);
}
function reader() {
console.log(bufs);
console.log(offs)
Object.keys(bufs).forEach(function(size) {
size = +size;
var offset = offs[size],
test = new Buffer(size);
fs.read(file, test, 0, test.length, offset, function() {
if (test.toString("base64") != buf(size).toString("base64")) {
console.log("WTF? " + test.length + ":" + buf(size).length);
console.log(buf(size).slice(0,4).toString("base64"), test.slice(0, 4).toString("base64"))
}
});
});
}
write(8731)
write(21246)
write(21116)
write(7928)
write(11806)
write(18028)
write(19229)
write(17800)
write(14394)
write(4890)
reader()
# strace -f node buggy.js 2>&1 | grep '\(pwrite\|pread\|WTF\)'
[pid 12081] pread(7, "var fs = require(\"fs\"),\n\tfile "..., 1775, 0) = 1775
[pid 12083] pwrite(7, "H\213T$ \366\302\1\17\204a\1\0\0H\203\354 H\211T$\10I\272\20\312\200\1\0\0\0"..., 8731, 3193696 <unfinished ...>
[pid 12083] <... pwrite resumed> ) = 8731
[pid 12083] pwrite(7, "\377\377\377\377\0\0\0\0\0\0\0\0\0\0\0\0\1\0\1\1\1\0\0\0\260\221:\315\267\177\0\0"..., 21246, 3202427 <unfinished ...>
[pid 12083] <... pwrite resumed> ) = 21246
[pid 12083] pwrite(7, "\0\0\0\0\2\0\0\0\0\0\0\0\377\377\377\377\377\377\377\377\0\0\0\0\0\0\0\0\0\0\0\0"..., 21116, 3223673 <unfinished ...>
[pid 12083] <... pwrite resumed> ) = 21116
[pid 12083] pwrite(7, "\230\305\275\313\267\177\0\0\230\305\275\313\267\177\0\0\240\302\201\1\0\0\0\0\240\302\201\1\0\0\0\0"..., 7928, 3244789 <unfinished ...>
[pid 12083] <... pwrite resumed> ) = 7928
[pid 12083] pwrite(7, "0\244:\315\267\177\0\0\277\0\0\0\300\0\0\0005\0\0\0\0\0\0\0\220+\203\1\0\0\0\0"..., 11806, 3252717 <unfinished ...>
[pid 12083] <... pwrite resumed> ) = 11806
[pid 12083] pwrite(7, "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"..., 19229, 3282551 <unfinished ...>
[pid 12083] <... pwrite resumed> ) = 19229
[pid 12084] pwrite(7, "\0\244|\1\0\0\0\0\0\0\0\0\0\0\0\0Xz\203\1\0\0\0\0C\0\0\0*\0\0\0"..., 18028, 3264523 <unfinished ...>
[pid 12084] <... pwrite resumed> ) = 18028
[pid 12083] pwrite(7, "\360\233:\315\267\177\0\0\31\0\0\0\32\0\0\0 \336\200\1\0\0\0\0\230\324\203\1\0\0\0\0"..., 17800, 3301780 <unfinished ...>
[pid 12083] <... pwrite resumed> ) = 17800
[pid 12083] pwrite(7, "\260\222:\315\267\177\0\0\377\377\377\377\0\0\0\0000\223:\315\267\177\0\0\241)\0\0\0\0\0\0"..., 14394, 3319580 <unfinished ...>
[pid 12083] <... pwrite resumed> ) = 14394
[pid 12085] pwrite(7, "p\303|\1\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\244|\1\0\0\0\0"..., 4890, 3333974) = 4890
[pid 12084] pread(7, <unfinished ...>
[pid 12084] <... pread resumed> "p\303|\1\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\244|\1\0\0\0\0"..., 4890, 3333974) = 4890
[pid 12084] pread(7, <unfinished ...>
[pid 12084] <... pread resumed> "\230\305\275\313\267\177\0\0\230\305\275\313\267\177\0\0\240\302\201\1\0\0\0\0\240\302\201\1\0\0\0\0"..., 7928, 3244789) = 7928
[pid 12085] pread(7, <unfinished ...>
[pid 12085] <... pread resumed> "H\213T$ \366\302\1\17\204a\1\0\0H\203\354 H\211T$\10I\272\20\312\200\1\0\0\0"..., 8731, 3193696) = 8731
[pid 12085] pread(7, <unfinished ...>
[pid 12085] <... pread resumed> "0\244:\315\267\177\0\0\277\0\0\0\300\0\0\0005\0\0\0\0\0\0\0\220+\203\1\0\0\0\0"..., 11806, 3252717) = 11806
[pid 12084] pread(7, <unfinished ...>
[pid 12084] <... pread resumed> "\260\222:\315\267\177\0\0\377\377\377\377\0\0\0\0000\223:\315\267\177\0\0\241)\0\0\0\0\0\0"..., 14394, 3319580) = 14394
[pid 12085] pread(7, <unfinished ...>
[pid 12085] <... pread resumed> "\360\233:\315\267\177\0\0\31\0\0\0\32\0\0\0 \336\200\1\0\0\0\0\230\324\203\1\0\0\0\0"..., 17800, 3301780) = 17800
[pid 12083] pread(7, <unfinished ...>
[pid 12083] <... pread resumed> "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"..., 18028, 3264523) = 18028
[pid 12083] pread(7, <unfinished ...>
[pid 12083] <... pread resumed> "\0\0\0\0\0\0\0\0\0\0\0\0\250\350\203\1\0\0\0\0a\244\3669\267\177\0\0008\350\203\1"..., 19229, 3282551) = 19229
[pid 12085] pread(7, <unfinished ...>
[pid 12085] <... pread resumed> "\0\0\0\0\2\0\0\0\0\0\0\0\377\377\377\377\377\377\377\377\0\0\0\0\0\0\0\0\0\0\0\0"..., 21116, 3223673) = 21116
[pid 12084] pread(7, "\377\377\377\377\0\0\0\0\0\0\0\0\0\0\0\0\1\0\1\1\1\0\0\0\260\221:\315\267\177\0\0"..., 21246, 3202427) = 21246
[pid 12081] write(1, "WTF? 18028:18028\n", 17WTF? 18028:18028
[pid 12081] write(1, "WTF? 19229:19229\n", 17WTF? 19229:19229
@bobrik
Copy link
Author

bobrik commented Sep 4, 2012

setTimeout(reader, 1000) instead of just reader provide the same results.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment