-
Notifications
You must be signed in to change notification settings - Fork 168
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
RUST-1439 Cursors not killed on drop when runtime is in the process of shutting down #719
Comments
Hi @tgnottingham, thank you for reaching out! We agree this is a problematic situation and we're currently discussing as a team what options we might have to alleviate this; I'll post an update here on that soon. In the meantime I wanted to ask for some clarification on this point:
Just to clarify, you're saying you see the following:
Regarding timeouts, in earlier versions of MongoDB there was a default cursor timeout of 10 minutes, though as of MongoDB 4.4.8 (SERVER-6036), the lifetime of a cursor is determined by the session used to create it. Any command you run with the Rust driver will use a session - either an "explicit" session, if you are creating and passing one in yourself when you call If you are seeing cursors from a shut-down application remain alive for longer than The driver does pool and re-use sessions while an application is running, however when the application restarts that pool would recreated from scratch, so sessions would not be reused across application runs and so I do not believe a cursor could be incorrectly kept alive that way even if the I'll also note that I discovered there is a spec requirement the Rust driver neglected to implement, that on client shutdown, the |
Hi! Quick update on the cleanup issue. We think we can improve this situation with a fix I describe in RUST-1439. To summarize, we will spawn a task for the purpose of cleanup at the time a cursor/change stream is created, and have that task wait on a message from a one-shot channel to proceed with cleanup. The |
Hi, thanks for the response!
I wouldn't say I've verified it exactly. 🙂 Just inferred it from Grafana and Prometheus metrics -- the query
That might work, but I do wonder if it's possible for the sending or receiving task to be terminated before they do what they need to do. This might not be what you had in mind, but I found that the following doesn't always print, for example: #[tokio::main]
async fn main() {
let _s = SendOnDrop::new();
}
struct SendOnDrop {
tx: Option<tokio::sync::oneshot::Sender<i32>>,
}
impl SendOnDrop {
fn new() -> SendOnDrop {
let (tx, rx) = tokio::sync::oneshot::channel();
tokio::spawn(async move {
println!("{}", rx.await.unwrap());
});
SendOnDrop {
tx: Some(tx),
}
}
}
impl Drop for SendOnDrop {
fn drop(&mut self) {
self.tx.take().unwrap().send(1).unwrap();
}
} Also tried a variation where Anyway, something to watch out for. 🙂 |
Versions/Environment
1.64.0-nightly (c0bbd42ce 2022-07-03)
Debian Bullseye in Docker container
cargo pkgid mongodb
&cargo pkgid bson
)2.3.0 and 2.3.0
db.version()
)4.4.15
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 thetokio::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. 🙂
The text was updated successfully, but these errors were encountered: