Skip to content

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

Closed

Description

@vicuna

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

Activity

  1. vicuna commented on Mar 14, 2016

    @vicuna
    Author

    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.

  2. vicuna commented on Mar 14, 2016

    @vicuna
    Author

    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.

  3. vicuna commented on May 10, 2016

    @vicuna
    Author

    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.

  4. vicuna commented on Jul 7, 2016

    @vicuna
    Author

    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.

  5. vicuna commented on May 23, 2018

    @vicuna
    Author

    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

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions