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

Data race in lwt_unix_start_job #1026

Open
OlivierNicole opened this issue Jul 11, 2024 · 1 comment
Open

Data race in lwt_unix_start_job #1026

OlivierNicole opened this issue Jul 11, 2024 · 1 comment

Comments

@OlivierNicole
Copy link

Something strange showed up in ThreadSanitizer logs: in Lwt_unix, more precisely in lwt_unix_start_job, the thread_waiting_count variable is written with the pool mutex taken:

lwt/src/unix/lwt_unix_stubs.c

Lines 1002 to 1010 in 68cf601

lwt_unix_mutex_lock(&pool_mutex);
DEBUG("waiting for something to do");
/* Wait for something to do. */
while (pool_queue == NULL) {
++thread_waiting_count;
lwt_unix_condition_wait(&pool_condition, &pool_mutex);
}

This happens on the worker thread.

However, it can be read by the main thread without locking any mutexes:

lwt/src/unix/lwt_unix_stubs.c

Lines 1061 to 1069 in 68cf601

CAMLprim value lwt_unix_start_job(value val_job, value val_async_method) {
lwt_unix_job job = Job_val(val_job);
lwt_unix_async_method async_method = Int_val(val_async_method);
int done = 0;
/* Fallback to synchronous call if there is no worker available and
we can not launch more threads. */
if (async_method != LWT_UNIX_ASYNC_METHOD_NONE && thread_waiting_count == 0 &&
thread_count >= pool_size)

This looks like a genuine data race.

@OlivierNicole
Copy link
Author

Similarly TSan reports unsynchronised read and write between:

static void execute_job(lwt_unix_job job) {
DEBUG("executing the job");
lwt_unix_mutex_lock(&job->mutex);
/* Mark the job as running. */
job->state = LWT_UNIX_JOB_STATE_RUNNING;

and

done = job->state == LWT_UNIX_JOB_STATE_DONE;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant