Skip to content

RUST-945 Check that explicit sessions were created on the correct client #405

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

Merged
merged 1 commit into from
Aug 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/client/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,15 @@ impl Client {
}
match session.into() {
Some(session) => {
if !Arc::ptr_eq(&self.inner, &session.client().inner) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't have PartialEq derived for Client (and it wouldn't make much sense to do so), so this verifies that the inner fields on both the clients point to the same data.

return Err(ErrorKind::InvalidArgument {
message: "the session provided to an operation must be created from the \
same client as the collection/database"
.into(),
}
.into());
}

if let Some(SelectionCriteria::ReadPreference(read_preference)) =
op.selection_criteria()
{
Expand Down
3 changes: 1 addition & 2 deletions src/selection_criteria.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use std::convert::TryInto;
use std::{collections::HashMap, sync::Arc, time::Duration};
use std::{collections::HashMap, convert::TryInto, sync::Arc, time::Duration};

use derivative::Derivative;
use serde::{de::Error as SerdeError, Deserialize, Deserializer};
Expand Down
43 changes: 41 additions & 2 deletions src/test/spec/sessions.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
use tokio::sync::RwLockWriteGuard;
use tokio::sync::{RwLockReadGuard, RwLockWriteGuard};

use crate::test::{run_spec_test, LOCK};
use crate::{
bson::doc,
error::ErrorKind,
test::{run_spec_test, TestClient, LOCK},
};

use super::{run_unified_format_test, run_v2_test};

Expand All @@ -17,3 +21,38 @@ async fn run_legacy() {
let _guard: RwLockWriteGuard<()> = LOCK.run_exclusively().await;
run_spec_test(&["sessions", "legacy"], run_v2_test).await;
}

#[cfg_attr(feature = "tokio-runtime", tokio::test(flavor = "multi_thread"))]
#[cfg_attr(feature = "async-std-runtime", async_std::test)]
#[function_name::named]
async fn explicit_session_created_on_same_client() {
let _guard: RwLockReadGuard<_> = LOCK.run_concurrently().await;

let client0 = TestClient::new().await;
let client1 = TestClient::new().await;

let mut session0 = client0.start_session(None).await.unwrap();
let mut session1 = client1.start_session(None).await.unwrap();

let db = client0.database(function_name!());
let err = db
.list_collections_with_session(None, None, &mut session1)
.await
.unwrap_err();
match *err.kind {
ErrorKind::InvalidArgument { message } => assert!(message.contains("session provided")),
other => panic!("expected InvalidArgument error, got {:?}", other),
}

let coll = client1
.database(function_name!())
.collection(function_name!());
let err = coll
.insert_one_with_session(doc! {}, None, &mut session0)
.await
.unwrap_err();
match *err.kind {
ErrorKind::InvalidArgument { message } => assert!(message.contains("session provided")),
other => panic!("expected InvalidArgument error, got {:?}", other),
}
}