ARM-software / devlib

Library for interaction with and instrumentation of remote devices.
Apache License 2.0
47 stars 78 forks source link

utils/ssh: Fix atexit.register() SshConnection leak #679

Closed douglas-raillard-arm closed 6 months ago

douglas-raillard-arm commented 6 months ago

SshConnection registers an atexit handler so the connection is closed upon exiting the process if it has not been done before. However, the handler keeps a reference on the connection, which means it will stay alive. If lots of short-lived connections are created (which can happen when using e.g. ThreadPoolExecutor), they will simply stay around and leak.

Fix that by using a weak reference (WeakMethod) to register in the atexit handler, with a callback to unregister it when the object is deallocated.

Fixes https://github.com/ARM-software/devlib/issues/676