From 8a9660442fbdc1c196407cbde3d2ed145a24412d Mon Sep 17 00:00:00 2001 From: "Jeffrey C. Ollie" Date: Sun, 23 Aug 2026 17:32:41 -0500 Subject: [PATCH] close the device when querying it fails next opened /dev/hidrawN and guarded it with an errdefer before asking for its DeviceInfo, but errdefer only runs when the scope is left by returning an error, and the query is followed by catch continue. Since both fallible calls in the loop are caught, next never returns an error at all, so that errdefer could not fire under any circumstances. A device that opened but would not answer HIDIOCGRAWINFO was left open and its descriptor leaked for the life of the process. Close it explicitly on that path instead, with a comment recording why the errdefer that used to be there did nothing, so it does not come back. Co-Authored-By: Claude Opus 5 (1M context) Claude-Session: https://claude.ai/code/session_01JRS7gtG7BeWuwFUf8xW4qS --- src/DeviceInfoIterator.zig | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/DeviceInfoIterator.zig b/src/DeviceInfoIterator.zig index ef6ea5f..405c948 100644 --- a/src/DeviceInfoIterator.zig +++ b/src/DeviceInfoIterator.zig @@ -45,8 +45,13 @@ pub fn next(self: *DeviceInfoIterator, io: std.Io) !?DeviceInfo { while (self.index < 64) { defer self.index += 1; const device = Device.open(io, self.index) catch continue; - errdefer device.close(io); - return device.getDeviceInfo(io) catch continue; + // An errdefer would not fire here, because failing to query the + // device continues the scan rather than returning an error, so the + // close has to be spelled out. + return device.getDeviceInfo(io) catch { + device.close(io); + continue; + }; } return null; } -- knotmirror 0.0.1