ocsigen / lwt

OCaml promises and concurrent I/O
https://ocsigen.org/lwt
MIT License
704 stars 172 forks source link

Data race in lwt_unix_start_job #1026

Open OlivierNicole opened 2 months ago

OlivierNicole commented 2 months ago

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:

https://github.com/ocsigen/lwt/blob/68cf601f5d0250a0193e7f0c7af5a65eb39f32c9/src/unix/lwt_unix_stubs.c#L1002-L1010

This happens on the worker thread.

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

https://github.com/ocsigen/lwt/blob/68cf601f5d0250a0193e7f0c7af5a65eb39f32c9/src/unix/lwt_unix_stubs.c#L1061-L1069

This looks like a genuine data race.

OlivierNicole commented 2 months ago

Similarly TSan reports unsynchronised read and write between:

https://github.com/ocsigen/lwt/blob/68cf601f5d0250a0193e7f0c7af5a65eb39f32c9/src/unix/lwt_unix_stubs.c#L912-L918

and

https://github.com/ocsigen/lwt/blob/68cf601f5d0250a0193e7f0c7af5a65eb39f32c9/src/unix/lwt_unix_stubs.c#L1126