Skip to content

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

Closed

Description

@vicuna

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".

Activity

  1. vicuna commented on May 17, 2018

    @vicuna
    Author

    Comment author: @xavierleroy

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

  2. vicuna commented on May 17, 2018

    @vicuna
    Author

    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.

  3. vicuna commented on May 17, 2018

    @vicuna
    Author

    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.
    
  4. vicuna commented on May 17, 2018

    @vicuna
    Author

    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.

  5. vicuna commented on May 17, 2018

    @vicuna
    Author

    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.

  6. vicuna commented on May 17, 2018

    @vicuna
    Author

    Comment author: @xavierleroy

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

  7. vicuna commented on May 21, 2018

    @vicuna
    Author

    Comment author: @xavierleroy

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

  8. vicuna commented on May 23, 2018

    @vicuna
    Author

    Comment author: @xavierleroy

    Merged #1790

  9. added this to the 4.07.0 milestone on Mar 14, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions