[monitorlib] Close idle connection after a while#1408
Merged
BenjaminPelletier merged 2 commits intointeruss:mainfrom Apr 8, 2026
Merged
[monitorlib] Close idle connection after a while#1408BenjaminPelletier merged 2 commits intointeruss:mainfrom
BenjaminPelletier merged 2 commits intointeruss:mainfrom
Conversation
This was referenced Apr 2, 2026
Member
BenjaminPelletier
left a comment
There was a problem hiding this comment.
It seems like this approach creates a memory leak. How about something like this approach?
class Foo:
_timer: threading.Timer | None = None
def __init__(self):
Foo._start_closure_timer(weakref.ref(self))
@staticmethod
def _start_closure_timer(wref: weakref.ReferenceType[Foo]) -> None:
def wrapper():
weak_self_final = wref()
if weak_self_final is not None:
try:
print("weak_self_final.close()")
finally:
Foo._start_closure_timer(wref)
weak_self_initial = wref()
if weak_self_initial:
weak_self_initial._timer = threading.Timer(SOCKET_KEEP_ALIVE_LIMIT, wrapper)
weak_self_initial._timer.daemon = True
weak_self_initial._timer.start()
def __del__(self):
if timer := self._timer:
timer.cancel()This suggestion implements my comments on [1408](interuss#1408) plus a few additional things: * Avoids memory leak by using weakref in periodic closure check * Avoids race condition between closure and new request by adding _closure_lock * Informs user when automatic closure occurs via log message, including differentiating UTMClientSession instances with int ID * Increases keepalive period to just under max keepalive time likely used by external infrastructure Tested in current state and verified a large number of sessions (90+) and closures. Tested cherrypicking [1411](interuss#1411) and verified a smaller number of sessions and closures.
BenjaminPelletier
approved these changes
Apr 8, 2026
github-actions bot
added a commit
that referenced
this pull request
Apr 8, 2026
Co-authored-by: Benjamin Pelletier <BenjaminPelletier@users.noreply.github.com> a8acd6b
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Proposition 1 to fix connections leaking. See #1409 for alternative version (or we could have both, but that probably not needed).
The test suite is currently creating a set of
UTMClientSessionduring initialization, for each test and endpoint used.This create issues, as connections use keep-alive mechanism and are never closed. #1407 is an example of that with a lot USS, reaching the limit after a while.
This version of the fix add a thread that close the session after inactivity. This ensure connections are closed and don't share session between tests, but has the disadvantage of creating lot of threads, and therefor background processing outside tests. (NB: We can also use a single thread and a form of register, but that we just remove thread's overhead).
Tested manually by checking the number of open sockets and a very shot timeout that stay low. Connections are still reused.