Skip to content

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

@Klem3n

Description

@Klem3n

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    confirmed-bugWe've confirmed this is a bug in Mongoose and will fix it.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions