Skip to content

Commit 577018f

Browse files
committed
CDRIVER-2615 prohibit session and w=0 bulk write
1 parent a0e7275 commit 577018f

File tree

2 files changed

+49
-10
lines changed

2 files changed

+49
-10
lines changed

src/libmongoc/src/mongoc/mongoc-write-command.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -887,6 +887,14 @@ _mongoc_write_command_execute (
887887
}
888888

889889
if (server_stream->sd->max_wire_version >= WIRE_VERSION_OP_MSG) {
890+
if (cs && !mongoc_write_concern_is_acknowledged (write_concern)) {
891+
bson_set_error (
892+
&result->error,
893+
MONGOC_ERROR_COMMAND,
894+
MONGOC_ERROR_COMMAND_INVALID_ARG,
895+
"Cannot use client session with unacknowledged writes");
896+
EXIT;
897+
}
890898
_mongoc_write_opmsg (command,
891899
client,
892900
server_stream,

src/libmongoc/tests/test-mongoc-client-session.c

Lines changed: 41 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1268,35 +1268,41 @@ session_test_destroy (session_test_t *test)
12681268

12691269

12701270
static void
1271-
check_success_no_commands (session_test_t *test)
1271+
check_sessions_from_same_client_enforced (session_test_t *test)
12721272
{
12731273
if (test->session_client != test->client) {
12741274
BSON_ASSERT (!test->succeeded);
12751275
ASSERT_ERROR_CONTAINS (test->error,
12761276
MONGOC_ERROR_COMMAND,
12771277
MONGOC_ERROR_COMMAND_INVALID_ARG,
12781278
"Invalid sessionId");
1279-
1280-
return;
12811279
}
1280+
}
1281+
12821282

1283+
static void
1284+
check_sessions_with_w0_prohibited (session_test_t *test)
1285+
{
12831286
if (test->expect_explicit_lsid && !test->acknowledged) {
12841287
BSON_ASSERT (!test->succeeded);
12851288
ASSERT_ERROR_CONTAINS (test->error,
12861289
MONGOC_ERROR_COMMAND,
12871290
MONGOC_ERROR_COMMAND_INVALID_ARG,
12881291
"session with unacknowledged");
1289-
return;
12901292
}
1291-
1292-
ASSERT_OR_PRINT (test->succeeded, test->error);
12931293
}
12941294

12951295

12961296
static void
12971297
check_success (session_test_t *test)
12981298
{
1299-
check_success_no_commands (test);
1299+
check_sessions_from_same_client_enforced (test);
1300+
check_sessions_with_w0_prohibited (test);
1301+
1302+
if (test->session_client == test->client &&
1303+
test->expect_explicit_lsid == test->acknowledged) {
1304+
ASSERT_OR_PRINT (test->succeeded, test->error);
1305+
}
13001306

13011307
if (test->succeeded) {
13021308
ASSERT_CMPINT (test->n_started, >, 0);
@@ -2015,22 +2021,23 @@ test_bulk (session_test_t *test)
20152021

20162022
test->succeeded = mongoc_bulk_operation_insert_with_opts (
20172023
bulk, tmp_bson ("{}"), NULL, &test->error);
2018-
check_success_no_commands (test);
2024+
check_sessions_from_same_client_enforced (test);
20192025

20202026
test->succeeded = mongoc_bulk_operation_update_one_with_opts (
20212027
bulk,
20222028
tmp_bson ("{}"),
20232029
tmp_bson ("{'$set': {'x': 1}}"),
20242030
NULL,
20252031
&test->error);
2026-
check_success_no_commands (test);
2032+
check_sessions_from_same_client_enforced (test);
20272033

20282034
test->succeeded = mongoc_bulk_operation_remove_one_with_opts (
20292035
bulk, tmp_bson ("{}"), NULL, &test->error);
2030-
check_success_no_commands (test);
2036+
check_sessions_from_same_client_enforced (test);
20312037

20322038
i = mongoc_bulk_operation_execute (bulk, NULL, &test->error);
20332039
test->succeeded = (i != 0);
2040+
check_sessions_with_w0_prohibited (test);
20342041

20352042
mongoc_bulk_operation_destroy (bulk);
20362043
}
@@ -2621,6 +2628,30 @@ test_session_install (TestSuite *suite)
26212628
test_insert_one,
26222629
false,
26232630
false);
2631+
add_unacknowledged_test (
2632+
suite,
2633+
"/Session/unacknowledged/bulk/explicit_cs/inherit_wc",
2634+
test_bulk,
2635+
true,
2636+
true);
2637+
add_unacknowledged_test (
2638+
suite,
2639+
"/Session/unacknowledged/bulk/explicit_cs/explicit_wc",
2640+
test_bulk,
2641+
true,
2642+
false);
2643+
add_unacknowledged_test (
2644+
suite,
2645+
"/Session/unacknowledged/bulk/implicit_cs/inherit_wc",
2646+
test_bulk,
2647+
false,
2648+
true);
2649+
add_unacknowledged_test (
2650+
suite,
2651+
"/Session/unacknowledged/bulk/implicit_cs/explicit_wc",
2652+
test_bulk,
2653+
false,
2654+
false);
26242655
/* find_and_modify_with_opts only inherits acknowledged write concerns, so
26252656
* skip tests that inherit a write concern. Technically, an explicit
26262657
* unacknowledged write concern doesn't make much sense with findAndModify,

0 commit comments

Comments
 (0)