Skip to content

Commit 2ef7518

Browse files
dmantipovamarts
authored andcommitted
cli: fix data race when handling connection status
Found with GCC ThreadSanitizer: WARNING: ThreadSanitizer: data race (pid=287943) Write of size 4 at 0x00000047dfa0 by thread T4: #0 cli_rpc_notify /path/to/glusterfs/cli/src/cli.c:313 (gluster+0x40a6df) #1 rpc_clnt_handle_disconnect /path/to/glusterfs/rpc/rpc-lib/src/rpc-clnt.c:821 (libgfrpc.so.0+0x13f04) #2 rpc_clnt_notify /path/to/glusterfs/rpc/rpc-lib/src/rpc-clnt.c:882 (libgfrpc.so.0+0x13f04) #3 rpc_transport_notify /path/to/glusterfs/rpc/rpc-lib/src/rpc-transport.c:520 (libgfrpc.so.0+0xf070) #4 socket_event_poll_err /path/to/glusterfs/rpc/rpc-transport/socket/src/socket.c:1364 (socket.so+0x812c) #5 socket_event_handler /path/to/glusterfs/rpc/rpc-transport/socket/src/socket.c:2958 (socket.so+0xc453) #6 socket_event_handler /path/to/glusterfs/rpc/rpc-transport/socket/src/socket.c:2854 (socket.so+0xc453) #7 event_dispatch_epoll_handler /path/to/glusterfs/libglusterfs/src/event-epoll.c:640 (libglusterfs.so.0+0xcaf23) #8 event_dispatch_epoll_worker /path/to/glusterfs/libglusterfs/src/event-epoll.c:751 (libglusterfs.so.0+0xcaf23) #9 <null> <null> (libtsan.so.0+0x2d33f) Previous read of size 4 at 0x00000047dfa0 by thread T3 (mutexes: write M3587): #0 cli_cmd_await_connected /path/to/glusterfs/cli/src/cli-cmd.c:321 (gluster+0x40ca37) #1 cli_cmd_process /path/to/glusterfs/cli/src/cli-cmd.c:123 (gluster+0x40cc74) #2 cli_batch /path/to/glusterfs/cli/src/input.c:29 (gluster+0x40c2b9) #3 <null> <null> (libtsan.so.0+0x2d33f) Location is global 'connected' of size 4 at 0x00000047dfa0 (gluster+0x00000047dfa0) Change-Id: Ie85a8a80a2c5b82252c0c1d45e68ebe9938da2eb Signed-off-by: Dmitry Antipov <[email protected]> Fixes: #1311
1 parent 3510916 commit 2ef7518

File tree

4 files changed

+25
-11
lines changed

4 files changed

+25
-11
lines changed

cli/src/cli-cmd.c

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ static pthread_cond_t conn = PTHREAD_COND_INITIALIZER;
2828
static pthread_mutex_t conn_mutex = PTHREAD_MUTEX_INITIALIZER;
2929

3030
int cli_op_ret = 0;
31-
int connected = 0;
31+
static gf_boolean_t connected = _gf_false;
3232

3333
static unsigned
3434
cli_cmd_needs_connection(struct cli_cmd_word *word)
@@ -328,19 +328,32 @@ cli_cmd_await_connected(unsigned conn_timo)
328328
}
329329

330330
int32_t
331-
cli_cmd_broadcast_connected()
331+
cli_cmd_broadcast_connected(gf_boolean_t status)
332332
{
333333
pthread_mutex_lock(&conn_mutex);
334334
{
335-
connected = 1;
335+
connected = status;
336336
pthread_cond_broadcast(&conn);
337337
}
338-
339338
pthread_mutex_unlock(&conn_mutex);
340339

341340
return 0;
342341
}
343342

343+
gf_boolean_t
344+
cli_cmd_connected(void)
345+
{
346+
gf_boolean_t status;
347+
348+
pthread_mutex_lock(&conn_mutex);
349+
{
350+
status = connected;
351+
}
352+
pthread_mutex_unlock(&conn_mutex);
353+
354+
return status;
355+
}
356+
344357
int
345358
cli_cmd_submit(struct rpc_clnt *rpc, void *req, call_frame_t *frame,
346359
rpc_clnt_prog_t *prog, int procnum, struct iobref *iobref,

cli/src/cli-rpc-ops.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ enum gf_task_types { GF_TASK_TYPE_REBALANCE, GF_TASK_TYPE_REMOVE_BRICK };
5050
extern struct rpc_clnt *global_quotad_rpc;
5151
rpc_clnt_prog_t cli_quotad_clnt;
5252
extern rpc_clnt_prog_t *cli_rpc_prog;
53-
extern int connected;
5453

5554
static int32_t
5655
gf_cli_remove_brick(call_frame_t *frame, xlator_t *this, void *data);
@@ -3406,7 +3405,7 @@ gf_cli_quota_list(cli_local_t *local, char *volname, dict_t *dict,
34063405
char *default_sl, int count, int op_ret, int op_errno,
34073406
char *op_errstr)
34083407
{
3409-
if (!connected)
3408+
if (!cli_cmd_connected())
34103409
goto out;
34113410

34123411
if (count > 0) {

cli/src/cli.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@
5555

5656
#include "xdr-generic.h"
5757

58-
extern int connected;
5958
/* using argp for command line parsing */
6059

6160
const char *argp_program_version =
@@ -303,14 +302,14 @@ cli_rpc_notify(struct rpc_clnt *rpc, void *mydata, rpc_clnt_event_t event,
303302

304303
switch (event) {
305304
case RPC_CLNT_CONNECT: {
306-
cli_cmd_broadcast_connected();
305+
cli_cmd_broadcast_connected(_gf_true);
307306
gf_log(this->name, GF_LOG_TRACE, "got RPC_CLNT_CONNECT");
308307
break;
309308
}
310309

311310
case RPC_CLNT_DISCONNECT: {
311+
cli_cmd_broadcast_connected(_gf_false);
312312
gf_log(this->name, GF_LOG_TRACE, "got RPC_CLNT_DISCONNECT");
313-
connected = 0;
314313
if (!global_state->prompt && global_state->await_connected) {
315314
ret = 1;
316315
cli_out(

cli/src/cli.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -329,11 +329,14 @@ cli_local_get();
329329
void
330330
cli_local_wipe(cli_local_t *local);
331331

332+
gf_boolean_t
333+
cli_cmd_connected();
334+
332335
int32_t
333-
cli_cmd_await_connected();
336+
cli_cmd_await_connected(unsigned timeout);
334337

335338
int32_t
336-
cli_cmd_broadcast_connected();
339+
cli_cmd_broadcast_connected(gf_boolean_t status);
337340

338341
int
339342
cli_rpc_notify(struct rpc_clnt *rpc, void *mydata, rpc_clnt_event_t event,

0 commit comments

Comments
 (0)