Fix hanging process when participant disconnects during init#1087
Fix hanging process when participant disconnects during init#1087
Conversation
🦋 Changeset detectedLatest commit: 2b22376 The changes in this PR will be included in the next version bump. This PR includes changesets to release 21 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
| // Close the primary agent session if it exists | ||
| if (ctx._primaryAgentSession) { |
There was a problem hiding this comment.
🔴 Cleanup code after try-catch is unprotected — process can still hang if cleanup throws
The PR wraps the entry function in a try-catch to ensure joinFuture.resolve() is reached, but the cleanup code that runs after the try-catch (lines 143-163) is itself unprotected. If any of ctx._primaryAgentSession.close(), ctx._onSessionEnd(), or room.disconnect() throws, the exception will propagate out of the async IIFE, and joinFuture.resolve() on line 163 will never execute — causing the exact same process-hang the PR is trying to fix.
Root cause and concrete throw path
ctx._onSessionEnd() at line 149 calls this.makeSessionReport(session) (agents/src/job.ts:295), which can throw in at least two places:
// job.ts:266
if (!targetSession) {
throw new Error('Cannot prepare report, no AgentSession was found');
}
// job.ts:272
if (recorderIO && recorderIO.recording) {
throw new Error('Cannot create the AgentSession report, the RecorderIO is still recording');
}Neither of these throws is caught inside _onSessionEnd — only the uploadSessionReport call has a try-catch. So makeSessionReport throwing causes _onSessionEnd to reject, which causes the await at line 149 to throw, which propagates out of the IIFE, skipping joinFuture.resolve() on line 163.
Similarly, room.disconnect() at line 151 or ctx._primaryAgentSession.close() at line 145 could reject.
Impact: The process hangs indefinitely (never exits), which is the same bug the PR intends to fix.
(Refers to lines 143-163)
Was this helpful? React with 👍 or 👎 to provide feedback.
There was a problem hiding this comment.
@lukasIO Should we apply this suggestion as well?
Description
closes #927
when func(ctx) throws, the entire cleanup path is skipped because there was no try-catch around it. The joinFuture is never resolved, so the process never exits.
Changes Made
Pre-Review Checklist
Testing
restaurant_agent.tsandrealtime_agent.tswork properly (for major changes)Additional Notes
Note to reviewers: Please ensure the pre-review checklist is completed before starting your review.