Skip to content

fix: prevent PyMOL crash from unhandled AI component exceptions#4

Open
nmrson wants to merge 1 commit intoravishar313:masterfrom
nmrson:claude/intelligent-pare
Open

fix: prevent PyMOL crash from unhandled AI component exceptions#4
nmrson wants to merge 1 commit intoravishar313:masterfrom
nmrson:claude/intelligent-pare

Conversation

@nmrson
Copy link
Copy Markdown

@nmrson nmrson commented Feb 24, 2026

Summary

  • Fix PyMOL GUI crashing on startup or during use when AI components raise unhandled exceptions
  • Move OpenGL widget creation before AI components so PyMOL core always initializes even if AI features fail
  • Add defensive error boundaries around all AI-related imports, initialization, and event processing

Problem

PyMOL would crash entirely when:

  1. get_ai_runtime() constructor failed (the function call was outside try/except)
  2. update_feedback() timer callback hit any exception (no error handling, kills Qt event loop)
  3. AI UI imports failed at module load time (non-defensive top-level imports)
  4. Chat panel or chat store initialization failed before OpenGL widget was created

Changes

  • modules/pymol/ai/__init__.py: Defensive import of runtime module
  • modules/pymol/_gui.py: get_ai_runtime() fully wrapped in try/except; doTypedCommand() AI path protected
  • modules/pmg_qt/pymol_qt_gui.py:
    • OpenGL widget created first (before AI components)
    • All AI imports use try/except with fallback stubs
    • All chat_panel, ext_window, _chat_store references null-guarded
    • update_feedback() uses try/finally to always restart timer
    • Signal handlers (_on_chat_command_submitted, _on_chat_clear_requested, _on_chat_stop_requested) wrapped in try/except

Test plan

  • Launch PyMOL with all AI dependencies installed → AI chat panel works normally
  • Launch PyMOL with missing claude-agent-sdk → PyMOL starts without crash, AI features gracefully degraded
  • Launch PyMOL with missing openai package → PyMOL starts without crash
  • Toggle fullscreen with/without chat panel → no crash
  • Submit AI command when runtime fails → falls through to standard PyMOL command processing

🤖 Generated with Claude Code

The PyMOL GUI would crash on startup or during use when AI component
initialization or event processing raised unhandled exceptions. This
was caused by several missing error boundaries:

- get_ai_runtime() in _gui.py only wrapped the import in try/except
  but left the AiRuntime constructor call unprotected
- update_feedback() timer callback had no error handling, so any
  exception would kill the Qt event loop and freeze/crash PyMOL
- AI UI components (chat panel, chat store, history popup) were
  imported eagerly and created unconditionally, so any failure in
  these modules would prevent PyMOL from starting entirely
- OpenGL widget was created after the chat panel, meaning a chat
  panel failure would prevent the core PyMOL viewer from initializing

Key changes:
- Move OpenGL widget creation before AI components so PyMOL core
  always initializes even if AI features fail
- Make all AI-related imports defensive with try/except fallbacks
- Guard all self.chat_panel, self.ext_window, self._chat_store
  references with None checks for graceful degradation
- Wrap update_feedback() in try/finally to ensure the timer always
  restarts
- Wrap doTypedCommand() AI handling in try/except so commands fall
  through to standard PyMOL processing on failure

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@ravishar313
Copy link
Copy Markdown
Owner

I will have a look at this

@ravishar313
Copy link
Copy Markdown
Owner

Thanks for the fix. I validated PR #4 locally and AI init/runtime failures can currently crash the Qt app.

I’d like a small follow-up before merge:

  • Replace broad except Exception: pass blocks with explicit logging (at least warning-level) so failures are visible and debuggable.
  • Narrow the widest exception wrappers where possible, especially around update_feedback, to avoid hiding unrelated regressions.
  • In init.py, avoid exporting None for get_ai_runtime; use a stub that raises a clear error message instead.

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.

2 participants