-
Notifications
You must be signed in to change notification settings - Fork 66
wait_for API: Boolean to Result #286
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
Vanuan
wants to merge
8
commits into
kvark:main
Choose a base branch
from
Vanuan:wait_for_result
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
Conversation
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
Vanuan
commented
Nov 29, 2025
| "Metal command buffer error".to_string(), | ||
| )) | ||
| } | ||
| _ => {} |
Author
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.
| State | Meaning |
|---|---|
metal::MTLCommandBufferStatus::NotEnqueued |
A command buffer's initial state, which indicates its command queue isn't reserving a place for it. You can modify a command buffer in this state by encoding commands to it, or by adding a state change handler. |
metal::MTLCommandBufferStatus::Enqueued |
A command buffer's second state, which indicates its command queue is reserving a place for it. You can modify a command buffer in this state by encoding commands to it, or by adding a state change handler. |
metal::MTLCommandBufferStatus::Committed |
A command buffer's third state, which indicates the command queue is preparing to schedule the command buffer by resolving its dependencies. You can't modify a command buffer in this state. |
metal::MTLCommandBufferStatus::Scheduled |
A command buffer's fourth state, which indicates the command buffer has its resources ready and is waiting for the GPU to run its commands. You can't modify a command buffer in this state. |
metal::MTLCommandBufferStatus::Completed |
A command buffer's successful, final state, which indicates the GPU finished running the command buffer's commands without any problems. |
metal::MTLCommandBufferStatus::Error |
A command buffer's error state, which indicates the GPU encountered an error while running the command buffer's commands. |
Author
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.
| Error Code | Description |
|---|---|
none |
An error code that represents the absence of any problems. |
timeout |
An error code that indicates the system interrupted and terminated the command buffer before it finished running. |
pageFault |
An error code that indicates the command buffer generated a page fault the GPU can't service. |
notPermitted |
An error code that indicates a process doesn't have access to a GPU device. |
outOfMemory |
An error code that indicates the GPU device doesn't have sufficient memory to execute a command buffer. |
invalidResource |
An error code that indicates the command buffer has an invalid reference to resource. |
memoryless |
An error code that indicates the GPU ran out of one or more of its internal resources that support memoryless render pass attachments. |
deviceRemoved |
An error code that indicates a person physically removed the GPU device before the command buffer finished running. |
stackOverflow |
An error code that indicates the GPU terminated the command buffer because a kernel function of tile shader used too many stack frames. |
accessRevoked |
An error code that indicates the system has revoked the Metal device's access because it's responsible for too many timeouts or hangs. |
internal |
An error code that indicates the Metal framework has an internal problem. |
Author
|
@kvark ready to review |
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.
GPU Wait API Migration: Boolean to Result Implementation
Fixes #248 | Inspired by zed-industries/zed#43070
Problem
The
wait_forAPI provides GPU synchronization by blocking until the GPU reaches a specific sync point, preventing the CPU from destroying or reusing resources while they're still in use. When you submit work to the GPU (like rendering a frame), it continues running asynchronously. Without proper synchronization, the CPU could delete textures or reuse buffers while the GPU is still accessing them, causing memory corruption or crashes.Current limitation: Returns a simple boolean that loses critical error information:
false- no context for recoverySolution
Introduce
Result-based error handling while maintaining backward compatibility.Migration Strategy
Dual implementation approach:
wait_for()→bool(preserved, delegates to new method)wait_for_result()→Result<(), WaitError>(detailed errors)New Error Type
New Trait Method
Backend Implementations
Each backend maps its native error conditions to
WaitError:Ok(())TIMEOUTERROR_DEVICE_LOSTALREADY_SIGNALED/CONDITION_SATISFIEDTIMEOUT_EXPIREDMTLCommandBufferStatus::CompletedUsage Patterns
Frame Synchronization (Frame Pacer)
Before: Blind wait, no error handling
After: Conditional cleanup based on error
Buffer Reuse (Buffer Belt)
Before: Can't distinguish busy from error
After: Intelligent reuse decisions
Texture Cleanup (EGUI)
After: Partition textures by readiness state
Ok(())→ Delete immediatelyTimeout→ Keep for next frameShader Hot Reload
After: Block critical operations on device loss
Benefits
✅ Actionable Error Information: Distinguish timeouts (retry later) from device loss (unrecoverable)
✅ Safer Resource Management: Skip cleanup on device loss to prevent crashes
✅ Better Debugging: Detailed error messages from backend-specific failures
✅ Backward Compatible: Existing
wait_forcallers continue working unchanged✅ Foundation for Recovery: Enables future device loss recovery strategies
Migration Path
WaitErrorenum andwait_for_resultto traitwait_fordelegate towait_for_result