-
Type: Improvement
-
Resolution: Done
-
Priority: Major - P3
-
None
-
Affects Version/s: None
-
Component/s: None
In light of handling CTRL-C in the shell we observed that using `MongoClient.close(...)` does not close all connections that were opened by the client if a connection is still running an operation.
Take the following script as an example:
const MongoClient = require('mongodb').MongoClient; (async() => { console.log('Connecting...'); const client = await MongoClient.connect('mongodb://localhost:30001'); const db = client.db('test'); db.collection('ctrlc').find({ $where: 'while(true) {}' }).toArray().then(r => console.log(r)); setTimeout(async () => { await client.close(true); console.log('Closed...'); setInterval(() => { console.log('alive', new Date().toISOString()); }, 10_000); }, 1000); })().catch(e => process.nextTick(() => { console.log(e); process.exit(1); }));
The find operation will be running in the background and continues even after calling client.close(...) (which does not throw an error). The socket is still kept open until I terminate the process.
(NOTE: the example above only works as designed when there is a collection called ctrlc with at least one document)
This seems to be a regression in 4.x.
- related to
-
NODE-3263 Calling close() does not terminate all connections
- Backlog