Recent

Author Topic: Gracefully shutdown TFPHTTPServer in a thread  (Read 8522 times)

tudi_x

  • Hero Member
  • *****
  • Posts: 532
Gracefully shutdown TFPHTTPServer in a thread
« on: July 20, 2017, 04:31:45 pm »
hi All,
please see attached and screen capture.
i am getting an exception ' Could not accept a client connection on socket' when stopping the TFPHTTPServer server. also there are unfreed memory blocks.
please advise what i am missing.

thank you
Lazarus 2.0.2 64b on Debian LXDE 10

Thaddy

  • Hero Member
  • *****
  • Posts: 14201
  • Probably until I exterminate Putin.
Re: Gracefully shutdown TFPHTTPServer in a thread
« Reply #1 on: July 20, 2017, 05:04:56 pm »
If port 148 is not available.....
Specialize a type, not a var.

tudi_x

  • Hero Member
  • *****
  • Posts: 532
Re: Gracefully shutdown TFPHTTPServer in a thread
« Reply #2 on: July 20, 2017, 05:10:33 pm »
i think tcp port 148 is the jargon port according to IANA.
so you are saying that socket 148 is actually port 148?

would port 148 be related to port 8001 on which i am starting the HTTP server?
Lazarus 2.0.2 64b on Debian LXDE 10

rvk

  • Hero Member
  • *****
  • Posts: 6111
Re: Gracefully shutdown TFPHTTPServer in a thread
« Reply #3 on: July 20, 2017, 05:24:51 pm »
For me it's hanging again after _T.Server.Active := False;
It seems it doesn't cancel the server.
I thought this was solved but somehow with console and all these threads it's not working again.

(A few days ago the same code with Server.Active := false worked fine, sighhh)

rvk

  • Hero Member
  • *****
  • Posts: 6111
Re: Gracefully shutdown TFPHTTPServer in a thread
« Reply #4 on: July 20, 2017, 05:31:37 pm »
The exception is probably correct:
10004:
The WSACancelBlockingCall function has been removed in compliance with the Windows Sockets 2 specification, revision 2.2.0.
https://msdn.microsoft.com/en-us/library/windows/desktop/ms741547(v=vs.85).aspx

So when you set Server.Active := false the code calls CancelBlockingCall and that generates an exception.
You can handle that exception as if it was correct (if the code was 10004 of course).


tudi_x

  • Hero Member
  • *****
  • Posts: 532
Re: Gracefully shutdown TFPHTTPServer in a thread
« Reply #5 on: July 20, 2017, 05:37:32 pm »
thank you rvk. i was not aware of the WSACancelBlockingCall function.

please advise on the unfreed memory options...
Lazarus 2.0.2 64b on Debian LXDE 10

rvk

  • Hero Member
  • *****
  • Posts: 6111
Re: Gracefully shutdown TFPHTTPServer in a thread
« Reply #6 on: July 20, 2017, 05:38:14 pm »
And the leak is because you have fphttpapp in your http_m_listen.

When you terminate your program I don't think the Finalization-part of fphttpapp is called and you get a leak.

You could try just commenting it out:

Code: Pascal  [Select][+][-]
  1. uses
  2.   Classes, SysUtils, {fphttpapp,} fphttpserver;


tudi_x

  • Hero Member
  • *****
  • Posts: 532
Re: Gracefully shutdown TFPHTTPServer in a thread
« Reply #7 on: July 20, 2017, 05:45:39 pm »
i took out fphttpapp - still get the leak.

on a theoretic side - as WSACancelBlockingCall was removed from windows sockets 2, on windows TFPHTTPServer should have been using a different mechanism?
Lazarus 2.0.2 64b on Debian LXDE 10

rvk

  • Hero Member
  • *****
  • Posts: 6111
Re: Gracefully shutdown TFPHTTPServer in a thread
« Reply #8 on: July 20, 2017, 05:58:26 pm »
i took out fphttpapp - still get the leak.
on a theoretic side - as WSACancelBlockingCall was removed from windows sockets 2, on windows TFPHTTPServer should have been using a different mechanism?
Then the leak is probably due to the exception itself.

When there is an exception in the socket-code not all memory might be freed.

What happens if you replace the sleep(500) in the main with readln.
And then run the program, wait a few seconds and press enter.

