-
Notifications
You must be signed in to change notification settings - Fork 2.4k
fix(viewer): defer geometry disposal to avoid WebGPU dispose race (Sentry MONOREPO-EDITOR-DK/EG/EH) #468
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
anton-pascal
wants to merge
2
commits into
main
Choose a base branch
from
fix/sentry-EDITOR-DK
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
fix(viewer): defer geometry disposal to avoid WebGPU dispose race (Sentry MONOREPO-EDITOR-DK/EG/EH) #468
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| import type { BufferGeometry } from 'three' | ||
|
|
||
| /** | ||
| * WebGPU dispose-race mitigation. | ||
| * | ||
| * Disposing a `BufferGeometry` that is *still referenced by a live, rendered | ||
| * mesh* synchronously inside a `useFrame` rebuild loop is unsafe on the | ||
| * WebGPU backend. Three's WebGPU renderer keeps a `RenderObject` that holds | ||
| * the geometry's attributes; `BufferGeometry.dispose()` synchronously fires | ||
| * the `dispose` event, and the renderer's handler later reads `.id` / `.count` | ||
| * off attributes that have already been freed by the time the render pass | ||
| * unwinds — throwing `Cannot read properties of undefined (reading 'id')` | ||
| * (Sentry MONOREPO-EDITOR-DK / EG / EH). | ||
| * | ||
| * The renderer releases its `RenderObject` for a geometry once that geometry | ||
| * is no longer drawn (i.e. after the frame in which we swapped in the new | ||
| * geometry). So instead of disposing the *outgoing* live geometry mid-frame, | ||
| * we hand it to this queue and flush it at the START of the next frame, after | ||
| * the render pass that dropped it has completed. Behavior is identical — | ||
| * every geometry is still freed, just one frame later, after the renderer has | ||
| * let go of it. | ||
| * | ||
| * This is deliberately module-global (not per-system): every geometry system | ||
| * shares one flush point, and the queue is drained wholesale each frame. Only | ||
| * use `queueGeometryDispose` for geometries that were attached to a *rendered* | ||
| * mesh. Transient CSG intermediates that never entered the scene graph should | ||
| * still be disposed synchronously — the renderer never held a RenderObject for | ||
| * them, so there is no race. | ||
| */ | ||
| const pendingGeometryDisposals = new Set<BufferGeometry>() | ||
|
|
||
| /** | ||
| * Queue a live geometry for disposal after the current render. Safe to call | ||
| * with the same geometry more than once (Set-deduped). The mesh should already | ||
| * have been detached / reassigned to a fresh geometry before calling this. | ||
| */ | ||
| export function queueGeometryDispose(geometry: BufferGeometry | null | undefined): void { | ||
| if (!geometry) return | ||
| if (typeof (geometry as { dispose?: () => void }).dispose !== 'function') return | ||
| pendingGeometryDisposals.add(geometry) | ||
| } | ||
|
|
||
| /** | ||
| * Dispose everything queued since the last flush. Called once per frame by | ||
| * `GeometryDisposalFlushSystem` at a negative `useFrame` priority, before any | ||
| * rebuild system runs, so the renderer has had a full frame to release the | ||
| * outgoing geometries' RenderObjects. Do not call this from a rebuild | ||
| * system's `useFrame` — doing so risks disposing geometries queued by another | ||
| * system in the same frame, before the render pass. | ||
| */ | ||
| export function flushGeometryDisposals(): void { | ||
| if (pendingGeometryDisposals.size === 0) return | ||
| for (const geometry of pendingGeometryDisposals) { | ||
| try { | ||
| geometry.dispose() | ||
| } catch { | ||
| // A geometry may already be torn down (scene unload, HMR); freeing it | ||
| // twice is harmless and must never break the frame loop. | ||
| } | ||
| } | ||
| pendingGeometryDisposals.clear() | ||
| } |
30 changes: 30 additions & 0 deletions
30
packages/viewer/src/systems/geometry/geometry-disposal-flush.tsx
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| import { useFrame } from '@react-three/fiber' | ||
| import { flushGeometryDisposals } from '../../lib/deferred-dispose' | ||
|
|
||
| /** | ||
| * Single, authoritative flush point for the deferred geometry-disposal queue | ||
| * (Sentry MONOREPO-EDITOR-DK/EG/EH). | ||
| * | ||
| * Correctness depends on the flush running *before any rebuild system queues a | ||
| * new disposal in the same frame*. If a rebuild system (GeometrySystem, | ||
| * RoofSystem, …) both flushed and queued from its own `useFrame`, a system | ||
| * running later in the frame could flush geometries that an earlier system had | ||
| * just queued *this* frame — disposing them before the render pass and | ||
| * reintroducing the very race deferral is meant to prevent. | ||
| * | ||
| * R3F runs `useFrame` callbacks in ascending `priority` order (ties broken by | ||
| * mount order). Running this flush at a strongly negative priority guarantees | ||
| * it executes ahead of every rebuild system every frame, regardless of mount | ||
| * order, so the queue only ever contains geometries dropped on a *previous* | ||
| * frame — which the renderer has already released. This must be the ONLY | ||
| * caller of `flushGeometryDisposals`. | ||
| */ | ||
| const FLUSH_PRIORITY = -1000 | ||
|
|
||
| export const GeometryDisposalFlushSystem = () => { | ||
| useFrame(() => { | ||
| flushGeometryDisposals() | ||
| }, FLUSH_PRIORITY) | ||
|
|
||
| return null | ||
| } |
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
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Flush unmount strands disposal queue
Medium Severity
The
GeometryDisposalFlushSystemis a React component that drains the module-globalpendingGeometryDisposalsqueue viauseFrame. If this component unmounts (e.g., after a scene error or Canvas unmount), theuseFramecallback stops, leaving the global queue undrained. This causes GPU resources for queued geometries to leak, persisting until a full page reload.Additional Locations (1)
packages/viewer/src/systems/geometry/geometry-disposal-flush.tsx#L23-L30Reviewed by Cursor Bugbot for commit cb566be. Configure here.