#1533·opentui

`createRoot(renderer).render()` silently wipes the previous tree via `clearContainer`, leaking its effects permanently

Author: letrquanCreated Sep 24, 2026Updated Sep 24, 2026

Environment

  • OS: Windows 11 (not platform-specific; pure JS/reconciler behavior)
  • Runtime: Bun 1.4.0
  • @opentui/core: 0.5.12 (also latest published as of 2026-09-24)
  • @opentui/react: 0.5.12
  • react: 19.2.8

Summary

@opentui/react's createRoot(renderer).render(node) calls reconciler.createContainer(renderer.root, ...) fresh on every call (see packages/react/src/reconciler/renderer.ts's createRoot), unlike ReactDOM.createRoot(el).render(), which updates the existing tree in place on a second call. Because reconciler.createContainer treats a new container's first commit as an initial mount, react-reconciler calls the host config's clearContainer before painting it -- and @opentui/react's clearContainer (packages/react/src/reconciler/host-config.ts) does:

typescript
clearContainer(container: Container) {
  const children = container.getChildren()
  children.forEach((child) => container.remove(child))
},

This removes every existing child of renderer.root, including the entire previous tree's rendered output -- but it never tells that previous tree's own React fiber root to unmount. The result: calling .render() a second time silently erases the first tree's visible content, while its component instances, hooks, useEffect/useLayoutEffect cleanups, and any timers or subscriptions they started keep running.

This leak is permanent, not just "until the whole root is unmounted". createRoot's own container variable is reassigned on every render() call, so root.unmount() (reconciler.updateContainer(null, container, null, () => {})) and the renderer.once(DESTROY, cleanup) handler both only ever see the LAST container. The first tree's container reference is never stored anywhere else, so nothing -- not even unmount() or renderer.destroy() -- can ever reach it again. The repro below confirms this directly: the first tree's setInterval is still firing 300ms after root.unmount() was called and awaited.

The one documented pattern in the README (createRoot(renderer).render(<App />), called exactly once) never hits this. It surfaces the moment an application (or a test helper written to look like ink-testing-library's rerender()) calls .render() again to apply an update, which is a reasonable thing to expect a "root" object to support given ReactDOM's own naming and behavior, and given that Root here already exposes an unmount() method that implies exactly this kind of full lifecycle.

Minimal repro

typescript
// repro.tsx -- run with `bun repro.tsx`
import { createTestRenderer } from "@opentui/core/testing";
import { createRoot } from "@opentui/react";
import React, { useEffect } from "react";

const sleep = (ms: number) => new Promise((r) => setTimeout(r, ms));
let firstTicks = 0;

function Probe({ label, onTick }: { label: string; onTick?: () => void }) {
  useEffect(() => {
    const id = setInterval(() => onTick?.(), 100);
    return () => clearInterval(id);
  }, []);
  return <text>{label}</text>;
}

const { renderer } = await createTestRenderer({ width: 20, height: 10 });
const root = createRoot(renderer as any);

root.render(<Probe label="first" onTick={() => firstTicks++} />);
await sleep(250);

root.render(<Probe label="second" />); // clearContainer wipes "first"'s output here
await sleep(250);
console.log("root children after 2nd render():", renderer.root.getChildren().map((c: any) => c.id));

await root.unmount(); // the only teardown this API exposes
await sleep(50);
const ticksAtUnmount = firstTicks;
await sleep(300);
console.log(`first tree's interval ticks: ${ticksAtUnmount} at unmount -> ${firstTicks} 300ms later`);
console.log(firstTicks > ticksAtUnmount ? "LEAKED: still firing after unmount()" : "cleaned up correctly");
process.exit(0);

Steps

  1. bun install @opentui/[email protected] @opentui/[email protected] react
  2. Save the snippet above as repro.tsx, run bun repro.tsx.

Expected vs actual

Expected (matching ReactDOM.createRoot(el).render()'s update-in-place semantics): the first tree's effect cleanup runs when the second render() replaces it, and its interval never fires again -- root.unmount() afterward would have nothing left to do for it.

Actual, every run:

root children after 2nd render(): [ "text-2" ]
first tree's interval ticks: 5 at unmount -> 8 300ms later
LEAKED: still firing after unmount()

"text-1" (the first tree's rendered output) is already gone from renderer.root's children right after the second render() -- wiped by clearContainer -- but its setInterval keeps firing indefinitely, including after root.unmount() was called and awaited. There is no way to reach and tear down the first tree through the public API at all once a second render() call has happened.

Notes / possible cause

createRoot's container variable is already stored in the returned root's closure (used by cleanup()/unmount()). The natural fix, matching react-dom's own ReactDOMRoot.render(), is for render() to reuse that container via reconciler.updateContainer(node, container, null, () => {}) when one already exists, and only call _render/createContainer on the first call:

typescript
export function createRoot(renderer: CliRenderer): Root {
  let container: OpaqueRoot | null = null
  // ...
  return {
    render: (node: ReactNode) => {
      engine.attach(renderer)
      const wrapped = React.createElement(AppContext.Provider, { value: { keyHandler: renderer.keyInput, renderer } },
        React.createElement(ErrorBoundary, null, node))
      if (container) {
        reconciler.updateContainer(wrapped, container, null, () => {})
      } else {
        container = _render(wrapped, renderer.root)
      }
    },
    unmount: cleanup,
  }
}

This is also related to 03-dual-package-remove-instanceof.md in this same directory: clearContainer's container.remove(child) call is the exact Renderable.remove() call that throws "remove expects a renderable child object" across a dual-module-identity split. A consumer whose render() calls happen to straddle that split (e.g. a bundler/test-runner resolving @opentui/core through two different conditions in different parts of the module graph) hits an uncaught crash here instead of the silent leak this report describes -- same root cause (createRoot always re-mounting), two different visible failures depending on whether the module graph is split.

Related issues

  • None found with matching keywords (createRoot, clearContainer, "render twice", "unmount leak") via gh search issues --repo anomalyco/opentui or gh search prs --repo anomalyco/opentui.
  • Issue #432 ("Bug: conditional sibling causes insertBefore crash in <box>", open) is a different, adjacent React-reconciler-integration bug (an anchor-resolution crash on insertBefore, and a follow-up comment about LineNumberRenderable's custom remove() needing to call clearTarget() when React unmounts its child) -- same general area (host-config lifecycle methods vs. React's own mount/unmount assumptions) but not a duplicate.