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? #537

Closed
pixelspark opened this issue Dec 29, 2010 · 5 comments
Closed
Labels

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...

@pixelspark
Copy link
Author

On Linux, the same thing happens (node v0.3.2-pre):

Without:
tommy@www3:$ md5sum in.txt
9c91b3f1cc60b8bf42aec04861ed942f in.txt
tommy@www3:
$ stat in.txt
File: `in.txt'
Size: 4727845 Blocks: 9264 IO Block: 4096 regular file
Device: fe02h/65026d Inode: 2498611 Links: 1
Access: (0755/-rwxr-xr-x) Uid: ( 1000/ tommy) Gid: ( 1000/ tommy)
Access: 2010-12-29 11:10:06.000000000 +0100
Modify: 2010-12-29 11:08:53.000000000 +0100
Change: 2010-12-29 11:08:53.000000000 +0100
tommy@www3:$ node ./bugtest.js
END of streaming: 4727845 bytes
Error event: Error: EBADF, Bad file descriptor
tommy@www3:
$ md5sum out.txt
9c91b3f1cc60b8bf42aec04861ed942f out.txt

With:

tommy@www3:~$ node ./bugtest.js 

fs.js:154
  return binding.close(fd);
                 ^
Error: EBADF, Bad file descriptor
    at Object.closeSync (fs.js:154:18)
    at [object Object].<anonymous> (/home/tommy/bugtest.js:30:5)
    at [object Object].emit (events.js:27:15)
    at afterRead (fs.js:721:12)
tommy@www3:~$ md5sum out.txt 
fedd2906818d4df1a2552157a61e6567  out.txt
tommy@www3:~$ md5sum in.txt 
9c91b3f1cc60b8bf42aec04861ed942f  in.txt

Although in some cases, it actually seems to work the way it should. Might be some kind of race condition...

@pixelspark
Copy link
Author

Strangely, piping seems to work the way it should:

var FS = require("fs");
var System = require("sys");

var inStream = FS.createReadStream("./in.txt", {mode:0600, flags:"r"});
var outStream = FS.createWriteStream("./out2.txt", {mode:0600, flags:"w"});
var bytes = 0;

inStream.pipe(outStream);

@ghost
Copy link

ghost commented Dec 29, 2010

Duplicate of #535.

@japj
Copy link

japj commented Jul 7, 2011

#535 is solved, close this issue?

@japj
Copy link

japj commented Jul 23, 2011

@isaacs can you close this issue?

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

No branches or pull requests

3 participants