Skip to content

Commit 9428ea2

Browse files
committed
CDRIVER-2609 fix empty error after ismaster fails
1 parent 105993d commit 9428ea2

File tree

2 files changed

+40
-10
lines changed

2 files changed

+40
-10
lines changed

src/mongoc/mongoc-cluster.c

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -625,12 +625,12 @@ static mongoc_server_description_t *
625625
_mongoc_stream_run_ismaster (mongoc_cluster_t *cluster,
626626
mongoc_stream_t *stream,
627627
const char *address,
628-
uint32_t server_id)
628+
uint32_t server_id,
629+
bson_error_t *error)
629630
{
630631
const bson_t *command;
631632
mongoc_cmd_parts_t parts;
632633
bson_t reply;
633-
bson_error_t error;
634634
int64_t start;
635635
int64_t rtt_msec;
636636
mongoc_server_description_t *sd;
@@ -647,7 +647,7 @@ _mongoc_stream_run_ismaster (mongoc_cluster_t *cluster,
647647

648648
start = bson_get_monotonic_time ();
649649
server_stream = _mongoc_cluster_create_server_stream (
650-
cluster->client->topology, server_id, stream, &error);
650+
cluster->client->topology, server_id, stream, error);
651651
if (!server_stream) {
652652
RETURN (NULL);
653653
}
@@ -656,7 +656,8 @@ _mongoc_stream_run_ismaster (mongoc_cluster_t *cluster,
656656
&parts, cluster->client, "admin", MONGOC_QUERY_SLAVE_OK, command);
657657
parts.prohibit_lsid = true;
658658
if (!mongoc_cluster_run_command_parts (
659-
cluster, server_stream, &parts, &reply, &error)) {
659+
cluster, server_stream, &parts, &reply, error)) {
660+
bson_destroy (&reply);
660661
mongoc_server_stream_cleanup (server_stream);
661662
RETURN (NULL);
662663
}
@@ -668,7 +669,7 @@ _mongoc_stream_run_ismaster (mongoc_cluster_t *cluster,
668669

669670
mongoc_server_description_init (sd, address, server_id);
670671
/* send the error from run_command IN to handle_ismaster */
671-
mongoc_server_description_handle_ismaster (sd, &reply, rtt_msec, &error);
672+
mongoc_server_description_handle_ismaster (sd, &reply, rtt_msec, error);
672673

673674
bson_destroy (&reply);
674675

@@ -719,7 +720,7 @@ _mongoc_cluster_run_ismaster (mongoc_cluster_t *cluster,
719720
BSON_ASSERT (node->stream);
720721

721722
sd = _mongoc_stream_run_ismaster (
722-
cluster, node->stream, node->connection_address, server_id);
723+
cluster, node->stream, node->connection_address, server_id, error);
723724

724725
if (!sd) {
725726
return NULL;
@@ -1742,7 +1743,7 @@ mongoc_cluster_fetch_stream_single (mongoc_cluster_t *cluster,
17421743
#endif
17431744

17441745
sd = _mongoc_stream_run_ismaster (
1745-
cluster, stream, scanner_node->host.host_and_port, server_id);
1746+
cluster, stream, scanner_node->host.host_and_port, server_id, error);
17461747

17471748
if (!sd) {
17481749
return NULL;

tests/test-mongoc-cluster.c

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1306,7 +1306,7 @@ dollar_query_test_t tests[] = {
13061306
{NULL}};
13071307

13081308
static void
1309-
test_cluster_ismaster_fails (void)
1309+
_test_cluster_ismaster_fails (bool hangup)
13101310
{
13111311
mock_server_t *mock_server;
13121312
mongoc_uri_t *uri;
@@ -1326,6 +1326,7 @@ test_cluster_ismaster_fails (void)
13261326
/* increase heartbeatFrequencyMS to prevent background server selection. */
13271327
mongoc_uri_set_option_as_int32 (uri, "heartbeatFrequencyMS", 99999);
13281328
pool = mongoc_client_pool_new (uri);
1329+
mongoc_client_pool_set_error_api (pool, 2);
13291330
mongoc_uri_destroy (uri);
13301331
client = mongoc_client_pool_pop (pool);
13311332
/* do server selection to add this server to the topology. this does not add
@@ -1343,15 +1344,41 @@ test_cluster_ismaster_fails (void)
13431344
/* CDRIVER-2576: the server replies with an error, so
13441345
* _mongoc_stream_run_ismaster returns NULL, which
13451346
* _mongoc_cluster_run_ismaster must check. */
1346-
mock_server_replies_simple (request, "{'ok': 0}");
1347+
1348+
if (hangup) {
1349+
capture_logs (true); /* suppress "failed to buffer" warning */
1350+
mock_server_hangs_up (request);
1351+
BSON_ASSERT (!future_get_bool (future));
1352+
ASSERT_ERROR_CONTAINS (
1353+
error, MONGOC_ERROR_STREAM, MONGOC_ERROR_STREAM_SOCKET, "socket err");
1354+
} else {
1355+
mock_server_replies_simple (request, "{'ok': 0, 'code': 123}");
1356+
BSON_ASSERT (!future_get_bool (future));
1357+
ASSERT_ERROR_CONTAINS (
1358+
error, MONGOC_ERROR_SERVER, 123, "Unknown command error");
1359+
}
1360+
13471361
request_destroy (request);
1348-
future_wait (future);
13491362
future_destroy (future);
13501363
mock_server_destroy (mock_server);
13511364
mongoc_client_pool_push (pool, client);
13521365
mongoc_client_pool_destroy (pool);
13531366
}
13541367

1368+
static void
1369+
test_cluster_ismaster_fails (void)
1370+
{
1371+
_test_cluster_ismaster_fails (false);
1372+
}
1373+
1374+
1375+
static void
1376+
test_cluster_ismaster_hangup (void)
1377+
{
1378+
_test_cluster_ismaster_fails (true);
1379+
}
1380+
1381+
13551382
void
13561383
test_cluster_install (TestSuite *suite)
13571384
{
@@ -1474,4 +1501,6 @@ test_cluster_install (TestSuite *suite)
14741501
test_framework_skip_if_slow);
14751502
TestSuite_AddMockServerTest (
14761503
suite, "/Cluster/ismaster_fails", test_cluster_ismaster_fails);
1504+
TestSuite_AddMockServerTest (
1505+
suite, "/Cluster/ismaster_hangup", test_cluster_ismaster_hangup);
14771506
}

0 commit comments

Comments
 (0)