Recent

Author Topic: [SOLVED] StopAccepting() method not stopping TSocketServer.  (Read 2468 times)

zamronypj

  • Full Member
  • ***
  • Posts: 140
    • Fano Framework, Free Pascal web application framework
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?
« Last Edit: July 13, 2019, 10:55:50 am by zamronypj »
Fano Framework, Free Pascal web application framework https://fanoframework.github.io
Apache module executes Pascal program like scripting language https://zamronypj.github.io/mod_pascal/
Github https://github.com/zamronypj

zamronypj

  • Full Member
  • ***
  • Posts: 140
    • Fano Framework, Free Pascal web application framework
Re: StopAccepting() method not stopping TSocketServer.
« Reply #1 on: July 13, 2019, 10:42:46 am »
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/SocketSvrImpl.pas
https://github.com/fanoframework/fano/blob/master/src/Libs/Socket/UnixSocketSvrImpl.pas
https://github.com/fanoframework/fano/blob/master/src/Libs/Socket/InetSocketSvrImpl.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.
« Last Edit: July 30, 2019, 08:36:49 am by zamronypj »
Fano Framework, Free Pascal web application framework https://fanoframework.github.io
Apache module executes Pascal program like scripting language https://zamronypj.github.io/mod_pascal/
Github https://github.com/zamronypj

GDean

  • Newbie
  • Posts: 5
Re: StopAccepting() method not stopping TSocketServer.
« Reply #2 on: August 09, 2019, 05:51:23 am »
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/SocketSvrImpl.pas
https://github.com/fanoframework/fano/blob/master/src/Libs/Socket/UnixSocketSvrImpl.pas
https://github.com/fanoframework/fano/blob/master/src/Libs/Socket/InetSocketSvrImpl.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

  • Hero Member
  • *****
  • Posts: 4217
Re: [SOLVED] StopAccepting() method not stopping TSocketServer.
« Reply #3 on: August 09, 2019, 06:59:33 am »
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?

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.
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

zamronypj

  • Full Member
  • ***
  • Posts: 140
    • Fano Framework, Free Pascal web application framework
Re: [SOLVED] StopAccepting() method not stopping TSocketServer.
« Reply #4 on: August 09, 2019, 12:37:54 pm »
@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.
Fano Framework, Free Pascal web application framework https://fanoframework.github.io
Apache module executes Pascal program like scripting language https://zamronypj.github.io/mod_pascal/
Github https://github.com/zamronypj

 

TinyPortal © 2005-2018