xonecas.io
64875
Mc

mini-coder / commit 1c1519a

fix: guard session and tui writes after close

xonecas2026-09-22 1c1519ae470d2f386520e4e8a91356f03dfa54cd parent 1eb6ecc

2 files changed

src/session.ts+4−0
@@ -81,6 +81,7 @@ export class Session {
8181 id: string | null = null;
8282 logPath: string | null = null;
8383 private fd: number | null = null;
84+ private closed = false;
8485
8586 constructor(sessionsDir: string, cwd: string) {
8687 this.sessionsDir = sessionsDir;
@@ -88,6 +89,7 @@ export class Session {
8889 }
8990
9091 appendMessage(message: Message): void {
92+ if (this.closed) return;
9193 this.ensure(titleFor(message));
9294 this.write({
9395 type: "message",
@@ -97,11 +99,13 @@ export class Session {
9799 }
98100
99101 appendRequest(input: Omit<RequestRecord, "type" | "at">): void {
102+ if (this.closed) return;
100103 this.ensure("session");
101104 this.write({ type: "request", at: new Date().toISOString(), ...input });
102105 }
103106
104107 close(): void {
108+ this.closed = true;
105109 if (this.fd !== null) {
106110 closeSync(this.fd);
107111 this.fd = null;
src/tui/tui.ts+3−1
@@ -261,6 +261,7 @@ class Tui {
261261 }
262262
263263 private handleAgentEvent(event: AgentEvent): void {
264+ if (this.closed) return;
264265 switch (event.type) {
265266 case "phase":
266267 this.status = phaseStatus(event.phase, event.detail);
@@ -331,7 +332,7 @@ class Tui {
331332 }
332333
333334 private commit(text: string): void {
334- if (text === "") return;
335+ if (this.closed || text === "") return;
335336 let out = this.live.clear();
336337 out += text.endsWith("\n") ? text : `${text}\n`;
337338 this.term.write(out);
@@ -376,6 +377,7 @@ class Tui {
376377 private exit(): void {
377378 if (this.closed) return;
378379 this.closed = true;
380+ this.abort?.abort();
379381 this.term.write(this.live.clear());
380382 this.term.stop();
381383 this.opts.session.close();