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

Calling exit in at_exit callback results in infinite loop. #7178

Closed
vicuna opened this Issue Mar 11, 2016 · 5 comments

Comments

Projects
None yet
2 participants
@vicuna
Copy link
Collaborator

vicuna commented Mar 11, 2016

Original bug ID: 7178
Reporter: @dbuenzli
Assigned to: @xavierleroy
Status: resolved (set by @xavierleroy on 2018-05-23T18:00:20Z)
Resolution: fixed
Priority: normal
Severity: minor
Version: 4.02.3
Fixed in version: 4.07.0+dev/beta2/rc1/rc2
Category: standard library
Related to: #7796
Monitored by: @gasche @diml @hcarty @dbuenzli

Bug description

E.g. in C the following program will exit with 1.

#include <stdio.h>
#include <stdlib.h>

void do_exit (void)
{
puts ("Goodbye, world!\n");
exit (1);
}

int main (int argc, char *argv[])
{
atexit (do_exit);
puts ("Hello, world!\n");
return 0;
}

Steps to reproduce

ocaml

at_exit (fun () -> exit 1);;

  • : unit = ()

^D

Fatal error: exception Stack overflow
Raised by primitive operation at file "pervasives.ml", line 515, characters 30-3

@vicuna

This comment has been minimized.

Copy link
Collaborator Author

vicuna commented Mar 14, 2016

Comment author: @mshinwell

According to "man atexit" on my system, the POSIX.1-2001 behaviour of calling exit within an atexit handler is undefined. Indeed it suggests an infinite loop is a possible outcome. I think the correct way of exiting from an atexit function is probably to call _exit.

@vicuna

This comment has been minimized.

Copy link
Collaborator Author

vicuna commented Mar 14, 2016

Comment author: @dbuenzli

Indeed http://pubs.opengroup.org/onlinepubs/9699919799/functions/atexit.html requires from an atexit function either to return or to call _exit or "a function that causes abnormal termination". So a binding to _exit or abort (preferably the former) would be nice.

@vicuna

This comment has been minimized.

Copy link
Collaborator Author

vicuna commented May 10, 2016

Comment author: @dbuenzli

Note that this is not a binding issue since everything is handled in OCaml land. If we leave threading issues aside, the at_exit function here:

ocaml/stdlib/pervasives.ml

Lines 519 to 521 in 393f068

let exit retcode =
do_at_exit ();
sys_exit retcode

let exit retcode =
do_at_exit ();
sys_exit retcode

Could be replaced by:

let exit_code = ref None
let exit retcode = match !exit_code with
| Some _ when retcode = 0 -> ()
| Some _ -> exit_code := Some retcode
| None ->
exit_code := Some retcode;
do_at_exit ();
match !exit_code with
| None -> assert false
| Some retcode -> sys_exit retcode

to solve the problem. The semantics is that the last non-zero [exit] call gives the final exit code of the program.

@vicuna

This comment has been minimized.

Copy link
Collaborator Author

vicuna commented Jul 7, 2016

Comment author: @dbuenzli

Fix in #675

Basically along the lines mentioned above except I had to introduce an exception because exit's signature is int -> 'a, not int -> unit as the code above assumed.

@vicuna

This comment has been minimized.

Copy link
Collaborator Author

vicuna commented May 23, 2018

Comment author: @xavierleroy

The merge of #1790 fixes this issue, even if I didn't realize when I committed it. Marking this MPR as resolved in 4.07.

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.