Skip to content

fix(disk): skip drives returning ERROR_INVALID_FUNCTION or ERROR_NOT_SUPPORTED in IOCounters on Windows#2003

Merged
shirou merged 1 commit intoshirou:masterfrom
jose-manuel-almaza:fix/disk-iocounters-skip-unsupported-errors
Feb 19, 2026
Merged

fix(disk): skip drives returning ERROR_INVALID_FUNCTION or ERROR_NOT_SUPPORTED in IOCounters on Windows#2003
shirou merged 1 commit intoshirou:masterfrom
jose-manuel-almaza:fix/disk-iocounters-skip-unsupported-errors

Conversation

@jose-manuel-almaza
Copy link
Copy Markdown
Contributor

Summary

On Windows, IOCountersWithContext calls DeviceIoControl with IOCTL_DISK_PERFORMANCE for each fixed drive. If the call fails with any error, the function immediately returns, discarding all IO counters collected so far — including those from drives that succeeded.

This causes disk.IOCounters() to fail entirely on systems where at least one drive does not support IOCTL_DISK_PERFORMANCE, such as:

Fix

Skip drives that return ERROR_INVALID_FUNCTION or ERROR_NOT_SUPPORTED from DeviceIoControl and continue to the next drive. This matches the behavior of Python's psutil, which this code references:

// psutil/arch/windows/disk.c
else if (GetLastError() == ERROR_INVALID_FUNCTION) {
    psutil_debug("DeviceIoControl -> ERROR_INVALID_FUNCTION; ignore PhysicalDrive%i", devNum);
    goto next;
}
else if (GetLastError() == ERROR_NOT_SUPPORTED) {
    psutil_debug("DeviceIoControl -> ERROR_NOT_SUPPORTED; ignore PhysicalDrive%i", devNum);
    goto next;
}

Reference: https://github.com/giampaolo/psutil/blob/544e9daa4f66a9f80d7bf6c7886d693ee42f0a13/psutil/arch/windows/disk.c#L146-L163

Fixes #1461

…SUPPORTED in IOCounters on Windows

DeviceIoControl with IOCTL_DISK_PERFORMANCE may return ERROR_INVALID_FUNCTION
or ERROR_NOT_SUPPORTED for drives that do not support disk performance counters
(e.g. virtual drives like Google Drive, or Windows Server 2016 with disk
performance counters disabled). Previously this caused IOCountersWithContext to
return a fatal error, discarding IO stats already collected for other drives.

This change skips the unsupported drive and continues iterating, matching the
behavior of Python psutil:
https://github.com/giampaolo/psutil/blob/master/psutil/arch/windows/disk.c#L146-L163

Fixes shirou#1461
Copy link
Copy Markdown
Owner

@shirou shirou left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

Great fix — clean, minimal, and nicely aligned with psutil's behavior. The comment and reference to the psutil source are much appreciated. Thanks for tracking this down and fixing it!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

disk.IOCounters() returns ERROR_INVALID_FUNCTION when Google Drive is mounted as a disk in Windows.

2 participants