Description
Versions/Environment
- What version of Rust are you using?
1.64.0-nightly (c0bbd42ce 2022-07-03) - What operating system are you using?
Debian Bullseye in Docker container - What versions of the driver and its dependencies are you using? (Run
cargo pkgid mongodb
&cargo pkgid bson
)
2.3.0 and 2.3.0 - What version of MongoDB are you using? (Check with the MongoDB shell using
db.version()
)
4.4.15 - What is your MongoDB topology (standalone, replica set, sharded cluster, serverless)?
Sharded cluster
Describe the bug
Cursors are not killed when they are dropped if the tokio runtime is in the process of shutting down (for example, if tokio::runtime::Runtime::shutdown_timeout
or the tokio::runtime::Runtime
drop implementation is executing). Shutting down the runtime causes all unfinished futures to be dropped, which in turns causes any cursors existing in the corresponding tasks to be dropped. However, the cursors are not killed, presumably because the code that kills cursors attempts to execute a new task to do so, and tokio is unwilling to comply because it's trying to shutdown.
This is particularly a problem when you have a large number of cursors open, as it causes them to persist and take up mongo cluster resources. If your process is being restarted frequently, a very large number of open cursors can accumulate in a short period of time.
I should note that I've only tested this issue with cursors created by change streams.
(I'm also seeing these cursors persist for many hours, instead of being killed on the MongoDB side after 10 minutes of inactivity, but this may not be an issue specific to this driver.)
You can work around this problem by ensuring your cursors are dropped before shutting down the runtime. However, many users are unlikely to realize this needs to be done, so the issue would remain a continual hazard if not resolved. Also, ensuring the cursors are dropped before shutting down the runtime may be a significant challenge in some applications.
I'm unsure if a solution is possible, but it sure would be nice if it were. Thanks. 🙂