Skip to content

Commit 72433e4

Browse files
committed
chore(runner): add safeguard against duplicate sockets
1 parent 48c08ef commit 72433e4

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

engine/sdks/typescript/runner/src/tunnel.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -573,6 +573,24 @@ export class Tunnel {
573573
return;
574574
}
575575

576+
// Close existing WebSocket if one already exists for this request ID.
577+
// There should always be a close message sent before another open
578+
// message for the same message ID.
579+
//
580+
// This should never occur if all is functioning correctly, but this
581+
// prevents any edge case that would result in duplicate WebSockets for
582+
// the same request.
583+
const existingAdapter = this.#actorWebSockets.get(requestIdStr);
584+
if (existingAdapter) {
585+
this.log?.warn({
586+
msg: "closing existing websocket for duplicate open event for the same request id",
587+
requestId: requestIdStr,
588+
});
589+
// Close without sending a message through the tunnel since the server
590+
// already knows about the new connection
591+
existingAdapter.__closeWithoutCallback(1000, "ws.duplicate_open");
592+
}
593+
576594
// Track this WebSocket for the actor
577595
if (actor) {
578596
actor.webSockets.add(requestIdStr);

0 commit comments

Comments
 (0)