---
title: "Detect and simulate GPU device loss in C#"
url: https://recursiveai.org/onegpu/cookbook/detect-and-test-gpu-device-loss/
description: "Subscribe to a GPU device lost event in C#, inspect OneGpu’s loss reason, and test the notification on Vulkan without resetting the driver."
version: "1.1.0"
---

# Detect and simulate GPU device loss in C#

**OneGpu** is a free, open-source GPU compute library for .NET from Recursive AI. MIT or Apache-2.0.

[NuGet](https://www.nuget.org/packages/RecursiveAI.OneGpu.Backends) · [GitHub](https://github.com/recursive-ai-oss/onegpu-csharp) · [Docs](https://recursiveai.org/onegpu/docs/) · [Download example (.zip)](https://recursiveai.org/onegpu/cookbook/detect-and-test-gpu-device-loss/detect-and-test-gpu-device-loss.zip)

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.

```shell
dotnet add package RecursiveAI.OneGpu.Backends
```

Requires [.NET 10](https://dotnet.microsoft.com/download/dotnet/10.0) (net10.0); .NET 9 and earlier aren’t supported.

```csharp
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](https://recursiveai.org/onegpu/cookbook/detect-and-test-gpu-device-loss/detect-and-test-gpu-device-loss.zip) 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:

```text
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.

```csharp
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}");
```

```text
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:

```csharp
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

- [Recover from GPU compute device loss](https://recursiveai.org/onegpu/cookbook/recover-from-compute-device-loss/)
- [Choose a Vulkan physical device](https://recursiveai.org/onegpu/cookbook/pick-a-vulkan-device/)

## 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](https://recursiveai.org/onegpu/docs/compute/#device-loss) 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](https://recursiveai.org/onegpu/docs/compute/#every-wait-on-the-gpu-per-backend).
