fix(core): guard lifecycle passes loop against destroyed renderables#882
Open
anerli wants to merge 1 commit intoanomalyco:mainfrom
Open
fix(core): guard lifecycle passes loop against destroyed renderables#882anerli wants to merge 1 commit intoanomalyco:mainfrom
anerli wants to merge 1 commit intoanomalyco:mainfrom
Conversation
kommander
reviewed
Apr 5, 2026
Comment on lines
+500
to
-504
| super.destroy() | ||
| this.textBuffer.setSyntaxStyle(null) | ||
| this._textBufferSyntaxStyle.destroy() | ||
| this.textBufferView.destroy() | ||
| this.textBuffer.destroy() | ||
| super.destroy() |
Collaborator
There was a problem hiding this comment.
There was something very critical about this order... 🤔 can't remember exactly, but my alarm bells are ringing.
Collaborator
|
/review especially the destroy order for TextBufferRenderable, that order was critical. Check how it came to be this exact way and if it was changed around over time. |
| @@ -0,0 +1,35 @@ | |||
|
|
|||
| import { test, expect, beforeEach, afterEach, describe } from "bun:test" | |||
There was a problem hiding this comment.
The expect import is unused in this test file. Since this is a crash-prevention test (verifying that no exception is thrown), you could either remove the unused import, or add an explicit assertion like expect(text.isDestroyed).toBe(true) to make the test expectations clearer.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
fix(core): guard lifecycle passes loop against destroyed renderables
Problem
During file-write streaming in a React-based TUI app, the renderer intermittently crashes with
TextBuffer is destroyed. The crash is nondeterministic — it depends on event loop scheduling between React's commit phase and the async render loop.The crash occurs in
RootRenderable.render()when the lifecycle passes loop callsonLifecyclePass()on aTextRenderablewhoseTextBufferhas already been destroyed by a React unmount.Root Cause
The render loop's lifecycle passes loop has no
isDestroyedguard:The codebase already guards against this in 15+ other places, including the render commands loop just lines later:
Additionally,
TextBufferRenderable.destroy()destroys the nativeTextBufferbefore callingsuper.destroy(), which is what setsisDestroyed = trueand unregisters from the lifecycle passes Set. This creates a window where the buffer is dead but the renderable still appears alive.Fix
Guard the lifecycle passes loop — skip destroyed renderables, matching the existing pattern used throughout the codebase.
Reorder
TextBufferRenderable.destroy()— callsuper.destroy()first soisDestroyedis set and the renderable is unregistered from lifecycle passes before native resources are freed. This closes the window where the buffer is dead but the renderable looks alive.Test
Added
renderer.lifecycle.test.tswith a regression test that reproduces the crash by simulating a destroyedTextRenderableremaining in the lifecycle passes Set. Confirmed the test fails without the fix and passes with it.All 3985 existing tests continue to pass.