Skip to content

Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure

TypeScript

fix(control-ui): clear pending toast timers on ToastProvider unmount

Diego Zacariasf0b8a8cf1db1
diff --git a/control-ui/components/Toast/index.tsx b/control-ui/components/Toast/index.tsx
index 9705719b5..97c60537f 100644
--- a/control-ui/components/Toast/index.tsx
+++ b/control-ui/components/Toast/index.tsx
@@ -1,6 +1,14 @@
'use client'
-import React, { createContext, useCallback, useContext, useMemo, useRef, useState } from 'react'
+import React, {
+ createContext,
+ useCallback,
+ useContext,
+ useEffect,
+ useMemo,
+ useRef,
+ useState,
+} from 'react'
import type { ToastContextValue, ToastOptions, ToastRecord } from './types'
const DEFAULT_DURATION_MS = 3500
@@ -10,6 +18,18 @@ export function ToastProvider({ children }: { children: React.ReactNode }) {
const [items, setItems] = useState<ToastRecord[]>([])
const timersRef = useRef<Record<string, ReturnType<typeof setTimeout>>>({})
+ // Clear any pending auto-dismiss timers when the provider unmounts, so a
+ // scheduled setTimeout can never fire setItems after teardown (leaks a
+ // "window is not defined" / setState-after-unmount error under test).
+ useEffect(() => {
+ const timers = timersRef.current
+ return () => {
+ for (const timer of Object.values(timers)) {
+ clearTimeout(timer)
+ }
+ }
+ }, [])
+
const dismissToast = useCallback((id: string) => {
setItems(prev => prev.filter(item => item.id !== id))
const existing = timersRef.current[id]
diff --git a/control-ui/components/__tests__/Toast.test.tsx b/control-ui/components/__tests__/Toast.test.tsx
new file mode 100644
index 000000000..29a357968
--- /dev/null
+++ b/control-ui/components/__tests__/Toast.test.tsx
@@ -0,0 +1,51 @@
+import React from 'react'
+import { afterEach, describe, expect, it, vi } from 'vitest'
+import { act, render } from '@testing-library/react'
+import { ToastProvider, useToast } from '../Toast'
+
+function ToastTrigger({ message }: { message: string }) {
+ const { showToast } = useToast()
+ return (
+ <button type="button" onClick={() => showToast(message)}>
+ fire
+ </button>
+ )
+}
+
+afterEach(() => {
+ vi.useRealTimers()
+ vi.restoreAllMocks()
+})
+
+describe('ToastProvider timer lifecycle', () => {
+ // Regression guard: a toast schedules a 3500ms auto-dismiss setTimeout. If the
+ // provider unmounts (test teardown, route change) before it fires, the timer
+ // must be cleared — otherwise it runs setItems after the environment is gone
+ // ("window is not defined" under vitest/jsdom teardown). React 18 silences a
+ // post-unmount setState (no throw), so the observable contract is precisely
+ // "the pending handle gets clearTimeout'd on unmount" — which is asserted here.
+ it('clears the exact pending auto-dismiss handle on unmount', () => {
+ vi.useFakeTimers()
+ const setSpy = vi.spyOn(globalThis, 'setTimeout')
+ const clearSpy = vi.spyOn(globalThis, 'clearTimeout')
+
+ const { getByText, unmount } = render(
+ <ToastProvider>
+ <ToastTrigger message="hello" />
+ </ToastProvider>
+ )
+
+ act(() => {
+ getByText('fire').click()
+ })
+ // Capture the auto-dismiss handle that showToast just scheduled.
+ expect(setSpy).toHaveBeenCalledTimes(1)
+ const pendingHandle = setSpy.mock.results[0].value
+ expect(clearSpy).not.toHaveBeenCalledWith(pendingHandle)
+
+ unmount()
+
+ // The provider must have cleared that specific still-pending handle.
+ expect(clearSpy).toHaveBeenCalledWith(pendingHandle)
+ })
+})
diff --git a/control-ui/package-lock.json b/control-ui/package-lock.json
index ed451de3d..1781f35d4 100644
--- a/control-ui/package-lock.json
+++ b/control-ui/package-lock.json
@@ -1,12 +1,12 @@
{
"name": "clerum-control-ui",
- "version": "0.1.413",
+ "version": "0.1.414",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "clerum-control-ui",
- "version": "0.1.413",
+ "version": "0.1.414",
"dependencies": {
"@clerum/display-field": "file:../packages/display-field",
"@clerum/llm-providers": "file:../packages/llm-providers",
diff --git a/control-ui/package.json b/control-ui/package.json
index 53af037a4..64164cf73 100644
--- a/control-ui/package.json
+++ b/control-ui/package.json
@@ -1,6 +1,6 @@
{
"name": "clerum-control-ui",
- "version": "0.1.413",
+ "version": "0.1.414",
"private": true,
"scripts": {
"dev": "NEXT_IGNORE_INCORRECT_LOCKFILE=1 next dev",