Skip to content

Commit

Permalink
Unix.fork: do not destroy IO channel mutexes in the child process
Browse files Browse the repository at this point in the history
Some of these mutexes may be locked, so this is undefined behavior,
and can cause fatal errors in the C thread library (ocaml#12636).

Leaving IO channel mutexes unchanged is what OCaml 5 does.
  • Loading branch information
xavierleroy committed Oct 10, 2023
1 parent 9313ca9 commit d2a1aa2
Showing 1 changed file with 7 additions and 11 deletions.
18 changes: 7 additions & 11 deletions otherlibs/systhreads/st_stubs.c
Original file line number Diff line number Diff line change
Expand Up @@ -401,8 +401,6 @@ static void caml_thread_remove_info(caml_thread_t th)

static void caml_thread_reinitialize(void)
{
struct channel * chan;

/* Remove all other threads (now nonexistent)
from the doubly-linked list of threads */
while (curr_thread->next != curr_thread) {
Expand All @@ -417,15 +415,13 @@ static void caml_thread_reinitialize(void)
/* Tick thread is not currently running in child process, will be
re-created at next Thread.create */
caml_tick_thread_running = 0;
/* Destroy all IO mutexes; will be reinitialized on demand */
for (chan = caml_all_opened_channels;
chan != NULL;
chan = chan->next) {
if (chan->mutex != NULL) {
st_mutex_destroy(chan->mutex);
chan->mutex = NULL;
}
}
/* IO channel mutexes that are locked at the time of the fork()
remain locked forever in the child process. This can cause
deadlocks. We used to destroy all IO channel mutexes, so that
they are reinitialized on demand and deadlocks are avoided.
However, this could cause fatal errors in the C thread library
(see #12636). So, like in OCaml 5, let's keep the IO channel
mutexes unchanged. */
}

/* Initialize the thread machinery */
Expand Down

0 comments on commit d2a1aa2

Please sign in to comment.