Skip to content

Commit ed39fff

Browse files
authored
Merge pull request #3 from BasicBlock/pn/upgrade
potential fix for socket disconnect
2 parents 6c6220e + c3f049a commit ed39fff

File tree

1 file changed

+41
-23
lines changed

1 file changed

+41
-23
lines changed

packages/cli-v3/src/entryPoints/managed/controller.ts

Lines changed: 41 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -468,6 +468,18 @@ export class ManagedRunController {
468468
runId: this.runFriendlyId,
469469
message: "Socket connected to supervisor",
470470
});
471+
472+
if (this.runFriendlyId && this.snapshotFriendlyId) {
473+
this.sendDebugLog({
474+
runId: this.runFriendlyId,
475+
message: "Socket connected - re-subscribing to run notifications",
476+
properties: {
477+
snapshotFriendlyId: this.snapshotFriendlyId,
478+
},
479+
});
480+
481+
this.subscribeToRunNotifications(this.runFriendlyId, this.snapshotFriendlyId);
482+
}
471483
});
472484

473485
socket.on("connect_error", (error) => {
@@ -496,6 +508,22 @@ export class ManagedRunController {
496508
});
497509

498510
socket.on("disconnect", async (reason, description) => {
511+
const reconnectSocket = (trigger: string) => {
512+
this.sendDebugLog({
513+
runId: this.runFriendlyId,
514+
message: "Reconnecting supervisor socket",
515+
properties: { trigger, reason },
516+
});
517+
518+
socket.removeAllListeners();
519+
socket.disconnect();
520+
this.socket = this.createSupervisorSocket();
521+
522+
if (this.runFriendlyId && this.snapshotFriendlyId) {
523+
this.subscribeToRunNotifications(this.runFriendlyId, this.snapshotFriendlyId);
524+
}
525+
};
526+
499527
const parseDescription = ():
500528
| {
501529
description: string;
@@ -539,41 +567,31 @@ export class ManagedRunController {
539567
properties: { reason, ...parseDescription(), currentEnv, newEnv },
540568
});
541569

570+
const shouldForceReconnect = reason === "ping timeout";
571+
542572
if (!result) {
573+
if (shouldForceReconnect) {
574+
reconnectSocket("disconnect override fetch failed");
575+
}
543576
return;
544577
}
545578

546-
// If runner ID changed, we detected a restore
547-
if (result.runnerIdChanged) {
579+
if (result.runnerIdChanged || result.supervisorChanged) {
548580
this.sendDebugLog({
549581
runId: this.runFriendlyId,
550-
message: "Runner ID changed - restore detected",
582+
message: "Restore-related env override detected",
551583
properties: {
584+
runnerIdChanged: result.runnerIdChanged,
552585
supervisorChanged: result.supervisorChanged,
553586
},
554587
});
555588

556-
if (!result.supervisorChanged) {
557-
return;
558-
}
559-
560-
// Only reconnect WebSocket if supervisor URL actually changed
561-
this.sendDebugLog({
562-
runId: this.runFriendlyId,
563-
message: "Supervisor URL changed - creating new socket connection",
564-
});
565-
566-
// First disconnect the old socket to avoid conflicts
567-
socket.removeAllListeners();
568-
socket.disconnect();
569-
570-
// Create a new socket with the updated URL and headers
571-
this.socket = this.createSupervisorSocket();
589+
reconnectSocket("restore-related env override");
590+
return;
591+
}
572592

573-
// Re-subscribe to notifications if we have an active execution
574-
if (this.runFriendlyId && this.snapshotFriendlyId) {
575-
this.subscribeToRunNotifications(this.runFriendlyId, this.snapshotFriendlyId);
576-
}
593+
if (shouldForceReconnect) {
594+
reconnectSocket("ping timeout");
577595
}
578596

579597
return;

0 commit comments

Comments
 (0)