Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bugfix: Deadlock in zlog_fini() while entering pthread_rwlock_wrlock() in multithreaded environment #184

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 23 additions & 5 deletions src/zlog.c
Original file line number Diff line number Diff line change
Expand Up @@ -302,10 +302,27 @@ void zlog_fini(void)
int rc = 0;

zc_debug("------zlog_fini start------");
rc = pthread_rwlock_wrlock(&zlog_env_lock);
if (rc) {
zc_error("pthread_rwlock_wrlock fail, rc[%d]", rc);
return;

do {
zc_error(">> pthread_rwlock_trywrlock");
rc = pthread_rwlock_trywrlock(&zlog_env_lock);
if (rc) {
zc_error("zlog_fini: pthread_rwlock_trywrlock fail, rc[%d]", rc);
usleep(100 * 1000);
}
} while(rc != 0 && rc != EDEADLK && rc != EBUSY);

if (rc != EBUSY) {
rc = pthread_rwlock_wrlock(&zlog_env_lock);
if (rc) {
zc_error("zlog_fini: pthread_rwlock_wrlock fail, rc[%d]", rc);
if (rc != EDEADLK && rc != EBUSY) {
zc_error("zlog_fini: pthread_rwlock_wrlock fail, rc[%d], early exit", rc);
return;
}
}
} else {
zc_error("zlog_fini: pthread_rwlock_wrlock skipping, last rc[%d]", rc);
}

if (!zlog_env_is_init) {
Expand Down Expand Up @@ -534,6 +551,7 @@ char *zlog_get_mdc(char *key)
return value;
err:
rc = pthread_rwlock_unlock(&zlog_env_lock);

if (rc) {
zc_error("pthread_rwlock_unlock fail, rc=[%d]", rc);
return NULL;
Expand Down Expand Up @@ -1007,7 +1025,7 @@ int zlog_set_record(const char *rname, zlog_record_fn record_output)
zlog_rule_set_record(a_rule, zlog_env_records);
}

zlog_set_record_exit:
zlog_set_record_exit:
rd = pthread_rwlock_unlock(&zlog_env_lock);
if (rd) {
zc_error("pthread_rwlock_unlock fail, rd=[%d]", rd);
Expand Down