Skip to content

Commit

Permalink
RUST-1719 Fix panic after SessionCursor::with_type is called (#928)
Browse files Browse the repository at this point in the history
Co-authored-by: Isabel Atkinson <isabel.atkinson@mongodb.com>
  • Loading branch information
mlokr and isabelatkinson committed Aug 15, 2023
1 parent 662248b commit 290595b
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 9 deletions.
12 changes: 3 additions & 9 deletions src/cursor/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -309,17 +309,15 @@ impl<T> SessionCursor<T> {
where
D: Deserialize<'a>,
{
let out = SessionCursor {
SessionCursor {
client: self.client.clone(),
info: self.info.clone(),
state: Some(self.take_state()),
drop_address: self.drop_address.take(),
_phantom: Default::default(),
#[cfg(test)]
kill_watcher: self.kill_watcher.take(),
};
self.mark_exhausted(); // prevent a `kill_cursor` call in `drop`
out
}
}

pub(crate) fn address(&self) -> &ServerAddress {
Expand All @@ -346,12 +344,8 @@ impl<T> SessionCursor<T> {
}

impl<T> SessionCursor<T> {
fn mark_exhausted(&mut self) {
self.state.as_mut().unwrap().exhausted = true;
}

pub(crate) fn is_exhausted(&self) -> bool {
self.state.as_ref().unwrap().exhausted
self.state.as_ref().map_or(true, |state| state.exhausted)
}

#[cfg(test)]
Expand Down
30 changes: 30 additions & 0 deletions src/test/cursor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,3 +235,33 @@ async fn borrowed_deserialization() {
i += 1;
}
}

#[cfg_attr(feature = "tokio-runtime", tokio::test)]
#[cfg_attr(feature = "async-std-runtime", async_std::test)]
async fn session_cursor_with_type() {
let _guard: RwLockReadGuard<()> = LOCK.run_concurrently().await;
let client = TestClient::new().await;

let mut session = client.start_session(None).await.unwrap();
let coll = client.database("db").collection("coll");
coll.drop_with_session(None, &mut session).await.unwrap();

coll.insert_many_with_session(
vec![doc! { "x": 1 }, doc! { "x": 2 }, doc! { "x": 3 }],
None,
&mut session,
)
.await
.unwrap();

let mut cursor: crate::SessionCursor<bson::Document> = coll
.find_with_session(doc! {}, None, &mut session)
.await
.unwrap();

let _ = cursor.next(&mut session).await.unwrap().unwrap();

let mut cursor_with_type: crate::SessionCursor<bson::RawDocumentBuf> = cursor.with_type();

let _ = cursor_with_type.next(&mut session).await.unwrap().unwrap();
}

0 comments on commit 290595b

Please sign in to comment.