(I'm still puzzled why it's not working for me anymore in Windows. Maybe I'll have to reinstall trunk completely again.)

rvk

  • Hero Member
  • *****
  • Posts: 6111
Re: Gracefully shutdown TFPHTTPServer in a thread
« Reply #9 on: July 20, 2017, 11:35:19 pm »
Well, I tried a fresh trunk 32 bit at home and there is also didn't respond to the _T.Server.Active := False;
Next I tried a 64 bit laz 1.6.4 and there it worked. I also got your exact result.
I tried some things but also couldn't figure out why you get the exception.
The Active := false triggers a CloseSocket which results in that exception.

Even with some simple console-code with just the THTTPServerThread (and without your TLink-thread) it gave the same exception.
I'm sure the following leak is due to that exception.

Frankly, I find TFPHttpServer in combination with a thread and setting Active := false quite unstable. In 32 bit trunk it doesn't even work. My preferred goto is Synapse for communications. (I already implemented it in my own Delphi program for Google authentication and redirect to local)

Code: Pascal  [Select][+][-]
  1. var
  2.   _T: THTTPServerThread;
  3. begin
  4.   writeln('execute 1');
  5.   _T := THTTPServerThread.Create(8001);
  6.   writeln('execute 2');
  7.   sleep(5000);
  8.   writeln('execute 3');
  9.   _T.Server.Active := False;
  10.   writeln('execute 4');
  11.   sleep(5000);
  12.    _T.WaitFor;
  13.   writeln('execute 5');
  14.   _T.Destroy;
  15.   writeln('execute 6');
  16. end.

Code: [Select]
execute 1
create http
execute 2
execute http
execute 3
execute 4
exception http: Could not accept a client connection on socket: 296, error 10004
finish http
execute 5
destroy http
execute 6
Heap dump by heaptrc unit
86 memory blocks allocated : 4309/4600
84 memory blocks freed     : 4133/4424
2 unfreed memory blocks : 176
True heap size : 196608
True free heap : 196384
Should be : 196176
Call trace for block $00000000001307D0 size 24
  $000000010000A702
  $00000001000084F7
  $0000000100031F86
  $0000000100032603
  $0000000100032DE0
  $0000000100032C43
  $0000000100031711
Call trace for block $0000000000126550 size 152
  $000000010000A702
  $00000001000084F7
  $0000000100032C0B
  $0000000100031711
  $00000001000312FE
  $000000010002DB4C line 59 of http_m_listen.pas
  $000000010001A49D

Josh

  • Hero Member
  • *****
  • Posts: 1271
Re: Gracefully shutdown TFPHTTPServer in a thread
« Reply #10 on: July 20, 2017, 11:55:01 pm »
Hi
Not sure if it relates to this scenario, but I recently had odd issue with trunk using gdb on win 7 64 bit running 32bit app in ide, running 64bit app in ide was fine. My issue was not with threads, if I run the 32bit app in ide without debugger it worked fine.

Its purely a shot in the dark, probably nothing to do with your issue, but thought i would mention just in case.
« Last Edit: July 20, 2017, 11:58:15 pm by josh »
The best way to get accurate information on the forum is to post something wrong and wait for corrections.

rvk

  • Hero Member
  • *****
  • Posts: 6111
Re: Gracefully shutdown TFPHTTPServer in a thread
« Reply #11 on: July 20, 2017, 11:59:07 pm »
Not a bad shot. But in this case, because it was a console program, I compiled it in Lazarus IDE and started it in a command prompt. So GDB.exe was not used in my case.

Thanks for thinking along.

Josh

  • Hero Member
  • *****
  • Posts: 1271
Re: Gracefully shutdown TFPHTTPServer in a thread
« Reply #12 on: July 21, 2017, 12:01:47 am »
ok, was wondering if heaptrc maybe exhibiting the same, does the console app throw an exception without heaptrc.

A big shot in dark, and my last odd thought of the day.
The best way to get accurate information on the forum is to post something wrong and wait for corrections.

rvk

  • Hero Member
  • *****
  • Posts: 6111
Re: Gracefully shutdown TFPHTTPServer in a thread
« Reply #13 on: July 21, 2017, 12:04:55 am »
ok, was wondering if heaptrc maybe exhibiting the same, does the console app throw an exception without heaptrc.
Yup, the exception is still there without the heaptrc. (So probably the leak too)

Code: [Select]
execute 1
create http
execute 2
execute http
execute 3
execute 4
exception http: Could not accept a client connection on socket: 288, error 10004
finish http
execute 5
destroy http
execute 6

Josh

  • Hero Member
  • *****
  • Posts: 1271
Re: Gracefully shutdown TFPHTTPServer in a thread
« Reply #14 on: July 21, 2017, 12:24:15 am »
I have downloaded the code, but cannot get it work past free resource api,
I was going to try using cmem,
The best way to get accurate information on the forum is to post something wrong and wait for corrections.

 

TinyPortal © 2005-2018