posixd

How to exit from a D program gracefully when ctrl+c happens?


I would like to shut down a vibe.d application gracefully by stopping the eventloop.

import vibe.vibe;
import core.sys.posix.signal;

void main()
{
  enum SIGINT = 2;
  signal(SIGINT, &stopapp);

  auto settings = new HTTPServerSettings;
  settings.port = 8080;
  settings.bindAddresses = ["::1", "127.0.0.1"];
  listenHTTP(settings, &hello);

  logInfo("Please open http://127.0.0.1:8080/ in your browser.");
  runApplication();

}

void hello(HTTPServerRequest req, HTTPServerResponse res)
{
  res.writeBody("Hello, World!");
}

void stopapp(int value){
  logInfo("Stopping app...");
  exitEventLoop();
}

Unfortunately this does not work:

source/app.d(7,9): Error: function core.stdc.signal.signal(int sig, extern (C) void function(int) nothrow @nogc @system func) is not callable using argument types (int, void function(int value))
source/app.d(7,9):        cannot pass argument & stopapp of type void function(int value) to parameter extern (C) void function(int) nothrow @nogc @system func
dmd failed with exit code 1.

Is there a simple library that does this?


Solution

  • signal is a C function. For a C function to call a D function, the D function should be marked as extern(C) signalHandler().

    import  std.stdio;
    
    extern(C) void signal(int sig, void function(int) );
    
    // Our handler, callable by C
    extern(C) void handle(int sig) {
        writeln("Signal:",sig);
    }
    
    void main()
    {
        enum SIGINT = 2; // OS-specific
    
        signal(SIGINT,&handle);
        writeln("Hello!");
        readln();
        writeln("End!");
    }
    

    As for your vibe.d example, vibe.d handles the SIGINT by itself. This should work:

    import vibe.vibe;
    
    void main()
    {
        auto settings = new HTTPServerSettings;
        settings.port = 8080;
        settings.bindAddresses = ["::1", "127.0.0.1"];
        listenHTTP(settings, &hello);
    
        logInfo("Please open http://127.0.0.1:8080/ in your browser.");
        runApplication();
    }
    
    void hello(HTTPServerRequest req, HTTPServerResponse res)
    {
        res.writeBody("Hello, World!");
    }
    

    Running this and pressing C-c.

    09:21:59 ~/code/d/stackoverflow/q1
    $ ./q1
    [main(----) INF] Listening for requests on http://[::1]:8080/
    [main(----) INF] Listening for requests on http://127.0.0.1:8080/
    [main(----) INF] Please open http://127.0.0.1:8080/ in your browser.
    ^C[main(----) INF] Received signal 2. Shutting down.
    Warning (thread: main): leaking eventcore driver because there are still active handles
    FD 6 (streamListen)
    FD 7 (streamListen)
    Warning (thread: main): leaking eventcore driver because there are still active handles
    FD 6 (streamListen)
    FD 7 (streamListen)
    09:22:04 ~/code/d/stackoverflow/q1
    $