@@ -409,7 +409,7 @@ mongoc_cluster_run_command_internal (mongoc_cluster_t *cluster,
409
409
cluster -> iov .len ,
410
410
cluster -> sockettimeoutms ,
411
411
error )) {
412
- mongoc_cluster_disconnect_node (cluster , cmd -> server_id );
412
+ mongoc_cluster_disconnect_node (cluster , cmd -> server_id , true, error );
413
413
414
414
/* add info about the command to writev_full's error message */
415
415
_bson_error_message_printf (
@@ -427,11 +427,12 @@ mongoc_cluster_run_command_internal (mongoc_cluster_t *cluster,
427
427
reply_header_size ,
428
428
reply_header_size ,
429
429
cluster -> sockettimeoutms )) {
430
- mongoc_cluster_disconnect_node (cluster , cmd -> server_id );
431
430
RUN_CMD_ERR (MONGOC_ERROR_STREAM ,
432
431
MONGOC_ERROR_STREAM_SOCKET ,
433
432
"socket error or timeout" );
434
433
434
+ mongoc_cluster_disconnect_node (
435
+ cluster , cmd -> server_id , !mongoc_stream_timed_out (stream ), error );
435
436
GOTO (done );
436
437
}
437
438
@@ -1347,7 +1348,9 @@ _mongoc_cluster_auth_node (mongoc_cluster_t *cluster,
1347
1348
* mongoc_cluster_disconnect_node --
1348
1349
*
1349
1350
* Remove a node from the set of nodes. This should be done if
1350
- * a stream in the set is found to be invalid.
1351
+ * a stream in the set is found to be invalid. If @invalidate is
1352
+ * true, also mark the server Unknown in the topology description,
1353
+ * passing the error information from @why as the reason.
1351
1354
*
1352
1355
* WARNING: pointers to a disconnected mongoc_cluster_node_t or
1353
1356
* its stream are now invalid, be careful of dangling pointers.
@@ -1363,9 +1366,13 @@ _mongoc_cluster_auth_node (mongoc_cluster_t *cluster,
1363
1366
*/
1364
1367
1365
1368
void
1366
- mongoc_cluster_disconnect_node (mongoc_cluster_t * cluster , uint32_t server_id )
1369
+ mongoc_cluster_disconnect_node (mongoc_cluster_t * cluster ,
1370
+ uint32_t server_id ,
1371
+ bool invalidate ,
1372
+ const bson_error_t * why /* IN */ )
1367
1373
{
1368
1374
mongoc_topology_t * topology = cluster -> client -> topology ;
1375
+
1369
1376
ENTRY ;
1370
1377
1371
1378
if (topology -> single_threaded ) {
@@ -1377,13 +1384,15 @@ mongoc_cluster_disconnect_node (mongoc_cluster_t *cluster, uint32_t server_id)
1377
1384
/* might never actually have connected */
1378
1385
if (scanner_node && scanner_node -> stream ) {
1379
1386
mongoc_topology_scanner_node_disconnect (scanner_node , true);
1380
- EXIT ;
1381
1387
}
1382
- EXIT ;
1383
1388
} else {
1384
1389
mongoc_set_rm (cluster -> nodes , server_id );
1385
1390
}
1386
1391
1392
+ if (invalidate ) {
1393
+ mongoc_topology_invalidate_server (topology , server_id , why );
1394
+ }
1395
+
1387
1396
EXIT ;
1388
1397
}
1389
1398
@@ -1606,10 +1615,9 @@ _mongoc_cluster_stream_for_server (mongoc_cluster_t *cluster,
1606
1615
* ServerDescription of type Unknown, and fill the ServerDescription's
1607
1616
* error field with useful information."
1608
1617
*
1609
- * error was filled by fetch_stream_single/pooled, pass it to invalidate ()
1618
+ * error was filled by fetch_stream_single/pooled, pass it to disconnect ()
1610
1619
*/
1611
- mongoc_cluster_disconnect_node (cluster , server_id );
1612
- mongoc_topology_invalidate_server (topology , server_id , err_ptr );
1620
+ mongoc_cluster_disconnect_node (cluster , server_id , true, err_ptr );
1613
1621
}
1614
1622
1615
1623
RETURN (server_stream );
@@ -1643,18 +1651,23 @@ mongoc_cluster_stream_for_server (mongoc_cluster_t *cluster,
1643
1651
bson_error_t * error )
1644
1652
{
1645
1653
mongoc_server_stream_t * server_stream = NULL ;
1654
+ bson_error_t err_local ;
1646
1655
1647
1656
ENTRY ;
1648
1657
1649
1658
BSON_ASSERT (cluster );
1650
1659
BSON_ASSERT (server_id );
1651
1660
1661
+ if (!error ) {
1662
+ error = & err_local ;
1663
+ }
1664
+
1652
1665
server_stream = _mongoc_cluster_stream_for_server (
1653
1666
cluster , server_id , reconnect_ok , error );
1654
1667
1655
1668
if (!server_stream ) {
1656
1669
/* failed */
1657
- mongoc_cluster_disconnect_node (cluster , server_id );
1670
+ mongoc_cluster_disconnect_node (cluster , server_id , true, error );
1658
1671
}
1659
1672
1660
1673
RETURN (server_stream );
@@ -1801,7 +1814,8 @@ mongoc_cluster_fetch_stream_pooled (mongoc_cluster_t *cluster,
1801
1814
if (timestamp == -1 || cluster_node -> timestamp < timestamp ) {
1802
1815
/* topology change or net error during background scan made us remove
1803
1816
* or replace server description since node's birth. destroy node. */
1804
- mongoc_cluster_disconnect_node (cluster , server_id );
1817
+ mongoc_cluster_disconnect_node (
1818
+ cluster , server_id , false /* invalidate */ , NULL );
1805
1819
} else {
1806
1820
return _mongoc_cluster_create_server_stream (
1807
1821
topology , server_id , cluster_node -> stream , error );
@@ -2206,7 +2220,11 @@ mongoc_cluster_check_interval (mongoc_cluster_t *cluster, uint32_t server_id)
2206
2220
2207
2221
if (scanner_node -> last_used + (1000 * CHECK_CLOSED_DURATION_MSEC ) < now ) {
2208
2222
if (mongoc_stream_check_closed (stream )) {
2209
- mongoc_cluster_disconnect_node (cluster , server_id );
2223
+ bson_set_error (& error ,
2224
+ MONGOC_ERROR_STREAM ,
2225
+ MONGOC_ERROR_STREAM_SOCKET ,
2226
+ "connection closed" );
2227
+ mongoc_cluster_disconnect_node (cluster , server_id , true, & error );
2210
2228
return false;
2211
2229
}
2212
2230
}
@@ -2223,9 +2241,7 @@ mongoc_cluster_check_interval (mongoc_cluster_t *cluster, uint32_t server_id)
2223
2241
bson_destroy (& command );
2224
2242
2225
2243
if (!r ) {
2226
- mongoc_cluster_disconnect_node (cluster , server_id );
2227
- mongoc_topology_invalidate_server (
2228
- topology , server_id , & error /* IN */ );
2244
+ mongoc_cluster_disconnect_node (cluster , server_id , true, & error );
2229
2245
}
2230
2246
}
2231
2247
@@ -2420,6 +2436,7 @@ mongoc_cluster_try_recv (mongoc_cluster_t *cluster,
2420
2436
bson_error_t * error )
2421
2437
{
2422
2438
uint32_t server_id ;
2439
+ bson_error_t err_local ;
2423
2440
int32_t msg_len ;
2424
2441
int32_t max_msg_size ;
2425
2442
off_t pos ;
@@ -2435,6 +2452,10 @@ mongoc_cluster_try_recv (mongoc_cluster_t *cluster,
2435
2452
2436
2453
TRACE ("Waiting for reply from server_id \"%u\"" , server_id );
2437
2454
2455
+ if (!error ) {
2456
+ error = & err_local ;
2457
+ }
2458
+
2438
2459
/*
2439
2460
* Buffer the message length to determine how much more to read.
2440
2461
*/
@@ -2444,7 +2465,11 @@ mongoc_cluster_try_recv (mongoc_cluster_t *cluster,
2444
2465
MONGOC_DEBUG (
2445
2466
"Could not read 4 bytes, stream probably closed or timed out" );
2446
2467
mongoc_counter_protocol_ingress_error_inc ();
2447
- mongoc_cluster_disconnect_node (cluster , server_id );
2468
+ mongoc_cluster_disconnect_node (
2469
+ cluster ,
2470
+ server_id ,
2471
+ !mongoc_stream_timed_out (server_stream -> stream ),
2472
+ error );
2448
2473
RETURN (false);
2449
2474
}
2450
2475
@@ -2459,7 +2484,7 @@ mongoc_cluster_try_recv (mongoc_cluster_t *cluster,
2459
2484
MONGOC_ERROR_PROTOCOL ,
2460
2485
MONGOC_ERROR_PROTOCOL_INVALID_REPLY ,
2461
2486
"Corrupt or malicious reply received." );
2462
- mongoc_cluster_disconnect_node (cluster , server_id );
2487
+ mongoc_cluster_disconnect_node (cluster , server_id , true, error );
2463
2488
mongoc_counter_protocol_ingress_error_inc ();
2464
2489
RETURN (false);
2465
2490
}
@@ -2472,7 +2497,11 @@ mongoc_cluster_try_recv (mongoc_cluster_t *cluster,
2472
2497
msg_len - 4 ,
2473
2498
cluster -> sockettimeoutms ,
2474
2499
error )) {
2475
- mongoc_cluster_disconnect_node (cluster , server_id );
2500
+ mongoc_cluster_disconnect_node (
2501
+ cluster ,
2502
+ server_id ,
2503
+ !mongoc_stream_timed_out (server_stream -> stream ),
2504
+ error );
2476
2505
mongoc_counter_protocol_ingress_error_inc ();
2477
2506
RETURN (false);
2478
2507
}
@@ -2485,7 +2514,7 @@ mongoc_cluster_try_recv (mongoc_cluster_t *cluster,
2485
2514
MONGOC_ERROR_PROTOCOL ,
2486
2515
MONGOC_ERROR_PROTOCOL_INVALID_REPLY ,
2487
2516
"Failed to decode reply from server." );
2488
- mongoc_cluster_disconnect_node (cluster , server_id );
2517
+ mongoc_cluster_disconnect_node (cluster , server_id , true, error );
2489
2518
mongoc_counter_protocol_ingress_error_inc ();
2490
2519
RETURN (false);
2491
2520
}
0 commit comments