utils/memory_leak: fix debug_memusage_dump_random_backref_chain 0 case
What changed, and why it matters
This is a small bug fix in an internal debugging helper used to investigate memory leaks. It now gracefully returns nothing when the requested object type cannot be found, instead of crashing. There is no security relevance.
No security action needed; treat as routine code quality/debugging fix.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The patch modifies electrum/utils/memory_leak.py, a developer-only diagnostic utility. debug_memusage_dump_random_backref_chain previously called random.choice on objgraph.by_type(objtype) without checking whether the list was empty, which would raise IndexError. The fix stores the list, returns None if empty, and otherwise proceeds as before. This is a robustness improvement for a non-production debug tool.
Changed components
electrum/utils/memory_leak.pyInspect captured patch +6 / −2
diff --git a/electrum/utils/memory_leak.py b/electrum/utils/memory_leak.py
index 0d3b463..2c4026c 100644
--- a/electrum/utils/memory_leak.py
+++ b/electrum/utils/memory_leak.py
@@ -52,7 +52,7 @@ def debug_memusage_list_all_objects(limit: int = 50) -> list[tuple[str, int]]:
)
-def debug_memusage_dump_random_backref_chain(objtype: str) -> str:
+def debug_memusage_dump_random_backref_chain(objtype: str) -> Optional[str]:
"""Writes a dotfile to cwd, containing the backref chain
for a randomly selected object of type objtype.
@@ -68,10 +68,14 @@ def debug_memusage_dump_random_backref_chain(objtype: str) -> str:
import random
timestamp = datetime.datetime.now(datetime.timezone.utc).strftime("%Y%m%dT%H%M%SZ")
fpath = os.path.abspath(f"electrum_backref_chain_{timestamp}.dot")
+ objects = objgraph.by_type(objtype)
+ if not objects:
+ return None
+ random_obj = random.choice(objects)
with open(fpath, "w") as f:
objgraph.show_chain(
objgraph.find_backref_chain(
- random.choice(objgraph.by_type(objtype)),
+ random_obj,
objgraph.is_proper_module),
output=f)
return fpath
Why this scored 15/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.