-
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 1 commit
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| 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. Call once at the top of a | ||
| * `useFrame` callback, before any rebuild work runs, so the renderer has had a | ||
| * full frame to release the outgoing geometries' RenderObjects. | ||
| */ | ||
| 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() | ||
| } |
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.
Uh oh!
There was an error while loading. Please reload this page.