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

ZOOKEEPER-4809: Fix do_completion use-after-free when log level is debug #2139

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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 8 additions & 4 deletions zookeeper-client/zookeeper-client-c/src/mt_adaptor.c
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,7 @@ unsigned __stdcall do_io( void * v)
void *do_io(void *v)
#endif
{
log_callback_fn log_fn;
zhandle_t *zh = (zhandle_t*)v;
#ifndef WIN32
struct pollfd fds[2];
Expand Down Expand Up @@ -456,8 +457,9 @@ void *do_io(void *v)
if(is_unrecoverable(zh))
break;
}
api_epilog(zh, 0);
LOG_DEBUG(LOGCALLBACK(zh), "IO thread terminated");
log_fn = LOGCALLBACK(zh);
api_epilog(zh, 0);
LOG_DEBUG(log_fn, "IO thread terminated");
return 0;
}

Expand All @@ -468,6 +470,7 @@ void *do_completion(void *v)
#endif
{
zhandle_t *zh = v;
log_callback_fn fn;
api_prolog(zh);
notify_thread_ready(zh);
LOG_DEBUG(LOGCALLBACK(zh), "started completion thread");
Expand All @@ -479,8 +482,9 @@ void *do_completion(void *v)
pthread_mutex_unlock(&zh->completions_to_process.lock);
process_completions(zh);
}
api_epilog(zh, 0);
LOG_DEBUG(LOGCALLBACK(zh), "completion thread terminated");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it more conventional to swap the two statements ? I saw several return api_epilog(zh, rc) patterns. Also, line 460 has same problem.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed, it would be conventional to log first and then api_epilog().

But api_epilog() does zookeeper_close() when the rc is zero, i.e. the completion thread is still running after the "terminated" output, which could lead to confusion?

fn = LOGCALLBACK(zh);
api_epilog(zh, 0);
LOG_DEBUG(fn, "completion thread terminated");
return 0;
}

Expand Down