You are not logged in.
Environment
mORMot 2.4.13372, PUREMORMOT2, Delphi 13 Florence, Win32
Windows 11 (10.0.26300), THttpAsyncServer with hsoEnableTls (SChannel), 4 threads, bound to 127.0.0.1
Symptom
With ReportMemoryLeaksOnShutdown, our test suite (start server, a few HTTPS requests with THttpClientSocket, free the server - repeated in several test cases) sporadically reports, in about 1 of 35 runs:
125 - 132 bytes: THttpServerRequest x 1
317 - 348 bytes: THttpAsyncServerConnection x 1
+ a few AnsiString and some unknown buffers (957-1052, 1981-2172 bytes)It never happened when each test case was run alone in a loop (40x each), only when the whole suite runs - i.e. it is timing dependent.
Log evidence (hsoLogVerbose + LOG_VERBOSE, leaking run, timestamps removed)
Leaking case - the connection is deleted after Shutdown started, and no "Destroy: connections" follows:
THttpAsyncConnections(02ea2500) ConnectionNew {"THttpAsyncServerConnection(02727080)":{Handle:1}} sock=1168 count=0 gc=0
TAsyncConnectionsSockets(027db570) Stop(ProcessRead OnRead) sock=1168 handle=1 r=1 w=0
THttpAsyncConnections(02ea2500) Shutdown threads=4 total=1 reads=1/153 B writes=1/226 B now=1 hi=1 pending=0
THttpAsyncConnections(02ea2500) ConnectionDelete {"THttpAsyncServerConnection(02727080)":{Handle:1}} ndx=0 count=0 117us
THttpAsyncConnections(02ea2500) Destroy finishedFor comparison, a non-leaking case where the connection is still in the list at Shutdown - it is released by Destroy:
THttpAsyncConnections(02eb8d30) Shutdown threads=4 total=2 reads=2/343 B writes=2/373 B now=1 hi=2 pending=1
THttpAsyncConnections(02eb8d30) Destroy: connections=1
THttpAsyncConnections(02eb8d30) Destroy finishedAnalysis (src/net/mormot.net.async.pas)
TAsyncConnections.Shutdown calls Terminate first (3063).
The client closes the connection at the same moment. ConnectionRemove passes its Terminated check (3618) just before, then calls LockedConnectionDelete, which removes the instance from fConnection[] (3348) and calls AddGC (3357).
AddGC exits early because Terminated is now true (2912) - the instance is neither in fConnection[] (released by Destroy, 3124) nor in fGC1/fGC2 (released by FreeGC in Shutdown, 3107/3108). It is leaked together with its fRequest.
There seem to be two more paths with the same result:
ConnectionDelete exits on Terminated (3405) - for a connection which is not fInList (e.g. "Connection: close" / HTTP/1.0), this skips AddGC as well.
ConnectionCreate takes an instance from fGC2 (3286-3293) or creates one (3299), then ConnectionNew returns false if Terminated became true meanwhile (3311). The caller in TAsyncServer.Execute only closes the socket (4179) - the instance (a recycled one already owning an fRequest) is never released.
Suggestion (not tested, just ideas)
Let AddGC keep collecting instances until FreeGC has actually run in Shutdown (e.g. a dedicated flag set after FreeGC instead of Terminated), or free the instance directly once the GC lists are already finalized.
Same for the "not fInList" branch of ConnectionDelete.
When ConnectionNew fails in ConnectionCreate, put the instance back to the pool or free it, instead of leaving it to the caller which only closes the socket.
What we tried on our side
Before freeing the server we removed all listed connections ourselves via Async.ConnectionRemove (while not Terminated yet). All Shutdown lines then showed now=0 and the connection leak disappeared in 80 runs, but freeing the server then raised an exception in about 12 % of the runs, and once TWinIocp + TAsyncConnectionsSockets were leaked instead. So we dropped this workaround - we would be glad to have this fixed in mORMot itself.
Thanks a lot for your great work!
Offline
One data point from a production THttpAsyncServer 2.4.17809 (Windows, IOCP, behind haproxy):
the log shows ConnectionDelete(3031)=false count=2 once per night during normal operation, not at shutdown - the «connection not in the list» branch you describe.
Harmless at that rate, but it confirms the path is reachable outside tests.
Offline