Skip to content

fs: read/write streams don't emit close after error #23338

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

Closed
ronag opened this issue Oct 8, 2018 · 18 comments
Closed

fs: read/write streams don't emit close after error #23338

ronag opened this issue Oct 8, 2018 · 18 comments
Labels
fs Issues and PRs related to the fs subsystem / file system. stream Issues and PRs related to the stream subsystem.

Comments

@ronag
Copy link
Member

ronag commented Oct 8, 2018

No description provided.

@ronag ronag changed the title fs: read/write streams doesn't emit close after error fs: read/write streams don't emit close after error Oct 8, 2018
@sagitsofan
Copy link
Contributor

Can you please provide a code to reproduce?

@ronag
Copy link
Member Author

ronag commented Oct 25, 2018

const fs = require('fs')

fs.createReadStream('this file does not exist')
  .on('error', err => {
    console.log('error', err)
  })
  .on('close', () => {
    // Will not be called
    console.log('close')
  })

@ronag
Copy link
Member Author

ronag commented Oct 25, 2018

ping @mcollina

@mcollina mcollina added fs Issues and PRs related to the fs subsystem / file system. stream Issues and PRs related to the stream subsystem. labels Oct 26, 2018
@mcollina
Copy link
Member

This is definitely soemthing we would like to do.

cc @mafintosh

@Drieger
Copy link
Contributor

Drieger commented Oct 29, 2018

I would like to try to fix this but I need some guidance. Is it just a matter of emitting a close event altogether with the error one? Or should some additional action be taken?

I was thinking that if a file doesn't exist, close event shouldn't be emitted once file descriptor couldn't even be opened.

@ronag
Copy link
Member Author

ronag commented Oct 29, 2018

I was thinking that if a file doesn't exist, close event shouldn't be emitted once file descriptor couldn't even be opened.

You would think that. However, the stream API (as far as I understand it) needs to emit close regardless. You can think of close as destroyed. It's just an unfortunate coincidence that files also have the concept of "open".

@ronag
Copy link
Member Author

ronag commented Oct 29, 2018

Is it just a matter of emitting a close event altogether with the error one?

I believe so.

@mcollina
Copy link
Member

The solution to this should be based on #22795 (autoDestroy flag for streams).

@Drieger
Copy link
Contributor

Drieger commented Oct 30, 2018

Ok, I'll take a look at this

@antsmartian
Copy link
Contributor

antsmartian commented Nov 4, 2018

@mcollina If the solution is to be based on autoDestroy then by default (when autoDestory is false) on error, close won't be called? Or you meant we need to wrap the logic of emitting the close event over here: https://github.com/nodejs/node/blob/master/lib/internal/streams/destroy.js and then incorporating the destory logic in file open methods?

@mcollina
Copy link
Member

mcollina commented Nov 9, 2018

If autoDestroy: false  'close'  is not emitted. However by flipping autoDestroy: true  when creating the stream, then it will be emitted automatically, in the right order.

@antsmartian
Copy link
Contributor

antsmartian commented Nov 10, 2018

@mcollina If I understood it correctly, I did moved the code to use autoDestroy, can have a look at here. But this doesn't seems to be working (at least with the below example):

fs.createReadStream('./back12.js', {autoDestroy : true})
  .on('error', err => {
    console.log('error', err)
  })
  .on('close', () => {
    console.log('close') // doesn't get logged. 
})

I'm sure, I'm missing something here. Also, there is already a property called autoClose which is by default true which means on err, it's calling destroy.

@antsmartian
Copy link
Contributor

@mcollina Friendly remainder 🔉

@mcollina
Copy link
Member

@antsmartian can you please open full PR so I can do a proper review?

Some quick notes:

  1. a test is missing, even if it fails it should be there. Otherwise what are you verifying it against?
  2. autoDestroy: true should be passed to Readable at all times (but check autoClose)
  3. getting internal/streams/destroy  should not be needed at all. Why are you doing it?

@antsmartian
Copy link
Contributor

antsmartian commented Nov 20, 2018

@mcollina Sure, will raise a PR. I didn't write test cases, thats the reason I didn't raise PR. Will do, once I'm done with those changes.

With respect to:

getting internal/streams/destroy should not be needed at all. Why are you doing it?

I initially thought that, internal/streams/destroy should be used as it has the mechanism to destroy the stream when autoDestroy is set to true.

@Trott
Copy link
Member

Trott commented Nov 24, 2018

Sure, will raise a PR. I didn't write test cases, thats the reason I didn't raise PR. Will do, once I'm done with those changes.

If you wish, you can open the PR and put [WIP] (for "work in progress") in the PR title.

@antsmartian
Copy link
Contributor

antsmartian commented Nov 26, 2018

@Trott Yes, but looks like my initial understanding is wrong. So I need to pick up from the above comment provided. Will try to see if I can pull out things. But other's are free to open up a PR, in the meantime :)

@ronag
Copy link
Member Author

ronag commented Apr 19, 2020

I believe this has been resolved.

@ronag ronag closed this as completed Apr 19, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
fs Issues and PRs related to the fs subsystem / file system. stream Issues and PRs related to the stream subsystem.
Projects
None yet
Development

No branches or pull requests

6 participants