Skip to content

fs.createWriteStream does not fire close event #21122

Closed
@spurreiter

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    fsIssues and PRs related to the fs subsystem / file system.streamIssues and PRs related to the stream subsystem.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions