Skip to content

Commit 02d5557

Browse files
committed
Fix stale bearerToken closure by deferring registerConnection until after metadata refresh
Move registerConnection(connection) after the metadata refresh succeeds, so a connection with a stale activeBearerToken is never registered in the global map. In the 401 retry path, dispose the unregistered connection directly instead of calling removeConnection (which expects a registered connection). In the outer catch, handle both registered and unregistered connections.
1 parent 1e34360 commit 02d5557

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

apps/web/src/environments/runtime/service.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1064,8 +1064,6 @@ async function ensureSavedEnvironmentConnection(
10641064
...createEnvironmentConnectionHandlers(),
10651065
});
10661066

1067-
registerConnection(connection);
1068-
10691067
try {
10701068
try {
10711069
await refreshSavedEnvironmentMetadata(
@@ -1087,18 +1085,22 @@ async function ensureSavedEnvironmentConnection(
10871085
activeRecord = issued.record;
10881086
bearerToken = issued.bearerToken;
10891087
roleHint = issued.role;
1090-
await removeConnection(activeRecord.environmentId).catch(() => false);
1088+
await connection.dispose();
10911089
pendingSavedEnvironmentConnections.delete(activeRecord.environmentId);
10921090
return await ensureSavedEnvironmentConnection(activeRecord, {
10931091
bearerToken,
10941092
role: roleHint,
10951093
serverConfig: options?.serverConfig ?? null,
10961094
});
10971095
}
1096+
registerConnection(connection);
10981097
return connection;
10991098
} catch (error) {
11001099
setRuntimeError(activeRecord.environmentId, error);
1101-
await removeConnection(activeRecord.environmentId).catch(() => false);
1100+
const removed = await removeConnection(activeRecord.environmentId).catch(() => false);
1101+
if (!removed) {
1102+
await connection.dispose().catch(() => {});
1103+
}
11021104
throw error;
11031105
}
11041106
})();

0 commit comments

Comments
 (0)