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

Backtrace-friendly finalizers #7163

Closed
vicuna opened this issue Mar 4, 2016 · 3 comments
Closed

Backtrace-friendly finalizers #7163

vicuna opened this issue Mar 4, 2016 · 3 comments

Comments

@vicuna
Copy link

vicuna commented Mar 4, 2016

Original bug ID: 7163
Reporter: AltGr
Status: closed (set by @mshinwell on 2016-12-08T09:56:28Z)
Resolution: not a bug
Priority: normal
Severity: feature
Version: 4.02.3
Target version: 4.03.1+dev
Category: runtime system and C interface
Monitored by: @diml @hcarty

Bug description

A common pattern for finalizing is:

let _ =
try ...
with e ->
finalize ();
raise e

The issue here is that finalize might use exceptions internally. OCaml generally handles this, but if finalize uses the same exception as e (e.g. Not_found), the backtrace gets mangled. There is indeed no way that I know of for OCaml to find which was the "right" instance of Not_found.

Since we now have Printexc.get_raw_backtrace available in the stdlib, a possible solution interface-wise would be to also provide a function raise_with_backtrace:

let _ =
try ...
with e ->
let bt = Printexc.get_raw_backtrace () in
finalize ();
raise_with_backtrace bt e

It feels weird to have to use Printexc for this (well, it already has get_callstack), but you get the idea. Ideally try..with would allow to match both exception and backtrace, but that's much too invasive.

Is anything like this already available ? Would it be suitable ?

If desired, I could have a look at it.

@vicuna
Copy link
Author

vicuna commented Mar 4, 2016

Comment author: @gasche

This has been discussed recently and there are patches proposed to allow to do something like this. See:

#293
#358
#374
#378

In the end we didn't include the user-facing features in 4.03, but it's up to discussion for future releases.

PS: I found the name "finalizer" a bit confusing as I usually think of them for "GC finalizers" only, here it's "the stuff you put in finally blocks". Maybe "cleanup code" or "teardown code"?

@vicuna
Copy link
Author

vicuna commented Mar 4, 2016

Comment author: AltGr

Great, I wasn't aware of it, thanks, and sorry for the duplicate!

@vicuna
Copy link
Author

vicuna commented Dec 8, 2016

Comment author: @mshinwell

I think this can be closed. See the various GPRs above.

@vicuna vicuna closed this as completed Dec 8, 2016
@vicuna vicuna added the stdlib label Mar 14, 2019
@vicuna vicuna added this to the 4.03.1 milestone Mar 14, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant