-
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Description
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
- Set up a MongoDB change stream using Mongoose.
- Intentionally disconnect from the database to close the change stream.
- Wait for the MongoDB to try to reconnect the change stream
- 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.