Skip to content

fix(adapter): prevent dictionary memory leak and runtime dict iterati… - #87

Open
Adityakk9031 wants to merge 1 commit into
inkbox-ai:mainfrom
Adityakk9031:#86
Open

fix(adapter): prevent dictionary memory leak and runtime dict iterati…#87
Adityakk9031 wants to merge 1 commit into
inkbox-ai:mainfrom
Adityakk9031:#86

Conversation

@Adityakk9031

@Adityakk9031 Adityakk9031 commented Jul 30, 2026

Copy link
Copy Markdown
Contributor

Fixes #86

Summary

Fixes an issue in InkboxAdapter.send() where stale entries in self._voice_recently_closed were only evaluated and popped for the active chat_id, leading to unbounded dictionary growth and potential RuntimeError: dictionary changed size during iteration during concurrent voice call terminations.

Problem

  1. Unbounded Memory Leak: When a user closed a voice call, _voice_recently_closed[chat_id] recorded the timestamp. However, cleanup (pop) was only attempted if a subsequent message targeted the exact same chat_id after VOICE_GRACE_SECONDS. Inactive chats or messages sent to different chats never triggered pruning for stale entries.
  2. Dict Size Mutation Safety: Modifying self._voice_recently_closed without taking a snapshot during traversal risked raising RuntimeError when concurrent asynchronous voice call webhooks mutated the dictionary during execution.

Fix Applied

  • Global Dict Pruning: Sweeps all entries in self._voice_recently_closed and removes any timestamp older than VOICE_GRACE_SECONDS (60 seconds) on every send() call.
  • Iteration Snapshot: Uses a list snapshot (list(self._voice_recently_closed.items())) during evaluation to guarantee thread/async safe dictionary mutations.
  • Clock Alignment: Captures a single now = time.time() timestamp per evaluation cycle.

Verification

  • Executed unit test suite for voice progress suppression and post-call grace handling:
    python -m pytest tests/test_voice_progress_suppression.py

@Adityakk9031

Copy link
Copy Markdown
Contributor Author

@dimavrem22 and @alex-w-99 have a look

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Unhandled Unchecked Exception in Voice Grace Window Suppression Dict Garbage Collection (adapter.py)

1 participant