www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Clean process exit

reply "Dmitri" <deemok gmail.com> writes:
I'm a D noob, so my question is perhaps naive. At work, we 
develop servers that are 24/7 and as such they don't have a clean 
exit path. Sometimes, however, I'd like to troubleshoot a process 
and use valgrind, in which case I have to come with a way of 
forcing a program's exit.

I know I can control (some) valgrind tools from outside by 
forcing a log, but it is not always the case. So more often than 
not, I end up adding some sort of debugging code to be able to 
force an exit from outside - in which case it is as simple as 
calling stdlib's exit. This poses a problem when the exit is 
called from a thread other than the main one, since it did not 
have a _d_dos_registry on the way "in", but it gets called on the 
way "out" and crashes when the _tlsRanges is modified.

My questions are,
   * is it legit to want to terminate not from the main thread?
   * if not, what is the recommended simpler way of doing it?
May 07 2015
next sibling parent Jacob Carlborg <doob me.com> writes:
On 2015-05-07 18:15, Dmitri wrote:
 I'm a D noob, so my question is perhaps naive. At work, we develop
 servers that are 24/7 and as such they don't have a clean exit path.
 Sometimes, however, I'd like to troubleshoot a process and use valgrind,
 in which case I have to come with a way of forcing a program's exit.

 I know I can control (some) valgrind tools from outside by forcing a
 log, but it is not always the case. So more often than not, I end up
 adding some sort of debugging code to be able to force an exit from
 outside - in which case it is as simple as calling stdlib's exit. This
 poses a problem when the exit is called from a thread other than the
 main one, since it did not have a _d_dos_registry on the way "in", but
 it gets called on the way "out" and crashes when the _tlsRanges is
 modified.

 My questions are,
    * is it legit to want to terminate not from the main thread?
    * if not, what is the recommended simpler way of doing it?
I would recommend trying to avoid using "exit" to terminate the application. Something is keeping the process alive since it's running 24/7, like an endless loop. Try adding a condition to this loop then have a way to communicate this condition is changed to the process. One way of doing this would be to send a signal to the process which it handles and set this condition to false. The loop will exit and the process terminates in the usual way by running to the end. -- /Jacob Carlborg
May 07 2015
prev sibling next sibling parent reply "Vladimir Panteleev" <vladimir thecybershadow.net> writes:
On Thursday, 7 May 2015 at 16:15:29 UTC, Dmitri wrote:
 My questions are,
   * is it legit to want to terminate not from the main thread?
   * if not, what is the recommended simpler way of doing it?
Not the simplest, but I think the best way for an event-driven network application to exit is, upon receiving SIGTERM, to close its listening sockets and long-lived connections, allowing the event loop to exit naturally as it runs out of event sources to listen on. Since the signal may be received by an arbitrary thread, you may need to notify the event loop thread. How you do this depends on your event loop, but a simple way is to write to one end of a socket pair. As an example, I have this approach implemented here: https://github.com/CyberShadow/ae/blob/master/net/shutdown.d
May 07 2015
parent "Dmitri" <deemok gmail.com> writes:
I was afraid I would not have it easy :o)
Thanks for your suggestions, though.

-dmitri.
May 08 2015
prev sibling parent Steven Schveighoffer <schveiguy yahoo.com> writes:
On 5/7/15 12:15 PM, Dmitri wrote:
 I'm a D noob, so my question is perhaps naive. At work, we develop
 servers that are 24/7 and as such they don't have a clean exit path.
 Sometimes, however, I'd like to troubleshoot a process and use valgrind,
 in which case I have to come with a way of forcing a program's exit.

 I know I can control (some) valgrind tools from outside by forcing a
 log, but it is not always the case. So more often than not, I end up
 adding some sort of debugging code to be able to force an exit from
 outside - in which case it is as simple as calling stdlib's exit. This
 poses a problem when the exit is called from a thread other than the
 main one, since it did not have a _d_dos_registry on the way "in", but
 it gets called on the way "out" and crashes when the _tlsRanges is
 modified.

 My questions are,
    * is it legit to want to terminate not from the main thread?
    * if not, what is the recommended simpler way of doing it?
In all my server type multi-threaded apps, I do something like this: while(true) => while(!exiting) // exiting is some global bool waitForInput(forever) => waitForInput(1000ms) Then in the signal handler, I set exiting = true, nothing else. All the threads exit gracefully, and the main thread joins all the others. Max time to shut down is 1 second. Note that one should always handle shutdown when asked by the OS (either through signals or whatever mechanism the OS uses) so the OS can gracefully shut down. -Steve
May 12 2015