Handling signals in D

Norbert Preining norbert at preining.info
Wed Dec 26 03:19:45 UTC 2018


Hello everyone,
we are writing a program that synchronizes the OneDrive cloud 
service with the local computer, and run it as daemon in the 
background. To ensure proper database shutdown on exit, we need 
to install signal handlers that react to SIGINT etc.

The problem now is that at the same time we use 
std.net.curl.Curl, and that has some kind of bug in the shutdown 
routine when it is called during program termination. One always 
needs to manually call .shutdown, otherwise the program dumps 
core:
```
(gdb) bt
#0  0x0000000000000000 in ?? ()
#1  0x00007faf58f7e967 in std.net.curl.HTTP.Impl.~this() ()
    from /usr/lib/x86_64-linux-gnu/libphobos2-ldc-shared.so.82
#2  0x00007faf58ebbce9 in 
_D6object__T7destroyTS3std3net4curl4HTTP4ImplZQBiFKQBeZv ()
    from /usr/lib/x86_64-linux-gnu/libphobos2-ldc-shared.so.82
#3  0x00007faf58fce020 in 
_D3std8typecons__T10RefCountedTSQBe3net4curl4HTTP4ImplVEQCcQCb24RefCountedAutoInitializei1ZQCv6__dtorMFZv () from /usr/lib/x86_64-linux-gnu/libphobos2-ldc-shared.so.82
#4  0x00007faf58c8d2ae in rt_finalize2 () from 
/usr/lib/x86_64-linux-gnu/libdruntime-ldc-shared.so.82
#5  0x00007faf58c75af5 in 
_D2gc4impl12conservativeQw15SmallObjectPool13runFinalizersMFNbxAvZv ()
    from /usr/lib/x86_64-linux-gnu/libdruntime-ldc-shared.so.82
#6  0x00007faf58c746cb in 
_D2gc4impl12conservativeQw3Gcx13runFinalizersMFNbxAvZv ()
    from /usr/lib/x86_64-linux-gnu/libdruntime-ldc-shared.so.82
#7  0x00007faf58c7476f in 
_D2gc4impl12conservativeQw14ConservativeGC__T9runLockedS_DQCeQCeQCcQCnQBs13runFinalizersMFNbxAvZ2goFNbPSQDyQDyQDwQEh3GcxxQBcZvS_DQExQExQEvQFg9otherTimelS_DQFxQFxQFvQGg9numOtherslTQCzTxQDnZQFnMFNbKQDqKxQEeZv ()
    from /usr/lib/x86_64-linux-gnu/libdruntime-ldc-shared.so.82
#8  0x00007faf58c75633 in 
_DThn16_2gc4impl12conservativeQw14ConservativeGC13runFinalizersMFNbxAvZv ()
    from /usr/lib/x86_64-linux-gnu/libdruntime-ldc-shared.so.82
#9  0x00007faf58c949bc in _d_dso_registry () from 
/usr/lib/x86_64-linux-gnu/libdruntime-ldc-shared.so.82
#10 0x0000561068f9b3b6 in ldc.register_dso ()
#11 0x00007faf59684736 in _dl_fini () at dl-fini.c:138
#12 0x00007faf5887de3c in __run_exit_handlers (status=0, 
listp=0x7faf589ff718 <__exit_funcs>,
     run_list_atexit=run_list_atexit at entry=true, 
run_dtors=run_dtors at entry=true) at exit.c:108
#13 0x00007faf5887df6a in __GI_exit (status=<optimized out>) at 
exit.c:139
#14 <signal handler called>
```
Now, unfortunately the call to the shutdown procedure is not 
possible in `@nogc`, so I am a bit at loss how to deal with all 
this.

The code I am using is:
```
extern(C) @nogc @system void exitHandler(int value) {
        printf("Ooohhhh got %d\n", value);
        // workaround for segfault in std.net.curl.Curl.shutdown() 
on exit
        oneDrive.http.shutdown();
        exit(0);
}
```
and in `main` before entering the loop:
```
signal(SIGINT, &exitHandler);
```

Any suggestion would be very much appreciated.

Norbert




More information about the Digitalmars-d-learn mailing list