Re: Handling signals in D

2018-12-26 Thread Basile B via Digitalmars-d-learn
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("Ooo 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, );
```

Any suggestion would be very much appreciated.

Norbert


The 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


Handling signals in D

2018-12-25 Thread Norbert Preining via Digitalmars-d-learn

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  0x in ?? ()
#1  0x7faf58f7e967 in std.net.curl.HTTP.Impl.~this() ()
   from /usr/lib/x86_64-linux-gnu/libphobos2-ldc-shared.so.82
#2  0x7faf58ebbce9 in 
_D6object__T7destroyTS3std3net4curl4HTTP4ImplZQBiFKQBeZv ()

   from /usr/lib/x86_64-linux-gnu/libphobos2-ldc-shared.so.82
#3  0x7faf58fce020 in 
_D3std8typecons__T10RefCountedTSQBe3net4curl4HTTP4ImplVEQCcQCb24RefCountedAutoInitializei1ZQCv6__dtorMFZv () from /usr/lib/x86_64-linux-gnu/libphobos2-ldc-shared.so.82
#4  0x7faf58c8d2ae in rt_finalize2 () from 
/usr/lib/x86_64-linux-gnu/libdruntime-ldc-shared.so.82
#5  0x7faf58c75af5 in 
_D2gc4impl12conservativeQw15SmallObjectPool13runFinalizersMFNbxAvZv ()

   from /usr/lib/x86_64-linux-gnu/libdruntime-ldc-shared.so.82
#6  0x7faf58c746cb in 
_D2gc4impl12conservativeQw3Gcx13runFinalizersMFNbxAvZv ()

   from /usr/lib/x86_64-linux-gnu/libdruntime-ldc-shared.so.82
#7  0x7faf58c7476f in 
_D2gc4impl12conservativeQw14ConservativeGC__T9runLockedS_DQCeQCeQCcQCnQBs13runFinalizersMFNbxAvZ2goFNbPSQDyQDyQDwQEh3GcxxQBcZvS_DQExQExQEvQFg9otherTimelS_DQFxQFxQFvQGg9numOtherslTQCzTxQDnZQFnMFNbKQDqKxQEeZv ()

   from /usr/lib/x86_64-linux-gnu/libdruntime-ldc-shared.so.82
#8  0x7faf58c75633 in 
_DThn16_2gc4impl12conservativeQw14ConservativeGC13runFinalizersMFNbxAvZv ()

   from /usr/lib/x86_64-linux-gnu/libdruntime-ldc-shared.so.82
#9  0x7faf58c949bc in _d_dso_registry () from 
/usr/lib/x86_64-linux-gnu/libdruntime-ldc-shared.so.82

#10 0x561068f9b3b6 in ldc.register_dso ()
#11 0x7faf59684736 in _dl_fini () at dl-fini.c:138
#12 0x7faf5887de3c in __run_exit_handlers (status=0, 
listp=0x7faf589ff718 <__exit_funcs>,
run_list_atexit=run_list_atexit@entry=true, 
run_dtors=run_dtors@entry=true) at exit.c:108
#13 0x7faf5887df6a in __GI_exit (status=) at 
exit.c:139

#14 
```
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("Ooo 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, );
```

Any suggestion would be very much appreciated.

Norbert