Fix memory leak and handle errors gracefully in LLM autosuggester by aakritiieee7 · Pull Request #15275 · ipython/ipython · GitHub
Skip to content

Fix memory leak and handle errors gracefully in LLM autosuggester#15275

Open
aakritiieee7 wants to merge 1 commit into
ipython:mainfrom
aakritiieee7:fix-autosuggest-memory-leak
Open

Fix memory leak and handle errors gracefully in LLM autosuggester#15275
aakritiieee7 wants to merge 1 commit into
ipython:mainfrom
aakritiieee7:fix-autosuggest-memory-leak

Conversation

@aakritiieee7

Copy link
Copy Markdown

Summary

This PR resolves a memory leak in the auto-suggestion subsystem of the terminal interactive shell, fixes an incorrect default value in the prefixer, and improves robustness against cancellations and API errors.

Changes

  1. Memory Leak Fix: The disconnect method of NavigableAutoSuggestFromHistory was updated to remove _dismiss from the buffer's on_cursor_position_changed event. Previously, only the on_text_insert handler was removed, resulting in a leak where the auto-suggester remained referenced by the event.
  2. Default Prefixer Fix: Changed _llm_prefixer default implementation from returning "wrong" to "".
  3. Execution Robustness: Wrapped await self._llm_task execution in a try-except block to catch asyncio.CancelledError and Exception. This ensures that natural task cancellations (during cursor movement) or LLM provider errors do not bubble up to prompt_toolkit and crash the prompt.

Testing Checklist

  • Verify on_cursor_position_changed handler is removed upon disconnect.
  • Verify trigger routine gracefully handles task cancellation without throwing CancelledError.
  • Verify trigger routine gracefully catches and suppresses provider exceptions.

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.

1 participant