Repository navigation
Conversation
A DeviceIoControl sent to a wedged storage device can block forever, which hangs any thread doing storage enumeration. OpenDevice already runs behind a worker with a timeout and a per-path cooldown; raw IOCTL calls did not and would stall indefinitely. Route SendRawIoControl through the same pattern: the blocking DeviceIoControl runs on an IoControlWorker thread, the caller waits with IoControlTimeoutMilliseconds (default 30 s). On timeout the worker is abandoned and replaced, the device path (tracked from the handle that opened it) enters IoControlTimeouts for IoControlTimeoutCooldownMilliseconds (default 5 min), and the call returns false with LastIoControlError = ERROR_TIMEOUT so existing failure paths skip the device. Setting IoControlTimeoutMilliseconds to 0 or below restores the direct synchronous call.
There was a problem hiding this comment.
Copilot review overview
🔵 Needs a closer look
The change adds cross-thread native-IOCTL handling with abandoned-worker and late-completion buffer semantics on a UI-critical path, and the author notes the full multi-TFM build and runtime repro have not yet been verified, so human review is warranted.
Review effort: Balanced
Findings: None
What changed in this PR
This PR hardens WindowsStorageIoControl.SendRawIoControl against wedged storage devices whose driver never completes a DeviceIoControl IRP. Previously such a device blocked the calling thread (and, via LibreHardwareMonitor/FanControl, the WPF UI thread) indefinitely. The change mirrors the already-existing OpenDevice protection: the blocking IOCTL now runs on a dedicated background worker thread with a timeout, and a per-device-path cooldown quarantines a device that times out so subsequent calls skip it immediately.
Changes:
- Added
IoControlWorkerandIoControlRequest(structural copies of the existingOpenDeviceWorker/OpenDeviceRequest) to runDeviceIoControloff-thread with abandon-on-timeout semantics. - Reworked
SendRawIoControlto check a cooldown, run the core call directly whenIoControlTimeoutMilliseconds <= 0, or dispatch through the worker with a timeout; extractedSendRawIoControlCore. - Generalized the open-path cooldown helpers into shared
IsPathInTimeoutCooldown/RememberPathTimeout, added aConditionalWeakTable<SafeFileHandle,string>to recover a handle's device path, and exposedIoControlTimeoutMilliseconds/IoControlTimeoutCooldownMillisecondsknobs.
| File | Description |
|---|---|
| DiskInfoToolkit/Core/WindowsStorageIoControl.cs | Adds IOCTL timeout/cooldown fields, knobs, worker dispatch, handle→path tracking, and shared cooldown helpers; extracts SendRawIoControlCore. |
| DiskInfoToolkit/Core/Windows/IoControlWorker.cs | New background worker thread that executes the blocking IOCTL and supports abandonment on timeout. |
| DiskInfoToolkit/Core/Windows/IoControlRequest.cs | New request object carrying IOCTL parameters/results with completion, timeout, and disposal synchronization. |
I reviewed the concurrency model (global lock held across Wait, Complete/Dispose ordering, memory visibility via the completion event), the cooldown key handling (null-safe TrimStorageString, OrdinalIgnoreCase, weak-table path recovery), handle uniqueness for ConditionalWeakTable.Add, and LastIoControl* state on every return path. The implementation faithfully mirrors the existing, working OpenDevice machinery, and I found no high-confidence defects that differ from that established pattern. Note that the timeout/worker machinery (both the pre-existing open path and this new IOCTL path) has no direct unit tests — tests exercise a mock IStorageIoControl — so no test-coverage comment is warranted here.
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
|
Follow-up on the verification status: the library now compiles cleanly against all six target frameworks — Method note: the affected machine has no .NET SDK, so this was done with Roslyn 4.14 ( Remaining open item is the runtime verification against the failing reader that produced the trace in the description. |
|
While I don't think this is ready to be merged, it does explain a possible issue.
Could you please state which hardware exactly this is ? |
|
Sure — here is everything identifiable about the hardware:
Failure signature: the device storms Event Log with |
Unfortunately that does not help for further analysis. It is quite weird that it is stuck in the I'm going to try something else, perhaps that works properly. Could you please verify the same issue persists when you execute DiskInfoViewer (take from latest release or compile yourself; click Refresh button once after startup) ? |
|
Baseline verification done, as requested (reporting back from this thread). The release asset download ( Result while the device is healthy: no hang, fastThe machine rebooted since the incident; the RTL9210B enclosure re-enumerated and is currently in a good phase (PnP status OK on both bridge and disk, zero Process exited cleanly (code 0), ~11.7 s wall clock (10 s of that is the test's deliberate device-change wait). Every stage — device opens, geometry/descriptor/scsi-address ioctls, USB probe strategy, refresh — completed normally against the problem device. Two observations from the healthy dump that may be relevant:
What this means for the original issueThe hang does not reproduce while the device is in its healthy phase — consistent with it being intermittent across reboots (it had wedged twice before the config workaround). The wedged state can't be triggered on demand right now (it arises on its own, typically after the device has been powered a while / after standby); catching it would mean leaving an enumeration loop or the viewer running until it flips, which I can do if useful. The unprotected synchronous |
|
Unfortunately all of this is useless as it is not what I requested. Please also check yourself what your AI is about to reply and if it makes sense or if it is just bloating stuff up. |
|
Please try current master with DiskInfoViewer if you are able to reproduce the issue as previously described. |
Problem
SendRawIoControlperforms a synchronousDeviceIoControlwith no timeout and no cancellation. When a storage device is wedged — for example a failing USB bridge whose driver never completes the IRP — the call blocks forever, and so does every caller above it.This is not theoretical. FanControl V281 embeds DiskInfoToolkit through LibreHardwareMonitor, and a real-world failure mode is the app freezing at startup with the main window never appearing. A
dotnet-dumpof the hung process shows the WPF UI thread parked inside the IOCTL indefinitely (trigger: a failing Realtek USB card reader,USB\VID_0BDA&PID_9210, behind which a portable SSD stopped answering):The stuck thread sits in kernel, which also makes the hung process unkillable. The same holds for LHM's periodic storage refresh in long-running instances.
OpenDevicealready protects itself against exactly this class of device with a worker thread + timeout + per-path cooldown. Raw IOCTLs deserve the same treatment.Solution
Mirror the existing
OpenDeviceWorker/OpenDeviceRequestmachinery for IOCTLs:SendRawIoControlnow runs the blockingDeviceIoControlon anIoControlWorkerthread and waits up toIoControlTimeoutMilliseconds(default 30 s).IoControlTimeoutscooldown forIoControlTimeoutCooldownMilliseconds(default 5 min), and the call returnsfalsewithLastIoControlError = ERROR_TIMEOUT (1460). Every existingTryGet*/ pass-through failure path then simply skips the device — no API or caller changes needed.ConditionalWeakTable<SafeFileHandle, string>populated byOpenDevice, so the quarantine survives across enumeration rounds andWindowsStorageIoControlinstances (a re-opened device is skipped immediately instead of hanging again).IoControlTimeoutMilliseconds <= 0restores the previous direct synchronous call, following the same convention asOpenDeviceTimeoutMilliseconds.IsPathInTimeoutCooldown/RememberPathTimeout); behavior on the open side is unchanged.New public knobs:
WindowsStorageIoControl.IoControlTimeoutMillisecondsandWindowsStorageIoControl.IoControlTimeoutCooldownMilliseconds.Trade-offs (the same ones the open path already accepts)
falsereturn, so this is benign — same reasoning as the abandoned open path.Testing
dotnet buildhas not been run yet (no SDK on the affected machine); happy to iterate on any CI findings.Related: LibreHardwareMonitor calls
Storage.GetDisks()synchronously fromStorageGroup.AddHardware, and FanControl runs that whole chain on its UI thread at startup. This PR fixes the library level; the caller-side threading is tracked separately with the FanControl project.