You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Asynchronous dir.close() throws immediately, if already closed #36237
Calling asynchronous version of dir.close() with dir already closed, e.g. by it's async iterator.
const fs = require('fs')
const dir = fs.opendirSync('.') // Using async function in this example just for brevity.
dir.closeSync() // In my actual code, here is an async loop over dir.entries() which may terminate in the middle.
try {
dir.close((error) => {
if(!error) return
// We never get ERR_DIR_CLOSED here...
})
} catch (error) {
// ... but here instead (but we should see only ERR_INVALID_CALLBACK here).
}
How often does it reproduce? Is there a required condition?
Always, when dir.close() is called redundantly.
What is the expected behavior?
Asynchronous functions should forward errors via callback, unless the provided callback argument itself is invalid.
What do you see instead?
Error is thrown immediately (synchronously), This is especially embarrassing with promises API:
try {
dir.close().then(() => console.log('closed the dir just now')
).catch(error => console.log('something wrong', error))
} catch (error) {
if(error.code === 'ERR_DIR_CLOSED') console.log('Oh no... again!')
else console.log('a real surprise!', e)
}
Additional information
If this behaviour is intentional, it should be documented; otherwise I would gladly provide a fix.
Activity
aduh95 commentedon Nov 23, 2020
That's a bug, it should pass the error to the callback. PR is welcome :)
Lxxyx commentedon Nov 24, 2020
I'm working on this issue.
valango commentedon Nov 25, 2020
You guys were quick - I opened this issue at 2am local time and it was solved by the noon, next day. :)
Thank you!
fs: pass ERR_DIR_CLOSED asynchronously to dir.close
fs: pass ERR_DIR_CLOSED asynchronously to dir.close
fs: pass ERR_DIR_CLOSED asynchronously to dir.close