Hi, I'm still struggling with the drain timeout wh...
# ask-ai
t
Hi, I'm still struggling with the drain timeout when deploying a new version of my agent. What I did: • I deployed my agents with Render.com, set the
maxShutdownDelaySeconds
to 5 minutes • I set
WorkerOptions.drain_timeout
to
10000
What happens: • When a call is in progress on a given machine, the machine shuts down anyway • The call "restarts" on a new machine, but that means that the bot flow restarts as well... How can I fix this?
t
Thanks for reaching out, Pierrick. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Thanks — I’m responding to your note about the drain timeout and calls restarting during deploy. Quick summary of the problem: your platform (Render) is terminating the instance while a call is active, so the agent process stops mid-call and a new agent reconnects (which restarts the bot flow). What to check and do next 1) Match timeouts • Set
WorkerOptions.drain_timeout
to match Render’s
maxShutdownDelaySeconds
(5 minutes = 300 seconds). Your current
10000
is 10 seconds and is too short. 2) Ensure the agent actually drains on SIGTERM • Confirm the process receives SIGTERM and that your agent server enters draining (stops accepting new jobs but continues running active calls) rather than exiting immediately. • Make sure your container/runtime or entrypoint isn’t overriding or swallowing SIGTERM. 3) Confirm Render honors the grace period • Check Render logs for SIGTERM and for a forced kill before the 5-minute window expires. If the platform kills the process early, increase
drain_timeout
won’t help. 4) Make the agent’s load reporting accurate • Ensure your agent reports active jobs correctly (or supply a custom
load_fnc
) so the dispatcher won’t move work away from busy nodes. 5) Consider session state persistence • If an instance can still be terminated, persist critical session state (e.g.,
session.userdata
) to a DB and restore it on reconnect so flow can continue instead of restarting. If you’d like, tell me: • which SDK/language you’re using (Python/JS/etc.), and • how you start the agent process in Render (your Dockerfile/command or start script) I’ll give exact config examples and the specific places to add SIGTERM handling and state persistence for your environment. Sources: Agent server options | LiveKit docs | Deploying to custom environments | LiveKit Docs | Job lifecycle | LiveKit docs | Agent dispatch | LiveKit docs
t
I actually noticed that this also happens out of the blue. For instance, this call
SCL_gXTtfPjYGtww
restarted twice whereas there was no deploy or no action on the machine during this time.
t
Thanks — I’m following up about the unexpected restarts (example: call SCL_gXTtfPjYGtww) that happened with no deploy. To diagnose this, please share the following details and any logs you can collect around the timestamp(s) when the call restarted: 1. Runtime details • Which SDK / language is your agent running (Python, Node, etc.)? • How you start the agent on Render (Dockerfile CMD, start script, or Render startup command). 1. Timing & logs • Exact timestamps (UTC) when the call restarted. • Agent logs from ~1 minute before to ~2 minutes after each restart. Include system logs from Render showing signals, container shutdown, or OOM/killed events. 1. Agent configuration • Your
WorkerOptions.drain_timeout
value (current config file or code). • Render
maxShutdownDelaySeconds
and any autoscaling settings. 1. Load and dispatch behavior • Are you using explicit
agent_name
dispatch or automatic assignment? • Any custom
load_fnc
or active job reporting logic? 1. Session/state handling • Do you persist
session.userdata
or other state externally? If so, how (DB, cache)? What I’ll look for once you provide logs • SIGTERM/SIGKILL or other termination signals around the event time. • OOM or kernel kill messages from the platform. • Errors or exceptions in the agent that cause it to crash/restart. • Heartbeat/availability changes that might trigger reassignment. If you can paste the log snippets (or the key lines) and the timestamps, I’ll analyze them and recommend concrete fixes (config changes, signal handling, persistence, or dispatch adjustments).
t
Ok, I think I found why. In one of my agent's tools, I run an openai chat completions to increase my decision accuracy. When the code is run, the bot switches servers and restarts its flow. Do you know why ? I'm using Python and
openai_client.chat.completions.parse
is synchronous
t
Thanks — this pinpoints the likely cause. Why this happens • A synchronous
openai_client.chat.completions.parse
call blocks your agent process/event loop. • While blocked the agent appears unresponsive to the dispatcher/health checks, so the system treats the worker as unavailable and reassigns the call to another server — which restarts the bot flow. What you can do (pick one based on your app) • Use an async OpenAI call (preferred). Switch to the async client/method so the agent stays responsive while waiting for the LLM. • Offload the blocking call to a background thread or process. For example, run the sync call inside a thread pool (e.g.,
asyncio.to_thread(...)
) so the main event loop remains responsive. • Increase relevant timeouts so brief blocking won’t trigger reassignment: ◦ Raise
WorkerOptions.drain_timeout
to match your platform shutdown grace (if not already). ◦ Raise the agent request/health timeout (if you have a
maxRequestDuration
or similar) so the dispatcher tolerates longer operations. • Persist session state. Save
session.userdata
to a DB before long operations and restore after reconnect so the flow can continue if reassignment still occurs. If you want, tell me which async HTTP/OpenAI library you can use (or paste the relevant code snippet), and I’ll provide a short Python example converting your sync call to an async-safe pattern (either
async
client or
asyncio.to_thread
).