Commit Graph

5 Commits

Author SHA1 Message Date
SomberNight 87540dbe3e util.EventListener: store WeakMethods in CallbackManager to avoid leaks
This patch changes the CallbackManager to use WeakMethods (weakrefs) to
break the ref cycle and allow the GC to clean up the wallet objects.
unregister_callbacks() will also get called automatically, from
EventListener.__del__, to clean up the CallbackManager.

I also added a few unit tests for this.

fixes https://github.com/spesmilo/electrum/issues/10427

-----

original problem:

In many subclasses of `EventListener`, such as `Abstract_Wallet`, `LNWatcher`,
`LNPeerManager`, we call `register_callbacks()` in `__init__`.
`unregister_callbacks()` is usually called in the `stop()` method.

Example - consider the wallet object:
- `Abstract_Wallet.__init__()` calls `register_callbacks()`
- there is a `start_network()` method
- there is a `stop()` method, which calls `unregister_callbacks()`
- typically the wallet API user only calls `stop()` if they also called
  `start_network()`.

This means the callbacks are often left registered, leading to the wallet
objects not getting GC-ed. The GC won't clean them up as
`util.callback_mgr.callbacks` stores strong refs to instance methods
of `Abstract_Wallet`, hence strong refs to the `Abstract_Wallet` objects.

An annoying example is `daemon.check_password_for_directory`, which
potentially creates wallet objects for all wallet files in the datadir.
It simply constructs the wallets, does not call `start_network()` and
neither does it call `stop()`.
2026-01-21 17:49:41 +00:00
SomberNight 5d41891554 utils/memory_leak: factor out count_objects_in_memory
and use weakrefs, to properly allow using this to test garbage collection
2026-01-20 22:33:07 +00:00
SomberNight 3a8fbabcae utils/memory_leak: fix debug_memusage_dump_random_backref_chain 0 case
handle case where no objects of desired type are found
2026-01-20 22:31:08 +00:00
SomberNight 41269b9c88 utils/memory_leak: add helpers using 3rd-party package "objgraph"
we will not start depending on "objgraph", to be clear
2025-08-23 15:49:46 +00:00
SomberNight c777c0164e util: move DebugMem from util to utils/memory_leak.py 2025-08-23 15:04:05 +00:00