Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions src/app.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,43 @@ describe("App base MCP SDK v2 Protocol migration", () => {
expect(setupCalls).toBe(1);
});

for (const closingSide of ["app", "server"] as const) {
it(`cleans up automatic resize when ${closingSide} closes`, async () => {
const app = new App({ name: "view-app", version: "1.0.0" });
let activeObservers = 0;
let cleanupCalls = 0;
app.setupSizeChangedNotifications = () => {
activeObservers += 1;
return () => {
activeObservers -= 1;
cleanupCalls += 1;
};
};
let closeCallbacks = 0;
app.onclose = () => {
closeCallbacks += 1;
};

const first = await connectPair(app);
connected.push(first);
expect(activeObservers).toBe(1);
await first[closingSide].close();
expect(activeObservers).toBe(0);
expect(cleanupCalls).toBe(1);
expect(closeCallbacks).toBe(1);
await app.close();
expect(cleanupCalls).toBe(1);

const second = await connectPair(app);
connected.push(second);
expect(activeObservers).toBe(1);
await second[closingSide].close();
expect(activeObservers).toBe(0);
expect(cleanupCalls).toBe(2);
expect(closeCallbacks).toBe(2);
});
}

it("preserves strict late-handler timing errors", async () => {
const app = new App(
{ name: "view-app", version: "1.0.0" },
Expand Down
14 changes: 13 additions & 1 deletion src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -565,6 +565,18 @@ export class App extends Protocol<BaseContext> {
this.setEventHandler("hostcontextchanged", undefined);
}

private _autoResizeCleanup?: () => void;

protected override _onclose(): void {
const cleanup = this._autoResizeCleanup;
this._autoResizeCleanup = undefined;
try {
cleanup?.();
} finally {
super._onclose();
}
}

/** @internal */
protected buildContext(ctx: BaseContext): BaseContext {
return ctx;
Expand Down Expand Up @@ -2045,7 +2057,7 @@ export class App extends Protocol<BaseContext> {
this._initializedSent = true;

if (this.options?.autoResize) {
this.setupSizeChangedNotifications();
this._autoResizeCleanup = this.setupSizeChangedNotifications();
}
} catch (error) {
// Disconnect if initialization fails.
Expand Down