Skip to content

Kill a handler still running 30 s after a stop request - #557

Merged
erikhortsch merged 4 commits into
mainfrom
erikhortsch/kill-hung-handler
Nov 6, 2026
Merged

erikhortsch merged 4 commits into
mainfrom
erikhortsch/kill-hung-handler

Conversation

@erikhortsch

@erikhortsch erikhortsch commented Nov 5, 2026 •

Copy link
Copy Markdown
Contributor

Delete, WHIP delete and update end a session by asking the handler to stop over IPC, then waiting for it to exit. A handler that cannot stop never exits, so its session stays active until the pod is stopped. The WHIP relay hang fixed in #556 is one such case. The existing SIGKILL fallback in sessionCloser cannot catch it: it runs from IngressEnded, which only fires after the handler process has exited.

Change

The first DeleteIngress, DeleteWHIPResource or UpdateIngress call for a handler starts a 30 s timer (killGracePeriod). If the handler process is still running when the timer fires, it gets SIGKILL and a warning is logged. Later requests do not restart the timer.

The started process is stored in an atomic.Pointer, so the timer reads it without racing the relaunch loop.

UpdateIngress, DeleteIngress and DeleteWHIPResource now share one killIngress helper, since all three ignored their request.

A handler killed this way never sends its final state, so once it has exited the service publishes ENDPOINT_ERROR ("handler killed after it did not stop") instead. The service keeps the last state each handler reports, and builds this update from it with UpdatedAt set to the time of the kill.

Verified

  • TestKillAfterKillsHandlerStillRunning starts sleep 60, arms the timer with 50 ms, and expects the process to exit with "signal: killed". It tests the new path only, so there is no older behavior for it to fail against.
  • The same test checks that the kill is recorded, which is what makes the service publish ENDPOINT_ERROR once the handler exits.
  • TestUpdateIngressStateRecordsTracksAndForwards checks that the final update keeps the tracks the handler last reported and has a newer UpdatedAt. It fails without the stored state.
  • go build ./... and go test -race ./pkg/... pass.

Not done yet

  • A handler that never receives a stop request still runs until the pod stops. That happens when the client disappears without a DELETE and the room never removes the ingress participant.

🤖 Generated with Claude Code

Delete, WHIP delete and update all end a session by asking the handler to
stop over IPC and trusting it to exit. A handler that cannot stop, such as
one blocked before its pipeline starts, never exits, so the session stays
open until the pod is killed. The existing SIGKILL fallback cannot catch
this: it runs from IngressEnded, which only happens after the handler has
already exited.

Arm a 30 second timer on the first termination request and SIGKILL the
handler if it is still running when it fires. KillIngress waits for the
pipeline to finish, so a healthy handler is long gone by then. The started
process is published atomically so the timer reads it without racing the
relaunch loop.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
UpdateIngress, DeleteIngress and DeleteWHIPResource ignored their requests
and ran the same kill sequence. Move it into killIngress.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@erikhortsch
erikhortsch marked this pull request as ready for review November 5, 2026 21:34
@erikhortsch
erikhortsch requested a review from a team as a code owner November 5, 2026 21:34

@devin-ai-integration devin-ai-integration Bot left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note

Newer findings are available below. Devin Review posted a newer report on this PR, in addition to the findings presented here.

Devin Review found 2 potential issues.

Devin Review

Comment on lines +324 to +325
func (p *process) killIngress(ctx context.Context) (*livekit.IngressState, error) {
p.killAfter(killGracePeriod)

@devin-ai-integration devin-ai-integration Bot Nov 5, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔴 Canceled requests kill active ingress sessions

When a stop request expires before IPC delivery, killAfter still kills the healthy handler 30 seconds later. killIngress never disarms the timer when KillIngress returns an error, so an active stream ends unexpectedly.

Learn more

A stop RPC can fail before reaching the handler. The service starts the force-kill timer before calling KillIngress, which uses the caller's context. If that context has already expired, gRPC returns without delivering the stop signal. The timer still fires and kills a handler that has received no request. The current one-shot fuse also prevents a later successful request from starting a new timer.

Example: A client sends DeleteIngress while the handler's IPC server is starting, then its request times out. IPC never delivers KillIngress; the handler starts streaming normally, but the pending timer kills it 30 seconds after the canceled request.

Recommended fix: Arm the force-kill fallback only after the stop signal has actually been delivered, or use a stop protocol that acknowledges receipt independently of waiting for pipeline completion. Keep a timer for acknowledged stop requests even when their response times out.

Devin Review


Was this helpful? React with 👍 or 👎 to provide feedback.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All three RPCs that arm the timer are requests to end the session, so ending it when the IPC call fails is intended: before this change, a lost delete left the session running. The service also can't tell an undelivered KillIngress from a delivered one that is waiting on a hung pipeline: both return the context error.

Comment thread pkg/service/process_manager.go Resolved
…stop

A handler killed by the termination fallback never publishes its final
state, so the ingress kept its last reported status after the session
ended. Publish ENDPOINT_ERROR from the service once such a handler has
exited.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@devin-ai-integration devin-ai-integration Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Devin Review found 3 new potential issues.

1 flag not posted on this PR by your GitHub settings — view it in Devin Review. (Configure)

Devin Review

Comment on lines +192 to +194
if h.forceKilled.Load() {
s.publishKilledState(context.WithoutCancel(ctx), p)
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔴 Stalled state update blocks session cleanup

When UpdateIngressState stalls after a forced kill, runHandler cannot end the session. Its uncancelable update runs before SessionEnded and IngressEnded, leaving the session active despite the dead handler.

Learn more

The process manager reaps the killed handler before its deferred cleanup runs. The new state update uses a context without cancellation or deadline and runs synchronously ahead of SessionEnded and IngressEnded. If the state notifier waits indefinitely, neither cleanup call runs; the session stays registered and service shutdown waits for it.

Example: A killed handler exits while the IOInfo state-update RPC remains unresponsive. The updater waits, so IngressEnded never removes the session even though its process is gone.

Recommended fix: Bound the final update with a timeout and ensure cleanup executes even if the notifier stalls. Preserve the final-state-before-session-ended ordering when the update completes.

Devin Review


Was this helpful? React with 👍 or 👎 to provide feedback.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

serviceStateNotifier sends this over psrpc, which bounds every request with its 3 s default client timeout when the context has no deadline, so the update cannot hold up SessionEnded and IngressEnded.

Comment thread pkg/service/process_manager.go Resolved
Comment thread pkg/service/process_manager.go Resolved
The ERROR update for a killed handler started from the service's copy of
the ingress info, which only holds the state from session start. It
cleared the room and tracks the handler had reported, and carried the old
UpdatedAt, so it looked older than the updates it was meant to replace.

Keep the last state each handler reports and build the final update from
it, with UpdatedAt set to the time of the kill.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@erikhortsch
erikhortsch merged commit 4491205 into main Nov 6, 2026
14 checks passed
@erikhortsch
erikhortsch deleted the erikhortsch/kill-hung-handler branch November 6, 2026 21:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants