镜像站点 · 本页由第三方 GitHub 只读镜像提供,非 GitHub 官方站点,不接受任何登录或凭据输入。前往 github.com
Skip to content

Event 'close' emits twice (fs.createWriteStream) #31366

Closed

Description

@ledeneveugene

if i set (emitClose: false) then on version 12.14.1 event emits once.
Documentation: By default, the stream will not emit a 'close' event after it has been destroyed. This is the opposite of the default for other Writable streams. Set the emitClose option to true to change this behavior.

const fileWriteStream = fs.createWriteStream(saveTo, {emitClose: true});
file.pipe(fileWriteStream);
fileWriteStream.on('close', function () {
console.log('fileWriteStream.on(close')
}

Activity

  1. himself65 commented on Jan 15, 2020

    @himself65
    Member
    const fs = require('fs')
    const fileReadStream = fs.createReadStream('1.in')
    const fileWriteStream = fs.createWriteStream('1.out', {
      emitClose: true
    })
    
    fileReadStream.pipe(fileWriteStream)
    fileWriteStream.on('close', function () {
      console.log('fileWriteStream.on(close)')
    })
    fileWriteStream.on(close)
    fileWriteStream.on(close)

    with node v13.6.0

  2. himself65 commented on Jan 15, 2020

    @himself65
    Member

    more examples:

    const fs = require('fs')
    const fileWriteStream = fs.createWriteStream('1.out', {
      emitClose: true
    })
    fileWriteStream.on('close', () => {
      console.log('called')
    })
    
    fileWriteStream.write('1')
    fileWriteStream.write('2')
    
    fileWriteStream.end()
  3. himself65 commented on Jan 15, 2020

    @himself65
    Member
    const fs = require('fs')
    const fileWriteStream = fs.createWriteStream('1.out', {
      emitClose: true
    })
    fileWriteStream.on('close', () => {
      console.log('called')
    })
    
    fileWriteStream.write('1')
    fileWriteStream.write('2')
    
    fileWriteStream.destroy()
    events.js:298
          throw er; // Unhandled 'error' event
          ^
    
    Error [ERR_STREAM_DESTROYED]: Cannot call write after a stream was destroyed
    ?[90m    at WriteStream._write (internal/fs/streams.js:415:33)?[39m
    ?[90m    at WriteStream.<anonymous> (internal/fs/streams.js:411:12)?[39m
    ?[90m    at Object.onceWrapper (events.js:428:26)?[39m
    ?[90m    at WriteStream.emit (events.js:333:22)?[39m
    ?[90m    at internal/fs/streams.js:402:12?[39m
    ?[90m    at FSReqCallback.oncomplete (fs.js:154:23)?[39m
    Emitted 'error' event on WriteStream instance at:
    ?[90m    at errorOrDestroy (internal/streams/destroy.js:128:12)?[39m
    ?[90m    at onwriteError (_stream_writable.js:462:3)?[39m
    ?[90m    at onwrite (_stream_writable.js:483:7)?[39m
    ?[90m    at WriteStream._write (internal/fs/streams.js:415:30)?[39m
    ?[90m    at WriteStream.<anonymous> (internal/fs/streams.js:411:12)?[39m
        [... lines matching original stack trace ...]
    ?[90m    at FSReqCallback.oncomplete (fs.js:154:23)?[39m {
      code: ?[32m'ERR_STREAM_DESTROYED'?[39m
    }
  4. himself65 commented on Jan 15, 2020

    @himself65
    Member

    I think these problems have fixed on 67ed526.

    And that commit hasn't published to the node.js version

    related PR: #30851

  5. ledeneveugene commented on Jan 16, 2020

    @ledeneveugene
    Author

    I think these problems have fixed on [67ed526]

    Should i close issue or developers do it?

  6. ronag commented on Jan 16, 2020

    @ronag
    Member
    const fileWriteStream = fs.createWriteStream('1.out', {
      emitClose: true
    })

    Don't use emitClose: true here. It will emit 'close' anyway. This is an internal option that is only intended for stream implementations.

    Preferrably it shouldn't even be forwarded.

  7. ledeneveugene commented on Jan 17, 2020

    @ledeneveugene
    Author

    Don't use emitClose: true here. It will emit 'close' anyway. This is an internal option that is only intended for stream implementations.
    Preferrably it shouldn't even be forwarded.

    I tried without emitClose: true on node versions 10.16.3 and 12.14.1. You are rigth. Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions