Skip to content

writableStream.destroy() closes the underlying file even when stream is created with {autoClose: false, emitClose: false} #49241

Open
@notorca

Description

@notorca

Version

v18.17.1, v20.5.1

Platform

Linux 6.4.10-200.fc38.x86_64 #1 SMP PREEMPT_DYNAMIC Fri Aug 11 12:20:29 UTC 2023 x86_64 GNU/Linux

Subsystem

fs, stream

What steps will reproduce the bug?

Minimal program to reproduce.

Create 2 writeStreams from a FileHandle with {autoClose: false, emitClose: false} options. Write some data to the first stream and destroy it. Try to write some data to the second stream.

import { open } from 'node:fs/promises'
import { pipeline } from 'node:stream/promises'

async function main() {
    const destFile = await open('test.tmp', 'w')
    destFile.truncate(1024)
    const stream0 = destFile.createWriteStream({start: 0, autoClose: false, emitClose: false})
    const stream1 = destFile.createWriteStream({start: 512, autoClose: false, emitClose: false})
    stream0.on('error', (err) => { console.log('stream0 error', err) })
    await pipeline("Hello world from stream0", stream0)
    stream0.destroy(new Error('destroyed'))
    try {
        await pipeline("Hello world from stream1", stream1) // emits ERR_STREAM_WRITE_AFTER_END
    } finally {
        // destFile.fd is -1 here
        destFile.close()
    }
}

await main()

How often does it reproduce? Is there a required condition?

No response

What is the expected behavior? Why is that the expected behavior?

Data is written, FileHandle is not closed up until close() call because emitClose: true is specified on stream creation.

What do you see instead?

ERR_STREAM_WRITE_AFTER_END is emmited when trying to write to the steream1. destFile.fd is set to -1.

Additional information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    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