Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

end is called twice when using pipeline #42866

Closed
climba03003 opened this issue Apr 25, 2022 · 7 comments · Fixed by #46226
Closed

end is called twice when using pipeline #42866

climba03003 opened this issue Apr 25, 2022 · 7 comments · Fixed by #46226
Labels
help wanted Issues that need assistance from volunteers or PRs that need help to proceed. stream Issues and PRs related to the stream subsystem.

Comments

@climba03003
Copy link
Contributor

climba03003 commented Apr 25, 2022

Version

>= 17.3.0

Platform

Ubuntu 20.04

Subsystem

stream

What steps will reproduce the bug?

This behavior started since 17.3.0 and it can also be reproduce on 18.0.0

import { Readable, Writable } from "stream";
import { pipeline } from "stream/promises";

class CustomReadable extends Readable {
  constructor(name) {
    super();
    this.name = name;
  }

  _read() {
    console.log(`${this.name}: write hello world`);
    this.push("hello world");
    console.log(`${this.name}: end readable`);
    this.push(null);
  }
}

class CustomWritable extends Writable {
  constructor(name) {
    super();
    this.name = name;
  }

  _write() {
    console.log(`${this.name}: fire write`);
  }

  end() {
    console.log(`${this.name}: fire end`);
  }
}

// comment either one to see the actual flow
// fire end once when using pipe
new CustomReadable("pipe").pipe(new CustomWritable("pipe"));
// fire end twice when using pipeline
await pipeline(new CustomReadable("pipeline"), new CustomWritable("pipeline"));

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

Always.

What is the expected behavior?

.end should not call twice and behave similar to pipe.

// pipeline
pipeline: write hello world
pipeline: end readable
pipeline: fire write
pipeline: fire end

// pipe
pipe: write hello world
pipe: end readable
pipe: fire write
pipe: fire end

What do you see instead?

.end being called twice when using pipeline.

// pipeline
pipeline: write hello world
pipeline: end readable
pipeline: fire write
pipeline: fire end
pipeline: fire end

// pipe
pipe: write hello world
pipe: end readable
pipe: fire write
pipe: fire end

Additional information

It is currently affecting mongodb usage in node@18. I assume it is bug from core but not their side.
If it is the expected behavior, I will try to fix it on mongodb side.

import { pipeline } from 'stream/promises'
import fs from 'fs'

const { MongoClient, GridFSBucket } = require('mongodb');

const client = await MongoClient.connect(`mongodb://localhost:27017`);

const database = client.db('test01');
const bucket = new GridFSBucket(database, { bucketName: 'test01' });

const readable = fs.createReadStream('/tmp/test.txt');
const writable = bucket.openUploadStream('test.txt')
await pipeline(readable, writable)
@targos
Copy link
Member

targos commented Apr 25, 2022

@nodejs/streams

@targos targos added the stream Issues and PRs related to the stream subsystem. label Apr 25, 2022
@ronag
Copy link
Member

ronag commented Apr 25, 2022

It's unnecessary that pipeline calls it twice but I wouldn't consider it a bug. Calling end the second time should be a noop.

@mscdex
Copy link
Contributor

mscdex commented Apr 25, 2022

It's probably easier to just implement _final() instead unless there is a strong reason to be overriding end()?

@mcollina
Copy link
Member

  1. multiple calls to end() must be idempotent, so this is a bug in whoever is overriding end.
  2. pipeline should not call end more than once, but there

@climba03003
Copy link
Contributor Author

climba03003 commented Apr 26, 2022

It's probably easier to just implement _final() instead unless there is a strong reason to be overriding end()?

This issue is extracted from mongodb and I do not know the reason behind to override both .write() and .end().
But, it is actually affecting a lot of people when they switch to node@18.

I already send this issue to their issue tracket. And ask them about the reason behind and will they accept change to use ._write and ._final.

@ronag ronag added the help wanted Issues that need assistance from volunteers or PRs that need help to proceed. label Nov 4, 2022
@ronag
Copy link
Member

ronag commented Nov 4, 2022

Making sure pipeline only calls end once would be a nice to have fix.

@debadree25
Copy link
Member

Hello @ronag any pointers on where the end function is called during the pipeline function, have been trying to find a way to solve this issue

debadree25 added a commit to debadree25/node that referenced this issue Jan 16, 2023
nodejs-github-bot pushed a commit that referenced this issue Jan 19, 2023
Fixes: #42866
PR-URL: #46226
Reviewed-By: Luigi Pinca <[email protected]>
Reviewed-By: Robert Nagy <[email protected]>
Reviewed-By: Matteo Collina <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Minwoo Jung <[email protected]>
RafaelGSS pushed a commit that referenced this issue Jan 20, 2023
Fixes: #42866
PR-URL: #46226
Reviewed-By: Luigi Pinca <[email protected]>
Reviewed-By: Robert Nagy <[email protected]>
Reviewed-By: Matteo Collina <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Minwoo Jung <[email protected]>
juanarbol pushed a commit that referenced this issue Jan 26, 2023
Fixes: #42866
PR-URL: #46226
Reviewed-By: Luigi Pinca <[email protected]>
Reviewed-By: Robert Nagy <[email protected]>
Reviewed-By: Matteo Collina <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Minwoo Jung <[email protected]>
juanarbol pushed a commit that referenced this issue Jan 31, 2023
Fixes: #42866
PR-URL: #46226
Reviewed-By: Luigi Pinca <[email protected]>
Reviewed-By: Robert Nagy <[email protected]>
Reviewed-By: Matteo Collina <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Minwoo Jung <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Issues that need assistance from volunteers or PRs that need help to proceed. stream Issues and PRs related to the stream subsystem.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants