Skip to content

fix: handle LM Studio disconnect gracefully during generation #70

@VforVitorio

Description

@VforVitorio

Problem

When LM Studio is closed (or the server stops) while lmcode is mid-generation, a raw traceback is printed to the terminal — likely an httpx connection error or an LM Studio SDK exception. The user sees a crash instead of a clean message.

Expected behaviour

Catch the disconnect and show something like:

LM Studio disconnected. Press Ctrl+C to exit or restart lmcode.

Or exit cleanly with a one-liner:

error: lost connection to LM Studio — exiting.

Implementation notes

  • The error likely surfaces as an httpx.RemoteProtocolError, httpx.ConnectError, websockets.exceptions.ConnectionClosedError, or an lmstudio.LMStudioClientError raised inside model.act() in Agent._run_turn().
  • Wrap await model.act(...) (and possibly the outer Agent.run() connection block) with a catch for those exception types.
  • Print a clean Rich message using ERROR color and a Rule separator, then either loop back to prompt (with reconnect logic) or exit via sys.exit(1).
  • If looping back, the chat history is still intact and the user can reconnect by restarting LM Studio and pressing Enter.

Related

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    Status

    Done

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions