Skip to content

Improve the semantics of asynchronous exceptions [old version] #765

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

Closed
wants to merge 1 commit into from

Conversation

mshinwell
Copy link
Collaborator

This PR provides a way of making explicit the places where asynchronous exceptions may be raised. This in particular stops exceptions arising from allocation points and thus matches up with the model used by Flambda 2. One consequence is that the addition of safepoints will then work correctly with Flambda 2. This supercedes #194.

The asynchronous exceptions are now nicely wrapped up as follows:

exception Stack_overflow
exception Finaliser_raised of exn
exception Memprof_callback_raised of exn
exception Signal_handler_raised of exn

Uses of Out_of_memory that were asynchronous exceptions (there were some uses that were not) now terminate the program.

There is currently a new function Sys.with_async_exns that can be used to identify a point at which asynchronous exceptions should be emitted. There is also an experiment to implement the "bracket pattern" like in Haskell; see Sys.bracket. There is a file containing a couple of tests which will need to be much enhanced.

Asynchronous exceptions are funneled via a new exception pointer in the domain state, which points at normal trap frames, forming a second exception stack. The raising of asynchronous exceptions skips over traps that weren't inserted by with_async_exns. The second exception stack, save for the head which is in the domain state, is just held in what are effectively local variables in the caml_start_program / caml_callback code (albeit written in assembler).

Note that Stack_overflow is special and can be raised directly from a signal handler.

Joint work with @stedolan and @lpw25 .

@mshinwell mshinwell added to upstream PR should be sent to upstream OCaml flambda2 Prerequisite for, or part of, flambda2 4.13/4.14 merge Backports of 4.13 and 4.14 changes labels Aug 12, 2022
@mshinwell mshinwell force-pushed the bracket branch 2 times, most recently from ee9fd7c to 4957807 Compare August 12, 2022 09:58
@mshinwell mshinwell marked this pull request as draft August 12, 2022 11:42
@mshinwell mshinwell force-pushed the bracket branch 2 times, most recently from 0d395f3 to 8078a47 Compare August 12, 2022 13:03
@mshinwell
Copy link
Collaborator Author

mshinwell commented Aug 12, 2022

There is one particular point worth noting: this PR, when compiling to native code, currently aims to deliver async exceptions only to handlers registered using Sys.with_async_exns. I think it's cleaner and easier to understand this way. (The alternative is that async exceptions could be permitted to be raised from function calls, for example.)
I also tend to think that maybe the term "asynchronous exceptions" shouldn't be used, in favour of something else, which would emphasise that this really is something different from normal exceptions. "Panic" has been suggested a bit like Rust, although I think the version here raises these in more restricted circumstances.

Regarding bytecode, async exns would end up being delivered to normal exception handlers, but we could still support masking.

@mshinwell
Copy link
Collaborator Author

Note on backward compatibility: currently this isn't backwards-compatible, since it requires Break to be caught as Signal_handler_raised Break. This is probably ok, since code would need to be updated to use Sys.with_async_exns anyway.

@mshinwell mshinwell changed the title Improve the semantics of asynchronous exceptions Improve the semantics of asynchronous exceptions [old version] Aug 31, 2022
@mshinwell
Copy link
Collaborator Author

Superceded by #802

@mshinwell mshinwell closed this Aug 31, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
4.13/4.14 merge Backports of 4.13 and 4.14 changes flambda2 Prerequisite for, or part of, flambda2 to upstream PR should be sent to upstream OCaml
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant