Subscribe to GpuDevice.Lost to find out when your device can no longer run GPU work. This example uses a discrete GPU with Vulkan 1.3.
dotnet add package RecursiveAI.OneGpu.BackendsRequires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.
using System; using RecursiveAI.OneGpu.Vulkan; using var gpu = VulkanGpu.CreateDevice( new() { Adapter = "discrete" }); gpu.Lost += (_, e) => Console.WriteLine(e.Error.Reason); gpu.SimulateLoss(); Console.WriteLine(gpu.IsLost);
The example is the full program below: unzip it and dotnet run. To run only the code above, paste it into Program.cs of a new dotnet new console project and dotnet run.
Prints:
Simulated True
The full program
This version records the device state before and after the simulation and adds a second handler after the loss. The two calls to SimulateLoss() produce only one notification for the original subscriber.
using System; using System.Threading; using RecursiveAI.OneGpu; using RecursiveAI.OneGpu.Vulkan; using var gpu = VulkanGpu.CreateDevice(new() { Adapter = "discrete", LostTimeout = args is ["--no-timeout"] ? Timeout.InfiniteTimeSpan : null, }); Console.WriteLine($"{gpu.Backend}: {gpu.Adapter.Name}"); Console.WriteLine($"Initially lost: {gpu.IsLost}"); gpu.Lost += (_, e) => Console.WriteLine($"Lost: {e.Error.Reason}"); gpu.SimulateLoss(); gpu.SimulateLoss(); // Same loss, no second event. Console.WriteLine($"IsLost: {gpu.IsLost}"); Console.WriteLine($"LostError: {gpu.LostError?.Reason}"); gpu.Lost += (_, e) => Console.WriteLine($"Late subscriber: {e.Error.Reason}");
Vulkan: NVIDIA GeForce RTX 4070 Initially lost: False Lost: Simulated IsLost: True LostError: Simulated Late subscriber: Simulated
VulkanGpu.CreateDevice throws GpuUnsupportedException if "discrete" matches no adapter or Vulkan 1.3 support is missing; if only the discrete GPU is missing, pass Adapter = null to use the best available adapter (unless ONEGPU_ADAPTER overrides it).
Once IsLost is true, calls that need the GPU throw GpuDeviceLostException instead of continuing on that device.
Variations
Disable the Vulkan no-progress timeout for this device. Replace the device creation in the snippet at the top:
using var gpu = VulkanGpu.CreateDevice( new() { Adapter = "discrete", LostTimeout = System.Threading.Timeout .InfiniteTimeSpan, });
The zip runs this setting with dotnet run -- --no-timeout. The simulated loss still fires. This switches off only the no-progress cutoff, not loss reported by the driver.
Related recipes
The short answer
With OneGpu 1.1.0, subscribe to GpuDevice.Lost and use e.Error.Reason to classify the loss. Read IsLost to check the state and LostError to get the exception outside the event handler. Call SimulateLoss() on a device dedicated to the test. Unlike throwing a demo exception in your own code, this changes OneGpu’s device state and delivers the notification through its normal loss path.
How can I simulate a GPU device lost event without a driver reset?
Create a real Vulkan device, attach the handler, then call SimulateLoss(). No shader or queued work is needed. The exception in the event has Reason = Simulated. The device stays lost, so don’t call this on the device your app is using for other jobs. This test shows what the application observes, not whether Vulkan reports a particular real driver fault.
The compute docs say the event is raised on the thread that found the loss. In this example that’s the thread that calls SimulateLoss(); in an application it could be a readback thread. If the handler updates a UI, post the update to the UI thread. IsLost is already true inside the callback. If the device is already lost, your handler runs inside the += line, so set up anything it uses before you subscribe.
Does a Vulkan GPU wait time out if the device stops making progress?
OneGpu’s Vulkan backend uses a timeline counter to track finished submissions. If a wait sees no advance for LostTimeout, it marks the device lost with Reason = NoProgress. The default is 10 seconds on hardware adapters and no cutoff on software adapters. The timer measures time without a newly finished submission, not the total time you’ve been waiting. A long queue can keep making progress without tripping it.
A Vulkan VK_ERROR_DEVICE_LOST result is separate and gives Reason = Reported. Timeout.InfiniteTimeSpan restores the unbounded no-progress wait, so use it only when you accept that a stalled queue might not return. It does not suppress Reported or Simulated loss. See the per-backend wait behavior.