From a1abbc2b4daaed95b10eb807a3f18d49bac2e2ab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Catriel=20M=C3=BCller?= Date: Wed, 1 Apr 2026 16:08:14 -0300 Subject: [PATCH] refactor: fix unmount --- packages/opencode/src/kilocode/claw/hooks.ts | 38 ++++++++++++-------- 1 file changed, 23 insertions(+), 15 deletions(-) diff --git a/packages/opencode/src/kilocode/claw/hooks.ts b/packages/opencode/src/kilocode/claw/hooks.ts index 23f8a452c22..f25d3739e85 100644 --- a/packages/opencode/src/kilocode/claw/hooks.ts +++ b/packages/opencode/src/kilocode/claw/hooks.ts @@ -69,6 +69,26 @@ export function createClawChat(sdk: any) { } onMount(async () => { + // Register cleanup synchronously while the SolidJS owner scope is + // still available. After any `await` the owner is lost, so + // `onCleanup` calls would be silently discarded. + const cleanup = { + unsub: null as (() => void) | null, + unsubUpdated: null as (() => void) | null, + unsubPresence: null as (() => void) | null, + } + onCleanup(() => { + cleanup.unsub?.() + cleanup.unsubUpdated?.() + cleanup.unsubPresence?.() + if (chat) { + chat.disconnect().catch((err) => { + log.error("disconnect failed", { error: err?.message ?? String(err) }) + }) + } + chat = null + }) + log.info("fetching credentials") const res = await sdk.client.kilo.claw.chatCredentials().catch((e: any) => { log.error("chatCredentials() threw", { error: e?.message ?? String(e) }) @@ -105,7 +125,7 @@ export function createClawChat(sdk: any) { setOnline(presence(chat.channel, bot)) // Subscribe to new messages - const unsub = chat.onMessage((msg) => { + cleanup.unsub = chat.onMessage((msg) => { setMessages((prev) => { const next = [...prev, msg] return next.length > MAX_MESSAGES ? next.slice(-MAX_MESSAGES) : next @@ -113,7 +133,7 @@ export function createClawChat(sdk: any) { }) // Subscribe to message updates (bot streams token-by-token via message.updated) - const unsubUpdated = chat.onMessageUpdated((msg) => { + cleanup.unsubUpdated = chat.onMessageUpdated((msg) => { setMessages((prev) => { const idx = prev.findIndex((m) => m.id === msg.id) if (idx === -1) { @@ -127,22 +147,10 @@ export function createClawChat(sdk: any) { }) // Subscribe to bot presence changes - const unsubPresence = chat.onPresence(setOnline) + cleanup.unsubPresence = chat.onPresence(setOnline) setConnected(true) setLoading(false) - - onCleanup(() => { - unsub() - unsubUpdated() - unsubPresence() - if (chat) { - chat.disconnect().catch((err) => { - log.error("disconnect failed", { error: err?.message ?? String(err) }) - }) - } - chat = null - }) } catch (err: any) { log.error("connect failed", { error: err?.message ?? String(err),