Skip to content

fs.createWriteStream does not fire close event #21122

Description

@spurreiter
  • Version: v10.3.0
  • Platform: Linux dev 4.15.0-22-generic Simple project messaging. #24-Ubuntu SMP Wed May 16 12:15:17 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux
  • Subsystem:

The following code illustrates the observed issue:

// index.js
const fs = require('fs')
const {resolve} = require('path')

function copy (source, target, cb) {
  const read = fs.createReadStream(source)
  const write = fs.createWriteStream(target, {flags: 'w'})

  function copyit () {
    if (copyit.once) return
    copyit.once = true
    console.log('copyit')
     
    write.on('error', function (err) {
      console.log('onerror')
      cb(err)
    })
    write.on('close', function () {
      console.log('onclose')
      cb(null)
    })
    read.pipe(write)
  }

  read.on('readable', copyit)
  read.on('end', copyit)
}

const source = resolve(__dirname, 'index.js')
const target = resolve(__dirname, 'copy.js')

copy(source, target, (err) => {
  console.log('end', err)
})

On node>=10.0 only the readable event fires

copyit

On node@9.11.1 the output is

copyit
onclose
end null

Activity

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

    fsIssues and PRs related to file-system APIs and the fs module.streamIssues and PRs related to Node.js streams.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions