Skip to content

Commit 6135ca3

Browse files
committed
DAOS-18779 client: reset per-thread based variable after fork - b28
DAOS maintains some per-thread based variables on client. They are usually used to distinguish current process from the others. If application creates new process (such as via fork()), these per-thread variables will inherit the values from parents, and if without reset, we cannot distinguish the children processes from the parents via these per-thread based variables. That is not only confused but also may cause correctness issues. The patch offers related reset interfaces, and application can trigger them via after_fork() callback. Allow-unstable-test: true Signed-off-by: Fan Yong <fan.yong@hpe.com>
1 parent 091c541 commit 6135ca3

7 files changed

Lines changed: 53 additions & 21 deletions

File tree

src/client/api/event.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
* (C) Copyright 2016-2024 Intel Corporation.
3-
* (C) Copyright 2025 Hewlett Packard Enterprise Development LP
43
* (C) Copyright 2025 Google LLC
4+
* (C) Copyright 2025-2026 Hewlett Packard Enterprise Development LP
55
*
66
* SPDX-License-Identifier: BSD-2-Clause-Patent
77
*/
@@ -18,6 +18,7 @@
1818
#include "client_internal.h"
1919
#include <daos/mgmt.h>
2020
#include <daos/rpc.h>
21+
#include <daos/tls.h>
2122

2223
/** thread-private event */
2324
static __thread daos_event_t ev_thpriv;
@@ -48,6 +49,9 @@ daos_eq_lib_init(crt_init_options_t *crt_info)
4849
{
4950
int rc;
5051

52+
dc_tls_reset();
53+
daos_dti_reset();
54+
5155
D_MUTEX_LOCK(&daos_eq_lock);
5256
if (eq_ref > 0) {
5357
eq_ref++;
@@ -92,8 +96,6 @@ daos_eq_lib_init(crt_init_options_t *crt_info)
9296

9397
unlock:
9498
D_MUTEX_UNLOCK(&daos_eq_lock);
95-
if (rc == 0)
96-
daos_dti_reset();
9799

98100
return rc;
99101
crt:
@@ -107,6 +109,8 @@ daos_eq_lib_reset_after_fork(void)
107109
crt_init_options_t *crt_info;
108110
int rc;
109111

112+
d_log_reset();
113+
110114
eq_ref = 0;
111115
ev_thpriv_is_init = false;
112116
crt_info = daos_crt_init_opt_get(false, 1);

src/client/dfuse/il/int_posix.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/**
22
* (C) Copyright 2017-2024 Intel Corporation.
3+
* (C) Copyright 2026 Hewlett Packard Enterprise Development LP
34
*
45
* SPDX-License-Identifier: BSD-2-Clause-Patent
56
*/
@@ -827,6 +828,8 @@ child_hdlr(void)
827828
/** Reset event queue */
828829
ioil_eqh = ioil_iog.iog_main_eqh = DAOS_HDL_INVAL;
829830

831+
saved_errno = 0;
832+
830833
if (ioil_iog.iog_eq_count_max) {
831834
rc = daos_eq_create(&ioil_eqh);
832835
if (rc)

src/common/tls.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/**
22
* (C) Copyright 2016-2023 Intel Corporation.
3+
* (C) Copyright 2026 Hewlett Packard Enterprise Development LP
34
*
45
* SPDX-License-Identifier: BSD-2-Clause-Patent
56
*/
@@ -206,6 +207,15 @@ dc_tls_get(unsigned int tag)
206207
return (struct daos_thread_local_storage *)pthread_getspecific(dc_tls_key);
207208
}
208209

210+
void
211+
dc_tls_reset(void)
212+
{
213+
if (dc_tls_thread_init)
214+
dc_tls_fini();
215+
216+
dc_tls_thread_init = false;
217+
}
218+
209219
struct daos_thread_local_storage *
210220
dss_tls_get()
211221
{

src/gurt/dlog.c

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
* (C) Copyright 2016-2024 Intel Corporation.
3-
* (C) Copyright 2025 Hewlett Packard Enterprise Development LP
3+
* (C) Copyright 2025-2026 Hewlett Packard Enterprise Development LP
44
*
55
* SPDX-License-Identifier: BSD-2-Clause-Patent
66
*/
@@ -551,6 +551,9 @@ d_log_disable_logging(void)
551551
mst.log_old_fd = -1;
552552
}
553553

554+
static __thread uint32_t dlog_tid = -1;
555+
static __thread uint32_t dlog_pid = -1;
556+
554557
/**
555558
* d_vlog: core log function, front-ended by d_log
556559
* we vsnprintf the message into a holding buffer to format it. then we
@@ -569,8 +572,6 @@ void d_vlog(int flags, const char *fmt, va_list ap)
569572
{
570573
#define DLOG_TBSIZ 1024 /* bigger than any line should be */
571574
static __thread char b[DLOG_TBSIZ];
572-
static __thread uint32_t tid = -1;
573-
static __thread uint32_t pid = -1;
574575
static uint64_t last_flush;
575576

576577
uint64_t uid = 0;
@@ -608,14 +609,14 @@ void d_vlog(int flags, const char *fmt, va_list ap)
608609

609610
if ((mst.oflags & DLOG_FLV_TAG) && (mst.oflags & DLOG_FLV_LOGPID)) {
610611
/* Init static members in ahead of lock */
611-
if (pid == (uint32_t)(-1))
612-
pid = (uint32_t)getpid();
612+
if (unlikely(dlog_pid == (uint32_t)(-1)))
613+
dlog_pid = (uint32_t)getpid();
613614

614-
if (tid == (uint32_t)(-1)) {
615+
if (unlikely(dlog_tid == (uint32_t)(-1))) {
615616
if (mst.log_id_cb)
616-
mst.log_id_cb(&tid, NULL);
617+
mst.log_id_cb(&dlog_tid, NULL);
617618
else
618-
tid = (uint32_t)syscall(SYS_gettid);
619+
dlog_tid = (uint32_t)syscall(SYS_gettid);
619620
}
620621

621622
if (mst.log_id_cb)
@@ -653,9 +654,8 @@ void d_vlog(int flags, const char *fmt, va_list ap)
653654

654655
if (mst.oflags & DLOG_FLV_TAG) {
655656
if (mst.oflags & DLOG_FLV_LOGPID) {
656-
hlen += snprintf(b + hlen, sizeof(b) - hlen,
657-
"%s%d/%d/"DF_U64"] ", d_log_xst.tag,
658-
pid, tid, uid);
657+
hlen += snprintf(b + hlen, sizeof(b) - hlen, "%s%d/%d/" DF_U64 "] ",
658+
d_log_xst.tag, dlog_pid, dlog_tid, uid);
659659
} else {
660660
hlen += snprintf(b + hlen, sizeof(b) - hlen, "%s ",
661661
d_log_xst.tag);
@@ -1405,3 +1405,13 @@ int d_log_getmasks(char *buf, int discard, int len, int unterm)
14051405
/* buf == NULL means probe for length ... */
14061406
return ((buf == NULL) ? total : len - resid);
14071407
}
1408+
1409+
void
1410+
d_log_reset(void)
1411+
{
1412+
dlog_tid = -1;
1413+
dlog_pid = -1;
1414+
pre_err = 0;
1415+
pre_err_line = 0;
1416+
pre_err_time = 0;
1417+
}

src/include/daos/tls.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/**
22
* (C) Copyright 2016-2024 Intel Corporation.
3+
* (C) Copyright 2026 Hewlett Packard Enterprise Development LP
34
*
45
* SPDX-License-Identifier: BSD-2-Clause-Patent
56
*/
@@ -62,6 +63,8 @@ struct daos_thread_local_storage *
6263
dss_tls_get(void);
6364
struct daos_thread_local_storage *
6465
dc_tls_get(unsigned int tag);
66+
void
67+
dc_tls_reset(void);
6568

6669
int
6770
ds_tls_key_create(void);

src/include/gurt/dlog.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
* (C) Copyright 2016-2024 Intel Corporation.
3-
* (C) Copyright 2025 Hewlett Packard Enterprise Development LP
3+
* (C) Copyright 2025-2026 Hewlett Packard Enterprise Development LP
44
*
55
* SPDX-License-Identifier: BSD-2-Clause-Patent
66
*/
@@ -456,6 +456,12 @@ void d_log_sync(void);
456456
void
457457
d_log_disable_logging(void);
458458

459+
/**
460+
* Reset __thread variables (after fork).
461+
*/
462+
void
463+
d_log_reset(void);
464+
459465
#if defined(__cplusplus)
460466
}
461467
#endif

src/pool/rpc.h

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
* (C) Copyright 2016-2024 Intel Corporation.
3-
* (C) Copyright 2025 Hewlett Packard Enterprise Development LP
3+
* (C) Copyright 2025-2026 Hewlett Packard Enterprise Development LP
44
*
55
* SPDX-License-Identifier: BSD-2-Clause-Patent
66
*/
@@ -958,12 +958,8 @@ pool_req_create_common(crt_context_t crt_ctx, crt_endpoint_t *tgt_ep, crt_opcode
958958
{
959959
int rc;
960960
crt_opcode_t opcode;
961-
static __thread uuid_t cli_id;
962961
struct pool_op_in *in;
963962

964-
if (uuid_is_null(cli_id))
965-
uuid_generate(cli_id);
966-
967963
opcode = DAOS_RPC_OPCODE(opc, DAOS_POOL_MODULE, proto_ver);
968964
/* call daos_rpc_tag to get the target tag/context idx */
969965
tgt_ep->ep_tag = daos_rpc_tag(DAOS_REQ_POOL, tgt_ep->ep_tag);
@@ -982,7 +978,7 @@ pool_req_create_common(crt_context_t crt_ctx, crt_endpoint_t *tgt_ep, crt_opcode
982978
*req_timep = d_hlc_get();
983979

984980
if (req_timep) {
985-
uuid_copy(in->pi_cli_id, cli_id);
981+
daos_get_client_uuid(&in->pi_cli_id);
986982
in->pi_time = *req_timep;
987983
}
988984
return 0;

0 commit comments

Comments
 (0)