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

"at_exit" function run twice with cleanup-at-exit runtime option #7796

Closed
vicuna opened this Issue May 17, 2018 · 8 comments

Comments

Projects
None yet
2 participants
@vicuna
Copy link
Collaborator

vicuna commented May 17, 2018

Original bug ID: 7796
Reporter: @xavierleroy
Assigned to: @xavierleroy
Status: resolved (set by @xavierleroy on 2018-05-23T17:51:00Z)
Resolution: fixed
Priority: normal
Severity: major
Platform: x86-64
OS: Ubuntu Linux 16.04
Version: 4.07.0+dev/beta2/rc1/rc2
Target version: 4.07.0+dev/beta2/rc1/rc2
Fixed in version: 4.07.0+dev/beta2/rc1/rc2
Category: runtime system and C interface
Related to: #7178 #7253
Monitored by: @nojb @gasche @dbuenzli

Bug description

When "c=1" is put in OCAMLRUNPARAM, text printed to standard output in an at_exit function ends up written twice to standard output.

~/caml/devel$ cat foo.ml
let _ =
at_exit (fun () -> print_endline "\nAll tests succeeded.")
~/caml/devel$ boot/ocamlrun ./ocamlc -I ./stdlib -o foo.byte foo.ml
~/caml/devel$ byterun/ocamlrun foo.byte

All tests succeeded.
~/caml/devel$ OCAMLRUNPARAM=c=1 byterun/ocamlrun foo.byte

All tests succeeded.

All tests succeeded.
~/caml/devel$

Among other ill effects, this causes many tests in the test suite to fail when run with OCAMLRUNPARAM="c=1".

@vicuna

This comment has been minimized.

Copy link
Collaborator Author

vicuna commented May 17, 2018

Comment author: @xavierleroy

It seems that all "at_exit" functions are run TWICE if cleanup-at-exit is set. This is a serious mistake.

@vicuna

This comment has been minimized.

Copy link
Collaborator Author

vicuna commented May 17, 2018

Comment author: @nojb

at_exit functions are called from caml_shutdown

https://github.com/ocaml/ocaml/blob/trunk/byterun/startup_aux.c#L159

(caml_shutdown is called if cleanup_on_exit is set), but if the program finished normally, they would also have been called via std_exit.

@vicuna

This comment has been minimized.

Copy link
Collaborator Author

vicuna commented May 17, 2018

Comment author: @nojb

The change to call at_exit functions from caml_shutdown was thread-related:

commit a6cad36
Author: Max Mouratov mmouratov@gmail.com
Date: Sun Nov 29 23:26:18 2015 +0500

runtime: handling "at_exit" in caml_shutdown

Without this, the Thread machinery didn't shut down correctly.
@vicuna

This comment has been minimized.

Copy link
Collaborator Author

vicuna commented May 17, 2018

Comment author: @xavierleroy

I agree that caml_shutdown should run the at-exit functions. This is especially true if the OCaml code is embedded as a library in a larger program. I also think that we should not run the at-exit functions twice, and will propose a GPR for that.

@vicuna

This comment has been minimized.

Copy link
Collaborator Author

vicuna commented May 17, 2018

Comment author: @dbuenzli

Since people might look into the whole machinery I just want to relate this to #7253 and #7178 and associated aborted GPR.

@vicuna

This comment has been minimized.

Copy link
Collaborator Author

vicuna commented May 17, 2018

Comment author: @xavierleroy

Proposed fix at #1783 .
It does not address the other issues mentioned by @dbuenzli, but it an improvement nonetheless.

@vicuna

This comment has been minimized.

Copy link
Collaborator Author

vicuna commented May 21, 2018

Comment author: @xavierleroy

Alternate, improved fix at #1790
It also addresses #7253.

@vicuna

This comment has been minimized.

Copy link
Collaborator Author

vicuna commented May 23, 2018

Comment author: @xavierleroy

Merged #1790

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.