Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

Strange behaviour when reading from file stream using resume/pause? #536

Closed
pixelspark opened this issue Dec 29, 2010 · 0 comments
Closed

Comments

@pixelspark
Copy link

I recently stumbled upon a pretty weird issue with file streams in node v0.3.3-pre (pulled from git master) on OS X (10.6.5). The following script (which should just read in.txt and dump everything to out.txt) leads to different results when the resume/pause pair is commented out:

var FS = require("fs");
var System = require("sys");
var inStream = FS.createReadStream("./in.txt", {mode:0600, flags:"r"});
var outFD = FS.openSync("./out.txt", "w");
var bytes = 0;

inStream.addListener("data", function(chunk) {
    inStream.pause();
    if(chunk) {
        try {
            bytes += chunk.length;
            FS.writeSync(outFD, chunk.toString());
        }
        catch(e) {
            System.puts("Error: "+e);
            System.puts(e.stack);
        }
    }
    inStream.resume();
});

inStream.addListener("error", function(err) {
    System.puts("Error event: "+err);
});

inStream.addListener("end", function() {
    System.puts("END of streaming: "+bytes+" bytes");
    this.destroy();
    FS.closeSync(outFD);
});

Without the resume/pause pair, the MD5 of out.txt is exactly equal to the one of in.txt in all cases. However, when running with pause/resume, the MD5 of out.txt is different all the time. Also, I get two 'end' events.

tommy$ stat in.txt 
234881026 1212069 -rwxr-xr-x 1 tommy staff 0 4727845 "Dec 29 11:03:54 2010" "Dec 28 14:37:56 2010" "Dec 28 21:22:21 2010" "Dec 28 14:37:50 2010" 4096 9240 0 in.txt

Without resume/pause:
tommy$ md5 in.txt
MD5 (in.txt) = 9c91b3f1cc60b8bf42aec04861ed942f
tommy$ node ./bugtest.js
END of streaming: 4727845 bytes
Error event: Error: EBADF, Bad file descriptor
tommy$ md5 out.txt
MD5 (out.txt) = 9c91b3f1cc60b8bf42aec04861ed942f

With pause/resume:
tommy$ node ./bugtest.js
END of streaming: 4745290 bytes
END of streaming: 4745290 bytes

fs.js:171
  return binding.close(fd);
                 ^
Error: EBADF, Bad file descriptor
    at Object.closeSync (fs.js:171:18)
    at [object Object].<anonymous> (/Users/tommy/Desktop/bugtest.js:30:5)
    at [object Object].emit (events.js:28:17)
    at afterRead (fs.js:746:12)
tommy$ md5 out.txt 
MD5 (out.txt) = 749ba29f2728cd9dbf891937b42ea821
tommy$ stat out.txt 
234881026 1305678 -rw-r--r-- 1 tommy staff 0 4745290 "Dec 29 11:03:38 2010" "Dec 29 11:03:36 2010" "Dec 29 11:03:36 2010" "Dec 29 11:03:36 2010" 4096 9272 0 out.txt

Also, the EBADF is kind of weird...

This issue was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant