Skip to content

Commit efaf46c

Browse files
committed
fix: release stale sharing loads
1 parent 87ea745 commit efaf46c

2 files changed

Lines changed: 59 additions & 3 deletions

File tree

‎ui/src/pages/chat/chat-pane-sharing.test.ts‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,42 @@ describe("chat pane sharing authorization", () => {
312312
expect(request).not.toHaveBeenCalled();
313313
expect(sessions.refreshReplacement).not.toHaveBeenCalled();
314314
});
315+
316+
it("releases a stale same-key sharing load for the replacement session", async () => {
317+
const stale = sessionRow();
318+
const replacement = { ...sessionRow(), sessionId: "session-replacement" };
319+
const listed = createDeferred<SessionMembersListResult>();
320+
const request = vi.fn((method: string) => {
321+
if (method !== "session.members.list") {
322+
throw new Error(`unexpected request: ${method}`);
323+
}
324+
return request.mock.calls.length === 1
325+
? listed.promise
326+
: Promise.resolve(sharingResult(replacement));
327+
});
328+
const sessions = {
329+
refreshReplacement: vi.fn(),
330+
} as unknown as SessionCapability;
331+
const { pane: testPane, state } = createSharingTestChatPane({
332+
client: { request } as unknown as GatewayBrowserClient,
333+
sessions,
334+
});
335+
const pane = testPane as SharingPane;
336+
const pending = pane.loadSessionSharing(stale);
337+
state.sessionsResult = sharingSessionsResult(replacement);
338+
listed.resolve(sharingResult(stale));
339+
await pending;
340+
341+
expect(pane.sessionSharingStates.has(pane.sessionSharingCacheKey(stale.key))).toBe(false);
342+
343+
await pane.loadSessionSharing(replacement);
344+
345+
expect(request).toHaveBeenCalledTimes(2);
346+
expect(pane.sessionSharingStates.get(pane.sessionSharingCacheKey(replacement.key))).toEqual({
347+
loading: false,
348+
result: sharingResult(replacement),
349+
});
350+
});
315351
});
316352

317353
describe.each(mutations)("chat pane $name mutation connection ownership", (mutation) => {

‎ui/src/pages/chat/chat-pane-sharing.ts‎

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,19 @@ export abstract class ChatPaneSharing extends ChatPaneBase {
9090
// gateway/account change bumps the generation and clears this cache, so a
9191
// request that resolves after the switch must be dropped rather than
9292
// overwrite the new connection's menu with the previous account's data.
93-
this.setSessionSharingState(cacheKey, { ...current, loading: true, error: undefined });
93+
// Object identity also owns this key's request slot: same-key session
94+
// replacement or a forced reload must not let an older request win.
95+
const loadingState = { ...current, loading: true, error: undefined };
96+
const ownsLoadingState = () => this.sessionSharingStates.get(cacheKey) === loadingState;
97+
const clearOwnedLoadingState = () => {
98+
if (!ownsLoadingState()) {
99+
return;
100+
}
101+
const next = new Map(this.sessionSharingStates);
102+
next.delete(cacheKey);
103+
this.sessionSharingStates = next;
104+
};
105+
this.setSessionSharingState(cacheKey, loadingState);
94106
try {
95107
const result = await scope.client.request<SessionMembersListResult>("session.members.list", {
96108
sessionKey: currentRow.key,
@@ -100,16 +112,24 @@ export abstract class ChatPaneSharing extends ChatPaneBase {
100112
});
101113
if (
102114
!this.isConnectionScopeCurrent(scope) ||
103-
!this.currentSessionSharingRow(scope, currentRow)
115+
!this.currentSessionSharingRow(scope, currentRow) ||
116+
!ownsLoadingState()
104117
) {
118+
if (this.isConnectionScopeCurrent(scope)) {
119+
clearOwnedLoadingState();
120+
}
105121
return;
106122
}
107123
this.setSessionSharingState(cacheKey, { loading: false, result });
108124
} catch (error) {
109125
if (
110126
!this.isConnectionScopeCurrent(scope) ||
111-
!this.currentSessionSharingRow(scope, currentRow)
127+
!this.currentSessionSharingRow(scope, currentRow) ||
128+
!ownsLoadingState()
112129
) {
130+
if (this.isConnectionScopeCurrent(scope)) {
131+
clearOwnedLoadingState();
132+
}
113133
return;
114134
}
115135
this.setSessionSharingState(cacheKey, { loading: false, error: String(error) });

0 commit comments

Comments
 (0)