fix: persistent event loop in CLI interview — no more Event loop is closed #42
@@ -179,9 +179,16 @@ def interview(
|
||||
|
||||
typer.echo("Initializing Timmy for interview...\n")
|
||||
|
||||
# Use a single persistent event loop for the entire interview.
|
||||
# Calling asyncio.run() per question kills the loop between calls,
|
||||
# orphaning MCP stdio transports and causing "Event loop is closed."
|
||||
loop = asyncio.new_event_loop()
|
||||
asyncio.set_event_loop(loop)
|
||||
|
||||
try:
|
||||
# Force agent creation by calling chat once with a warm-up prompt
|
||||
try:
|
||||
asyncio.run(
|
||||
loop.run_until_complete(
|
||||
chat("Hello, Timmy. We're about to start your interview.", session_id="interview")
|
||||
)
|
||||
except Exception as exc:
|
||||
@@ -195,12 +202,21 @@ def interview(
|
||||
|
||||
typer.echo("Starting interview...\n")
|
||||
transcript = run_interview(
|
||||
chat_fn=lambda msg: asyncio.run(chat(msg, session_id="interview")),
|
||||
chat_fn=lambda msg: loop.run_until_complete(chat(msg, session_id="interview")),
|
||||
on_answer=_on_answer,
|
||||
)
|
||||
|
||||
# Print full transcript at the end
|
||||
typer.echo("\n" + format_transcript(transcript))
|
||||
finally:
|
||||
# Clean shutdown: close MCP sessions, then the loop
|
||||
try:
|
||||
from timmy.mcp_tools import close_mcp_sessions
|
||||
|
||||
loop.run_until_complete(close_mcp_sessions())
|
||||
except Exception:
|
||||
pass
|
||||
loop.close()
|
||||
|
||||
|
||||
@app.command()
|
||||
|
||||
Reference in New Issue
Block a user