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

Make signal handling work on OCaml 5 and with Eio #993

Merged
merged 4 commits into from
Jul 17, 2023

Conversation

talex5
Copy link
Contributor

@talex5 talex5 commented Jun 22, 2023

The first commit fixes #981 by using SA_ONSTACK with OCaml 5 so that signals do not cause memory corruption.

The second commit is a solution to #991. Before installing a signal handler, Lwt now checks first with the engine, which may now prevent Lwt from installing its handler and instead forward signals from some external handler.

This allows e.g. sharing the SIGCHLD handler with Eio.

It would also be possible to do it the other way around (so that Eio uses Lwt's signal handler). However, Lwt only notifies a single domain and if that domain is busy then the signal will be delayed, whereas Eio's signal handler notifies all interested domains immediately.

The third commit fixes #994, which causes the process to segfault if an OCaml signal handler runs in a Lwt worker thread before that thread has set the signal mask. This bug is more likely to be seen when using Lwt with Eio.

These changes should be low-risk:

/cc @raphael-proust

Before installing a signal handler, Lwt now checks first with the
engine, which may now prevent Lwt from installing its handler and
instead forward signals from some external handler.

This allows e.g. sharing the SIGCHLD handler with Eio.
@talex5
Copy link
Contributor Author

talex5 commented Jul 11, 2023

Eio uses a plain OCaml signal handler for SIGCHLD, so this might make #994 more likely.

If an OCaml signal handler runs in a Lwt worker thread then the process
will dereference a NULL pointer and crash. The worker tried to avoid
this by masking out all signals after starting, but by then it may be
too late. Now, we mask out signals before creating a worker thread and
then restore the mask in the main thread afterwards.
@talex5
Copy link
Contributor Author

talex5 commented Jul 12, 2023

I've added a fix for #994 to this PR too (and updated the PR description).

@raphael-proust
Copy link
Collaborator

Looks good to me.

@raphael-proust raphael-proust merged commit e0ebd72 into ocsigen:master Jul 17, 2023
22 of 23 checks passed
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

Successfully merging this pull request may close these issues.

Race in worker_loop SIGSEGV on OCaml 5 due to missing SA_ONSTACK
2 participants