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

Change stream: Mongoose suppresses error event after initial close #14177

Closed
2 tasks done
Klem3n opened this issue Dec 13, 2023 · 1 comment
Closed
2 tasks done

Change stream: Mongoose suppresses error event after initial close #14177

Klem3n opened this issue Dec 13, 2023 · 1 comment
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Milestone

Comments

@Klem3n
Copy link

Klem3n commented Dec 13, 2023

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the bug has not already been reported

Mongoose version

7.5.3

Node.js version

20.5.1

MongoDB server version

6.0

Typescript version (if applicable)

No response

Description

In Mongoose, a change in the error event handling for MongoDB change streams appears to have inadvertently affected the ability to catch errors after the connection has been closed. Specifically, the commit 386c366 introduced a change where the error event is suppressed after the change stream has been initially closed. This suppression prevents the propagation of errors that the MongoDB driver throws during reconnection attempts, as outlined in the MongoDB documentation for Change Streams.

The workaround is to subscribe directly on the driverChangeStream:

this.changeStream.driverChangeStream.on('error', (error) => {
      console.log('Driver change stream error');
      //this.reconnect();
});

Steps to Reproduce

  1. Set up a MongoDB change stream using Mongoose.
  2. Intentionally disconnect from the database to close the change stream.
  3. Wait for the MongoDB to try to reconnect the change stream
  4. Error thrown by MongoDB driver is not propagated to the change stream "error" event subscription.

Expected Behavior

The expected behavior is that Mongoose should emit error events from the MongoDB change stream even after the stream has been closed. This would enable applications to catch and handle these errors, especially to implement custom reconnection logic in scenarios where maintaining an active change stream is critical.

@vkarpov15 vkarpov15 added this to the 7.6.8 milestone Dec 23, 2023
@vkarpov15 vkarpov15 added the needs repro script Maybe a bug, but no repro script. The issue reporter should create a script that demos the issue label Dec 23, 2023
@Klem3n
Copy link
Author

Klem3n commented Dec 27, 2023

The reproduction script can be found here:

https://github.com/Klem3n/mongoose-error

@vkarpov15 vkarpov15 added confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. and removed needs repro script Maybe a bug, but no repro script. The issue reporter should create a script that demos the issue labels Dec 29, 2023
vkarpov15 added a commit that referenced this issue Dec 30, 2023
fix(ChangeStream): avoid suppressing errors in closed change stream
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Projects
None yet
Development

No branches or pull requests

2 participants