Skip to content

Commit db94044

Browse files
authored
Merge pull request #2274 from ZhuXxxx/master
1.Fix the bug that after calling zsys_shutdown() for the first time, …
2 parents 1b5062c + 69ec583 commit db94044

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

src/zsys.c

+16-1
Original file line numberDiff line numberDiff line change
@@ -161,10 +161,16 @@ s_zsys_vprintf_hint (int hint, const char *format, va_list argptr);
161161
// mutex for pthread_once to run the init function only once in a process
162162
static pthread_once_t init_all_mutex_var = PTHREAD_ONCE_INIT;
163163

164+
// handler to destroy s_mutex
165+
static void zsys_destroy_mutex() {
166+
ZMUTEX_DESTROY(s_mutex);
167+
}
168+
164169
// handler to initialize mutexes one time in multi threaded env
165170
static void zsys_initialize_mutex() {
166171
ZMUTEX_INIT (s_mutex);
167172
ZMUTEX_INIT (s_init_mutex);
173+
atexit (zsys_destroy_mutex);
168174
}
169175

170176
// handler to detect fork condition and cleanup the stale context inherited from parent process
@@ -175,6 +181,13 @@ static void zsys_pthread_at_fork_handler(void) {
175181
// call cleanup
176182
zsys_cleanup();
177183
}
184+
185+
// mutex for pthread_once to register the atexit function only once in a process
186+
static pthread_once_t register_atexit_shutdown = PTHREAD_ONCE_INIT;
187+
// handler to register the atexit function one time in multi threaded env
188+
static void zsys_register_atexit_shutdown(void) {
189+
atexit (zsys_shutdown);
190+
}
178191
#endif
179192

180193
void *
@@ -279,7 +292,7 @@ zsys_init (void)
279292
s_initialized = true;
280293

281294
#if defined (__UNIX__)
282-
atexit (zsys_shutdown);
295+
pthread_once(&register_atexit_shutdown, zsys_register_atexit_shutdown);
283296
pthread_atfork(NULL, NULL, &zsys_pthread_at_fork_handler);
284297
//don't hold the lock because some of the function will call zsys_init again
285298
ZMUTEX_UNLOCK(s_init_mutex);
@@ -407,7 +420,9 @@ zsys_shutdown (void)
407420
else
408421
zsys_error ("dangling sockets: cannot terminate ZMQ safely");
409422

423+
#if !defined (__UNIX__)
410424
ZMUTEX_DESTROY (s_mutex);
425+
#endif
411426

412427
// Free dynamically allocated properties
413428
freen (s_interface);

0 commit comments

Comments
 (0)