digitalmars.D.learn - Handling signals in D
- Norbert Preining (65/65) Dec 25 2018 Hello everyone,
- Basile B (6/32) Dec 26 2018 The technique to use here is to wrap the non-gc code in a
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 from /usr/lib/x86_64-linux-gnu/libphobos2-ldc-shared.so.82 _D6object__T7destroyTS3std3net4curl4HTTP4ImplZQBiFKQBeZv () from /usr/lib/x86_64-linux-gnu/libphobos2-ldc-shared.so.82 _D3std8typecons__T10RefCountedTSQBe3net4curl4HTTP4ImplVEQCcQCb24RefCountedAutoInitial zei1ZQCv6__dtorMFZv () from /usr/lib/x86_64-linux-gnu/libphobos2-ldc-shared.so.82 /usr/lib/x86_64-linux-gnu/libdruntime-ldc-shared.so.82 _D2gc4impl12conservativeQw15SmallObjectPool13runFinalizersMFNbxAvZv () from /usr/lib/x86_64-linux-gnu/libdruntime-ldc-shared.so.82 _D2gc4impl12conservativeQw3Gcx13runFinalizersMFNbxAvZv () from /usr/lib/x86_64-linux-gnu/libdruntime-ldc-shared.so.82 _D2gc4impl12conservativeQw14ConservativeGC__T9runLockedS_DQCeQCeQCcQCnQBs13runFinalizersMFNbxAvZ2goFNbPSQDyQDyQDwQEh3GcxxQBcZvS_DQExQExQEvQFg9otherTimelS_DQFxQFxQFvQGg9numOtherslTQCzTxQD ZQFnMFNbKQDqKxQEeZv () from /usr/lib/x86_64-linux-gnu/libdruntime-ldc-shared.so.82 _DThn16_2gc4impl12conservativeQw14ConservativeGC13runFinalizersMFNbxAvZv () from /usr/lib/x86_64-linux-gnu/libdruntime-ldc-shared.so.82 /usr/lib/x86_64-linux-gnu/libdruntime-ldc-shared.so.82 listp=0x7faf589ff718 <__exit_funcs>, run_list_atexit=run_list_atexit entry=true, run_dtors=run_dtors entry=true) at exit.c:108 exit.c:139 ``` 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
Dec 25 2018
On Wednesday, 26 December 2018 at 03:19:45 UTC, Norbert Preining wrote: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. [...] 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. NorbertThe technique to use here is to wrap the non-gc code in a delegate, cast it as a nogc delegate and call it, see [1] for more details. [1] https://p0nce.github.io/d-idioms/#Bypassing- nogc
Dec 26 2018