-
Type: Bug
-
Resolution: Fixed
-
Priority: Major - P3
-
Affects Version/s: 3.2.7
-
Component/s: None
-
None
-
Empty show more show less
In `change_stream.js`, we remove all listeners before closing the cursor here: https://github.com/mongodb/node-mongodb-native/blob/next/lib/change_stream.js#L183
This prevents us from properly using a `close` listener. We can use the following script to see this behavior:
let stream; async function main() { const uri = "mongodb://127.0.0.1:26000/"; const client = new MongoClient(uri, { useNewUrlParser: true }); try { await client.connect() } catch (err) { console.error("failed to connect to MongoDB") } stream = client.watch([], { // let stream = client.db("realm").collection("blogs").watch(watchPipeline, { fullDocument: "updateLookup" }); stream.on("change", onStreamChangeEvent); stream.on("close", onStreamClose); stream.on("end", onStreamEnd); stream.on("error", onStreamError); } function onStreamChangeEvent(rawEvent: object) { console.log(rawEvent); }; function onStreamClose() { console.warn("MongoDB stream closed."); }; function onStreamEnd() { console.warn("MongoDB stream ended."); }; function onStreamError(err) { console.error("MongoDB stream encountered error:"); console.error(err.stack); stream.close(); }; main(); console.log("Now watching events.")
To fix this, we can use something along the lines of the following:
const cursor = this.cursor; return cursor.close(err => { ['data', 'close', 'end', 'error'].forEach(event => cursor.removeAllListeners(event)); delete this.cursor; if (callback) return callback(err); return this.promiseLibrary.resolve(); });