You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Calling exit in at_exit callback results in infinite loop. #7178
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
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.
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.
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.
Activity
vicuna commentedon 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 commentedon 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 commentedon 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
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 commentedon 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 commentedon 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.