fix(usbraw): release claimed interface on open() error paths#369
Conversation
UsbrawDevice.open() registered its release/close/exit errdefer only after self.* was populated, so a pipe2 or allocator failure between a successful libusb_claim_interface and that point returned without releasing the interface or exiting the context — leaking the claim and leaving the device ownerless. Move the cleanup errdefer to immediately after the successful claim and re-attach the kernel driver, matching close(). Also re-attach the kernel driver in the UsbrawSuppress alloc-failure path for the same reason.
Qodo reviews are paused for this user.Troubleshooting steps vary by plan Learn more → On a Teams plan? Using GitHub Enterprise Server, GitLab Self-Managed, or Bitbucket Data Center? |
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (1)
📝 WalkthroughWalkthroughThis PR restructures error-handling in USB device initialization by moving the cleanup guard point earlier in the open flow. After successfully claiming the USB interface in ChangesUSB device initialization cleanup
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Possibly related PRs
Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Summary
Resource-leak fix surfaced by an adversarial review of the #355 work.
UsbrawDevice.open()registered itsrelease_interface+close+exiterrdefer only afterself.*was populated. Apipe2(std.posix.pipe2) or allocator (alloc.create) failure between a successfullibusb_claim_interfaceand that point returned an error without releasing the interface or exiting the context — leaking the claim and leaving the USB device ownerless (no kernel driver, not claimed by us).Fix
pipe2andalloc.createfailure paths, and re-attach the kernel driver in it (matchingclose()and theBUSY/ClaimFailedbranches).UsbrawSuppress.openSuppress()already released on alloc failure but did not re-attach the kernel driver; add the re-attach for the same consistency.Error-path only; no change to the success path.
Test plan
zig build -Dlibusb=truecompiles clean;zig build test -Dlibusb=true(full suite) green in the canonical Docker image.zig build test-tsan -Dlibusb=truegreen in Docker.Summary by CodeRabbit