[SOLVED] StopAccepting() method not stopping TSocketServer. lazarus / 38-Networking and Web Programming ---------------------------------------------------------------------- zamronypj · 2019-07-03 13:13 When use blocking, StartAccepting() method of TSocketServer will not exit until its internal variable FAccepting is false. Calling StopAccepting outside this loop is useless. All sample applications, for example https://github.com/graemeg/freepascal/blob/master/packages/fcl- base/examples/dsocksvr.pp, call StopAccepting inside OnConnect(). However, OnConnect() will be called only if there are data available. If there is no data available, then, stopAccepting may never be called. We can force to terminate application by sending SIGTERM/SIGINT signal, but that cause application is terminated immediately without opportunity to do some cleanup (closing open files, deleting temporary files etc). How to gracefully shutdown TSocketServer? ---------------------------------------------------------------------- zamronypj · 2019-07-13 08:42 I solved it by implementing my own socket server (Unix only). Just in case anyone interested, here is the link https://github.com/fanoframework/fano/blob/master/src/Libs/Socket/Sock etSvrImpl.pas https://github.com/fanoframework/fano/blob/master/src/Libs/Socket/Unix SocketSvrImpl.pas https://github.com/fanoframework/fano/blob/master/src/Libs/Socket/Inet SocketSvrImpl.pas I need to be able to gracefully exiting, to be able to view heaptrc output to analyze if there are any memory leak. Any feedback for improvement are welcome. ---------------------------------------------------------------------- GDean · 2019-08-09 03:51 Quote from: zamronypj on July 13, 2019, 10:42:46 amI solved it by implementing my own socket server (Unix only). Just in case anyone interested, here is the link https://github.com/fanoframework/fano/blob/master/src/Libs/Socket/Sock etSvrImpl.pas https://github.com/fanoframework/fano/blob/master/src/Libs/Socket/Unix SocketSvrImpl.pas https://github.com/fanoframework/fano/blob/master/src/Libs/Socket/Inet SocketSvrImpl.pas I need to be able to gracefully exiting, to be able to view heaptrc output to analyze if there are any memory leak. Any feedback for improvement are welcome. SocketSvrImpl has RunnableIntf, RunnableWithDataNotifIntf, DataAvailListenerIntf, StreamAdapterIntf in uses can you upload those to GitHub thanks gd ---------------------------------------------------------------------- lucamar · 2019-08-09 04:59 Quote from: zamronypj on July 03, 2019, 03:13:43 pmWe can force to terminate application by sending SIGTERM/SIGINT signal, but that cause application is terminated immediately without opportunity to do some cleanup (closing open files, deleting temporary files etc). How to gracefully shutdown TSocketServer? Didn't see this before, sorry. For some reason the default application object doesn't react to signals but you can use fpSigAction to add your own signal handler to react to almost any signal, the exceptions being SIGKILL and SIGSTOP. I see you already solved it but for completeness sake I have attached a program I did to test this. For your use case note particularly the "special" treatment of SIGTERM in HandleSignal / GracefulExit. Regards. ---------------------------------------------------------------------- zamronypj · 2019-08-09 10:37 @GDean, they are all in github. It is implemented to fit Fano Framework requirement so it may not fit your use case. @lucamar yes, fpSigAction() that is what I end up using in my own implementation.