Skip to content

Commit e067515

Browse files
committed
CDRIVER-4185 log and do not update state when creating thread fails (#1315)
* log and do not update state when creating thread fails add `errno_out` arg to mcommon_thread_create * return error code
1 parent 30db519 commit e067515

8 files changed

+55
-12
lines changed

src/common/common-thread-private.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,9 @@ typedef struct {
119119
* libbson and libmongoc statically. */
120120
int
121121
mcommon_thread_join (bson_thread_t thread);
122+
// mcommon_thread_create returns 0 on success. Returns a non-zero error code on
123+
// error. Callers may use `bson_strerror_r` to get an error message from the
124+
// returned error code.
122125
int
123126
mcommon_thread_create (bson_thread_t *thread,
124127
BSON_THREAD_FUN_TYPE (func),

src/common/common-thread.c

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,17 @@
1616

1717
#include "common-thread-private.h"
1818

19+
#include <errno.h>
20+
1921
#if defined(BSON_OS_UNIX)
2022
int
2123
mcommon_thread_create (bson_thread_t *thread,
2224
BSON_THREAD_FUN_TYPE (func),
2325
void *arg)
2426
{
27+
BSON_ASSERT_PARAM (thread);
28+
BSON_ASSERT_PARAM (func);
29+
BSON_ASSERT (arg || true); // optional.
2530
return pthread_create (thread, NULL, func, arg);
2631
}
2732
int
@@ -45,9 +50,13 @@ mcommon_thread_create (bson_thread_t *thread,
4550
BSON_THREAD_FUN_TYPE (func),
4651
void *arg)
4752
{
53+
BSON_ASSERT_PARAM (thread);
54+
BSON_ASSERT_PARAM (func);
55+
BSON_ASSERT (arg || true); // optional.
56+
4857
*thread = (HANDLE) _beginthreadex (NULL, 0, func, arg, 0, NULL);
4958
if (0 == *thread) {
50-
return 1;
59+
return errno;
5160
}
5261
return 0;
5362
}

src/libmongoc/src/mongoc/mongoc-server-monitor.c

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1402,9 +1402,19 @@ mongoc_server_monitor_run (mongoc_server_monitor_t *server_monitor)
14021402
bson_mutex_lock (&server_monitor->shared.mutex);
14031403
if (server_monitor->shared.state == MONGOC_THREAD_OFF) {
14041404
server_monitor->is_rtt = false;
1405-
server_monitor->shared.state = MONGOC_THREAD_RUNNING;
1406-
mcommon_thread_create (
1405+
int ret = mcommon_thread_create (
14071406
&server_monitor->thread, _server_monitor_thread, server_monitor);
1407+
if (ret == 0) {
1408+
server_monitor->shared.state = MONGOC_THREAD_RUNNING;
1409+
} else {
1410+
char errmsg_buf[BSON_ERROR_BUFFER_SIZE];
1411+
char *errmsg = bson_strerror_r (ret, errmsg_buf, sizeof errmsg_buf);
1412+
_server_monitor_log (server_monitor,
1413+
MONGOC_LOG_LEVEL_ERROR,
1414+
"Failed to start monitoring thread. This server "
1415+
"may not be selectable. Error: %s",
1416+
errmsg);
1417+
}
14081418
}
14091419
bson_mutex_unlock (&server_monitor->shared.mutex);
14101420
}
@@ -1415,9 +1425,19 @@ mongoc_server_monitor_run_as_rtt (mongoc_server_monitor_t *server_monitor)
14151425
bson_mutex_lock (&server_monitor->shared.mutex);
14161426
if (server_monitor->shared.state == MONGOC_THREAD_OFF) {
14171427
server_monitor->is_rtt = true;
1418-
server_monitor->shared.state = MONGOC_THREAD_RUNNING;
1419-
mcommon_thread_create (
1428+
int ret = mcommon_thread_create (
14201429
&server_monitor->thread, _server_monitor_rtt_thread, server_monitor);
1430+
if (ret == 0) {
1431+
server_monitor->shared.state = MONGOC_THREAD_RUNNING;
1432+
} else {
1433+
char errmsg_buf[BSON_ERROR_BUFFER_SIZE];
1434+
char *errmsg = bson_strerror_r (ret, errmsg_buf, sizeof errmsg_buf);
1435+
_server_monitor_log (
1436+
server_monitor,
1437+
MONGOC_LOG_LEVEL_ERROR,
1438+
"Failed to start Round-Trip Time monitoring thread. Error: %s",
1439+
errmsg);
1440+
}
14211441
}
14221442
bson_mutex_unlock (&server_monitor->shared.mutex);
14231443
}

src/libmongoc/src/mongoc/mongoc-topology-background-monitoring.c

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,9 +162,17 @@ _mongoc_topology_background_monitoring_start (mongoc_topology_t *topology)
162162
_mongoc_topology_background_monitoring_reconcile (topology, tdmod.new_td);
163163
/* Start SRV polling thread. */
164164
if (mongoc_topology_should_rescan_srv (topology)) {
165-
topology->is_srv_polling = true;
166-
mcommon_thread_create (
165+
int ret = mcommon_thread_create (
167166
&topology->srv_polling_thread, srv_polling_run, topology);
167+
if (ret == 0) {
168+
topology->is_srv_polling = true;
169+
} else {
170+
char errmsg_buf[BSON_ERROR_BUFFER_SIZE];
171+
char *errmsg = bson_strerror_r (ret, errmsg_buf, sizeof errmsg_buf);
172+
MONGOC_ERROR ("Failed to start SRV polling thread. SRV records "
173+
"will not be polled. Error: %s",
174+
errmsg);
175+
}
168176
}
169177
}
170178

src/libmongoc/tests/json-test-operations.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,9 @@ static void
140140
start_thread (json_test_worker_thread_t *wt)
141141
{
142142
wt->shutdown_requested = false;
143-
mcommon_thread_create (&wt->thread, json_test_worker_thread_run, wt);
143+
int ret =
144+
mcommon_thread_create (&wt->thread, json_test_worker_thread_run, wt);
145+
ASSERT_CMPINT (0, ==, ret);
144146
}
145147

146148
static void

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -451,8 +451,8 @@ test_client_pool_max_pool_size_exceeded (void)
451451
bson_mutex_init (&args->mutex);
452452
mongoc_cond_init (&args->cond);
453453

454-
mcommon_thread_create (&thread1, worker, args);
455-
mcommon_thread_create (&thread2, worker, args);
454+
ASSERT_CMPINT (0, ==, mcommon_thread_create (&thread1, worker, args));
455+
ASSERT_CMPINT (0, ==, mcommon_thread_create (&thread2, worker, args));
456456

457457
bson_mutex_lock (&args->mutex);
458458
while (args->nleft > 0) {

src/libmongoc/tests/test-mongoc-client-side-encryption.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6793,7 +6793,8 @@ test_bypass_mongocryptd_shared_library (void *unused)
67936793
listen_socket_args_t *args = bson_malloc0 (sizeof (listen_socket_args_t));
67946794
bson_mutex_init (&args->mutex);
67956795
mongoc_cond_init (&args->cond);
6796-
mcommon_thread_create (&socket_thread, listen_socket, args);
6796+
ASSERT_CMPINT (
6797+
0, ==, mcommon_thread_create (&socket_thread, listen_socket, args));
67976798

67986799
// configure mongoclient with auto encryption
67996800
char *env_cryptSharedLibPath =

src/libmongoc/tests/test-mongoc-handshake.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -925,7 +925,7 @@ test_mongoc_handshake_race_condition (void)
925925

926926
for (j = 0; j < 4; ++j) {
927927
BSON_ASSERT (!mcommon_thread_create (
928-
&threads[j], &handshake_append_worker, NULL));
928+
&threads[j], &handshake_append_worker, NULL /* args */));
929929
}
930930
for (j = 0; j < 4; ++j) {
931931
mcommon_thread_join (threads[j]);

0 commit comments

Comments
 (0)