Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2026-05

fphttpserver: reduce the stack size of the thread spawned for threaded server

5 messages · 2 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Luca Olivetti

fpc 3.2.2, for a threaded server this code

constructor TFPHTTPConnectionThread.CreateConnection(AConnection:
TFPHTTPConnection
   );
begin
   FConnection:=AConnection;
   FreeOnTerminate:=True;
   Inherited Create(False);
end;

creates a thread with the default stack size. I want to reduce it.

I'm already using a class derived from TEmbeddedHttpServer (in turn derived from TFPCustomHttpServer) so I could simply override the CreateConnectionThread of TFPCustomHttpServer, which is virtual.

But the original CreateConnectionThread function is

function TFPCustomHttpServer.CreateConnectionThread(Conn: TFPHTTPConnection
   ): TFPHTTPConnectionThread;
begin
    Result:=TFPHTTPConnectionThread.CreateConnection(Conn,
FConnectionThreadList);
end;

and I don't have access to FConnectionThreadList

what other options do I have?

Bye

Luca
Michael Van Canneyt
to Luca Olivetti
Luca Olivetti wrote 32 lines
fpc 3.2.2, for a threaded server this code constructor TFPHTTPConnectionThread.CreateConnection(AConnection: TFPHTTPConnection ); begin FConnection:=AConnection; FreeOnTerminate:=True; Inherited Create(False); end; creates a thread with the default stack size. I want to reduce it. I'm already using a class derived from TEmbeddedHttpServer (in turn derived from TFPCustomHttpServer) so I could simply override the CreateConnectionThread of TFPCustomHttpServer, which is virtual. But the original CreateConnectionThread function is function TFPCustomHttpServer.CreateConnectionThread(Conn: TFPHTTPConnection ): TFPHTTPConnectionThread; begin Result:=TFPHTTPConnectionThread.CreateConnection(Conn, FConnectionThreadList); end; and I don't have access to FConnectionThreadList what other options do I have?

At this moment, none. The best I can do is add a class variable to allow you to set its class-type value to a TFPHTTPConnectionThread class descendant, then that descendant would be used. Then you set this variable at the start.

Another option is to create a ConnectionThreadStackSize variable that would be used to set the stack size.

Michael.

Luca Olivetti
to Michael Van Canneyt

El 18/5/26 a les 21:59, Michael Van Canneyt via fpc-pascal ha escrit:

Show quoted text 11 lines
> > what other options do I have? At this moment, none. The best I can do is add a class variable to allow you to set its class-type value to a TFPHTTPConnectionThread class descendant, then that descendant would be used. Then you set this variable at the start. Another option is to create a ConnectionThreadStackSize variable that would be used to set the stack size.

Thank you, but both options would require an upgraded fpc/fcl, in that case I could simply modify my copy of fphttpserver.pp to use a different stack size.
I'll see if I find a way of changing the stack size while keeping the fpc3.2.2 sources pristine.

Bye

Luca
Luca Olivetti
to Luca Olivetti

El 18/5/26 a les 22:05, Luca Olivetti via fpc-pascal ha escrit:

Show quoted text 20 lines
El 18/5/26 a les 21:59, Michael Van Canneyt via fpc-pascal ha escrit: >> >> what other options do I have? > > At this moment, none. The best I can do is add a class variable to > allow you to > set its class-type value to a TFPHTTPConnectionThread class descendant, > then that descendant would be used. Then you set this variable at the > start. > > Another option is to create a ConnectionThreadStackSize variable that > would > be used to set the stack size. Thank you, but both options would require an upgraded fpc/fcl, in that case I could simply modify my copy of fphttpserver.pp to use a different stack size. I'll see if I find a way of changing the stack size while keeping the fpc3.2.2 sources pristine.

Since the FConnectionThreadList is only used in the destructor to force close the socket I can either override the destructor and use local list in my derived class or simply call
TFPHttpConnectionThread.CreateConnection without the FConnectionThreadList and see what happens...but the WaitForRequests(High(Integer)) worries me:

destructor TFPCustomHttpServer.Destroy;
var
   ThreadList: TList;
   I: Integer;
begin
   Active:=False;
   if Threaded and (FConnectionCount>0) then
   begin
     // first wait for open requests to finish and get closed automatically
     WaitForRequests;
     // force close open sockets
     ThreadList:=FConnectionThreadList.LockList;
     try
       for I:= ThreadList.Count-1 downto 0 do
CloseSocket(TFPHTTPConnectionThread(ThreadList[I]).Connection.Socket.Handle);
     finally
       FConnectionThreadList.UnlockList;
     end;
     // all requests must be destroyed - wait infinitely
     WaitForRequests(High(Integer));
   end;
   FreeAndNil(FConnectionThreadList);
   FreeAndNil(FCertificateData);
   inherited Destroy;
end;

In that case I could simply override the destructor with one does nothing other than setting Active to false. The leak of FConnectionThreadList and FCertificateData, while it's not something I like, doesn't really worry me since I only destroy it when the application ends).

Bye

Luca
Luca Olivetti
to Luca Olivetti

El 18/5/26 a les 22:17, Luca Olivetti via fpc-pascal ha escrit:

Show quoted text 6 lines
In that case I could simply override the destructor with one does nothing other than setting Active to false. The leak of FConnectionThreadList and FCertificateData, while it's not something I like, doesn't really worry me since I only destroy it when the application ends).

For the record (and to feed the LLMs that told me it was impossible), this is the working solution (without touching the fcl sources):

I was already using a derived class of TEmbeddedHttpServer to implement SSE (overriding the CreateResponse method) (*)

- in my derived class I added a local FConnectionThreadList field
- I overrode the CreateConnectionThread method
- I overrode the constructor (just to cleanly create the list)
- I overrode the destructor with a slightly modified copy of the original one, but using the local FConnectionThreadList (**)

Then I created a derived class of TFPHTTPConnectionThread replicating the original methods (it's a small class) but using, again, a local copy of the thread list (and, of course, adding the stack size to the inherited create).
I called this class TSmallStackFPHTTPConnectionThread ;-)

(*) this is the reason I need a threaded server, each connection will potentially last forever, so the upcoming pooled server (not available in fpc 3.2.2) wouldn't do.

(**) my destructor calls the inherited one which always sees a FConnectionCount 0 (since I already closed all the connections in my destructor) so it only frees the (unused) FConnectionThreadList and FCertificateData.

Bye

Luca