Skip to content

Commit 015d6f6

Browse files
mlokrisabelatkinson
andcommitted
RUST-1719 Fix panic after SessionCursor::with_type is called (mongodb#928)
Co-authored-by: Isabel Atkinson <isabel.atkinson@mongodb.com>
1 parent 662248b commit 015d6f6

File tree

2 files changed

+33
-9
lines changed

2 files changed

+33
-9
lines changed

src/cursor/session.rs

+3-9
Original file line numberDiff line numberDiff line change
@@ -309,17 +309,15 @@ impl<T> SessionCursor<T> {
309309
where
310310
D: Deserialize<'a>,
311311
{
312-
let out = SessionCursor {
312+
SessionCursor {
313313
client: self.client.clone(),
314314
info: self.info.clone(),
315315
state: Some(self.take_state()),
316316
drop_address: self.drop_address.take(),
317317
_phantom: Default::default(),
318318
#[cfg(test)]
319319
kill_watcher: self.kill_watcher.take(),
320-
};
321-
self.mark_exhausted(); // prevent a `kill_cursor` call in `drop`
322-
out
320+
}
323321
}
324322

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

348346
impl<T> SessionCursor<T> {
349-
fn mark_exhausted(&mut self) {
350-
self.state.as_mut().unwrap().exhausted = true;
351-
}
352-
353347
pub(crate) fn is_exhausted(&self) -> bool {
354-
self.state.as_ref().unwrap().exhausted
348+
self.state.as_ref().map_or(true, |state| state.exhausted)
355349
}
356350

357351
#[cfg(test)]

src/test/cursor.rs

+30
Original file line numberDiff line numberDiff line change
@@ -235,3 +235,33 @@ async fn borrowed_deserialization() {
235235
i += 1;
236236
}
237237
}
238+
239+
#[cfg_attr(feature = "tokio-runtime", tokio::test)]
240+
#[cfg_attr(feature = "async-std-runtime", async_std::test)]
241+
async fn session_cursor_with_type() {
242+
let _guard: RwLockReadGuard<()> = LOCK.run_concurrently().await;
243+
let client = TestClient::new().await;
244+
245+
let mut session = client.start_session(None).await.unwrap();
246+
let coll = client.database("db").collection("coll");
247+
coll.drop_with_session(None, &mut session).await.unwrap();
248+
249+
coll.insert_many_with_session(
250+
vec![doc! { "x": 1 }, doc! { "x": 2 }, doc! { "x": 3 }],
251+
None,
252+
&mut session,
253+
)
254+
.await
255+
.unwrap();
256+
257+
let mut cursor: crate::SessionCursor<bson::Document> = coll
258+
.find_with_session(doc! {}, None, &mut session)
259+
.await
260+
.unwrap();
261+
262+
let _ = cursor.next(&mut session).await.unwrap().unwrap();
263+
264+
let mut cursor_with_type: crate::SessionCursor<bson::RawDocumentBuf> = cursor.with_type();
265+
266+
let _ = cursor_with_type.next(&mut session).await.unwrap().unwrap();
267+
}

0 commit comments

Comments
 (0)