Skip to content

[lexical-table] Bug Fix: disconnect trackTable MutationObserver in removeListeners - #9074

Merged
etrepum merged 1 commit into
meta:mainfrom
kaminski-dev:fix/table-observer-disconnect
Aug 19, 2026
Merged

etrepum merged 1 commit into
meta:mainfrom
kaminski-dev:fix/table-observer-disconnect

Conversation

@kaminski-dev

Copy link
Copy Markdown
Contributor

Description

TableObserver.trackTable() creates a MutationObserver on the table element (attributes + childList + subtree), but nothing ever calls observer.disconnect(). removeListeners() aborts the DOM event listeners and undoes the command registrations, but the tracking observer outlives the TableObserver and the editor itself. After teardown, any mutation of the detached <table> subtree fires the leaked observer, whose callback calls $getTableAndElementByKey() and throws invariant #232 (TableObserver: Expected to find TableElement in DOM for key %s) from a MutationObserver microtask — uncatchable by onError or React error boundaries. It also leaks one observer plus the retained table subtree per table per editor mount.

This change registers observer.disconnect() in listenersToRemove, so removeListeners() disconnects the tracking observer — mirroring what resetEditor() already does for the core mutation observer. disconnect() also empties the observer's record queue, which closes the race where records queued in the same task as teardown would still be delivered afterwards. No behavioral change while the editor is alive.

Closes #9073

Test plan

Added packages/lexical-table/src/__tests__/unit/LexicalTableObserver.test.ts covering both that removeListeners() disconnects the tracking observer and that mutations of the detached table after editor teardown no longer fire it (including records queued in the same task as teardown).

Before

pnpm run test-unit packages/lexical-table/src/__tests__/unit/LexicalTableObserver.test.ts (new tests against unmodified LexicalTableObserver.ts):

 ❯ |unit| packages/lexical-table/src/__tests__/unit/LexicalTableObserver.test.ts (2 tests | 2 failed) 134ms
     × disconnects the tracking MutationObserver in removeListeners() 116ms
     × does not fire the tracking MutationObserver for mutations of the detached table after editor teardown 17ms

 FAIL  |unit| ... > disconnects the tracking MutationObserver in removeListeners()
AssertionError: expected +0 to be 1 // Object.is equality
 Test Files  1 failed (1)
      Tests  2 failed (2)

After

pnpm run test-unit packages/lexical-table:

 Test Files  13 passed (13)
      Tests  158 passed (158)

pnpm run tsc, npx prettier --check and npx eslint on the changed files all pass.

🤖 Generated with Claude Code

…moveListeners

## Description

`TableObserver.trackTable()` creates a `MutationObserver` on the table
element (`attributes` + `childList` + `subtree`), but nothing ever calls
`observer.disconnect()`. `removeListeners()` aborts the DOM event
listeners and undoes the command registrations, but the tracking
observer outlives the `TableObserver` and the editor itself. After
teardown, any mutation of the detached `<table>` subtree fires the
leaked observer, whose callback calls `$getTableAndElementByKey()` and
throws invariant meta#232 (`TableObserver: Expected to find TableElement in
DOM for key %s`) from a MutationObserver microtask — uncatchable by
`onError` or React error boundaries. It also leaks one observer plus
the retained table subtree per table per editor mount.

This change registers `observer.disconnect()` in `listenersToRemove`,
so `removeListeners()` disconnects the tracking observer — mirroring
what `resetEditor()` already does for the core mutation observer.
`disconnect()` also empties the observer's record queue, which closes
the race where records queued in the same task as teardown would still
be delivered afterwards. No behavioral change while the editor is
alive.

Closes meta#9073

## Test plan

Added `packages/lexical-table/src/__tests__/unit/LexicalTableObserver.test.ts`
covering both that `removeListeners()` disconnects the tracking
observer and that mutations of the detached table after editor teardown
no longer fire it (including records queued in the same task as
teardown).

### Before

`pnpm run test-unit packages/lexical-table/src/__tests__/unit/LexicalTableObserver.test.ts`
(new tests against unmodified `LexicalTableObserver.ts`):

```
 ❯ |unit| packages/lexical-table/src/__tests__/unit/LexicalTableObserver.test.ts (2 tests | 2 failed) 134ms
     × disconnects the tracking MutationObserver in removeListeners() 116ms
     × does not fire the tracking MutationObserver for mutations of the detached table after editor teardown 17ms

 FAIL  |unit| ... > disconnects the tracking MutationObserver in removeListeners()
AssertionError: expected +0 to be 1 // Object.is equality
 Test Files  1 failed (1)
      Tests  2 failed (2)
```

### After

`pnpm run test-unit packages/lexical-table`:

```
 Test Files  13 passed (13)
      Tests  158 passed (158)
```

`pnpm run tsc`, `npx prettier --check` and `npx eslint` on the changed
files all pass.

Co-Authored-By: Claude Fable 5 <[email protected]>
@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Aug 19, 2026
@vercel

vercel Bot commented Aug 19, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
lexical Ready Ready Preview Aug 19, 2026 2:34pm
lexical-playground Ready Ready Preview Aug 19, 2026 2:34pm

Request Review

@etrepum
etrepum added this pull request to the merge queue Aug 19, 2026
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to no response for status checks Aug 19, 2026
@etrepum
etrepum added this pull request to the merge queue Aug 19, 2026
Merged via the queue into meta:main with commit 42cf489 Aug 19, 2026
48 checks passed
@etrepum etrepum mentioned this pull request Aug 31, 2026

This branch was successfully deployed

2 active deployments
Preview – lexical — d8d42e0f Deployed Aug 19, 2026 by vercel[bot]
Preview – lexical-playground — d8d42e0f Deployed Aug 19, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. extended-tests Run extended e2e tests on a PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: TableObserver.trackTable() MutationObserver is never disconnected — uncaught invariant #232 after editor teardown

2 participants