Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Finished event called too soon #4

Closed
0x80 opened this issue Mar 14, 2015 · 4 comments
Closed

Finished event called too soon #4

0x80 opened this issue Mar 14, 2015 · 4 comments

Comments

@0x80
Copy link

0x80 commented Mar 14, 2015

After unzipping I immediatly traverse the directory structure with walk. The last few directories that were unzipped are always missed on the content traversal. If I trigger the same code on an already unzipped directory it finds all content.

To me it looks like the "finished" event is called prematurely. I've tested this on node 0.10 0.12 and iojs 1.5 and all give the same behavior.

Or is this expected behavior and should I somehow work around it? I've now solved it by simply making the excution wait a little while before doing the walk but that is a hack of course, and not very reliable at all.

@binarybro
Copy link

I have the same issue as the previous poster.

I had a quick look at the source code, and it seems that the parser's flush-callback is called, after emitting the 'close' event. It works for me if I change...

Parse.prototype._flush = function (callback) {
  if (!this._streamEnd || !this._streamFinish) {
    return setImmediate(this._flush.bind(this, callback));
  }

  this.emit('close');
  return callback();
};

...to...

Parse.prototype._flush = function (callback) {
  if (!this._streamEnd || !this._streamFinish) {
    return setImmediate(this._flush.bind(this, callback));
  }

  var r = callback();
  this.emit('close');
  return r;
};

binarybro added a commit to binarybro/node-unzip-2 that referenced this issue Mar 20, 2015
Parser's flush callback was called after emitting 'close'-event.
@0x80
Copy link
Author

0x80 commented Mar 21, 2015

Good find 👍 If it's just a callback I expect there's no point returning the result from it. Likely the original return was just a tail call.

Haven't tried it to be honest, but it would come down to

Parse.prototype._flush = function (callback) {
  if (!this._streamEnd || !this._streamFinish) {
    return setImmediate(this._flush.bind(this, callback));
  }

  callback();
  this.emit('close');
};

@joswhite
Copy link

I had the same issue. Except that my problem was that my unzip.Extract handler was on the finish event. When I changed the handler to the close event, it worked just fine. (I should have looked more closely at the examples in the test code!) My zip file is only 160kB, so I guess it wasn't large enough to be affected by the order-of-execution problem mentioned by @dzire187.

@leontastic
Copy link

I also encountered this issue. Calling fs.readFile on any newly extracted file in the finish event handler consistently results in an ENOENT: no such file or directory error. This doesn't happen if you use the close event.

glebdmitriew added a commit that referenced this issue Jan 22, 2018
@0x80 0x80 closed this as completed Mar 12, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants