Skip to content

Fix system hang on USB/removable device removal - #93

Merged
bobranten merged 2 commits into
bobranten:masterfrom
HorusGod007:fix/prevent-hang-on-device-removal
Mar 5, 2026
Merged

Fix system hang on USB/removable device removal#93
bobranten merged 2 commits into
bobranten:masterfrom
HorusGod007:fix/prevent-hang-on-device-removal

Conversation

@HorusGod007

@HorusGod007 HorusGod007 commented Mar 4, 2026

Copy link
Copy Markdown

Summary

  • block.c: Add 30-second timeouts to all KeWaitForSingleObject calls that wait for I/O completion. On timeout, cancel pending IRPs via IoCancelIrp and return STATUS_IO_TIMEOUT instead of hanging forever.
  • pnp.c: Move SetLongFlag(Vcb->Flags, VCB_DEVICE_REMOVED) earlier in both Ext2PnpRemove and Ext2PnpSurpriseRemove, before sending the IRP down and dismounting. Add 30-second timeouts to the PnP IRP waits in Remove, SurpriseRemove, and QueryRemove paths.
  • fsctl.c: Add 30-second timeout to Ext2IsMediaWriteProtected which also waited indefinitely for the lower driver.
  • read.c / write.c: Add VCB_DEVICE_REMOVED flag checks early in Ext2Read and Ext2Write, returning STATUS_NO_SUCH_DEVICE immediately to prevent new I/O from being issued to a removed device.

Problem

When a USB drive formatted with ext2/ext3/ext4 is physically removed while the Ext4Fsd driver has outstanding I/O, the system hangs indefinitely. This happens because KeWaitForSingleObject calls across multiple code paths wait with NULL timeout (infinite) for I/O completions that will never arrive from the now-removed device. Additionally, the VCB_DEVICE_REMOVED flag was set too late in the PnP removal path, after dismount, so concurrent threads had no way to detect the removal early.

When a USB drive with an ext4 filesystem is removed while the driver has
outstanding I/O, the system hangs indefinitely because
KeWaitForSingleObject calls block forever waiting for completions that
will never arrive from the removed device.

This commit fixes the issue with three changes:

- block.c: add 30-second timeouts to all KeWaitForSingleObject calls and
  cancel pending IRPs on timeout instead of waiting forever
- pnp.c: set VCB_DEVICE_REMOVED flag early in both PnpRemove and
  PnpSurpriseRemove so concurrent I/O threads can fail fast
- read.c, write.c: check VCB_DEVICE_REMOVED flag before issuing new I/O
  and return STATUS_NO_SUCH_DEVICE immediately
@HorusGod007
HorusGod007 force-pushed the fix/prevent-hang-on-device-removal branch from 5f1270b to d0f12d7 Compare March 4, 2026 13:26
@HorusGod007 HorusGod007 closed this Mar 4, 2026
@HorusGod007 HorusGod007 reopened this Mar 4, 2026
@HorusGod007 HorusGod007 closed this Mar 4, 2026
@bobranten

bobranten commented Mar 4, 2026 via email

Copy link
Copy Markdown
Owner

The previous commit missed three infinite KeWaitForSingleObject calls:

- pnp.c: Ext2PnpRemove and Ext2PnpSurpriseRemove wait forever for the
  lower driver to complete the PnP IRP after device removal
- fsctl.c: Ext2IsMediaWriteProtected waits forever for the
  write-protect check IRP to complete

All three now use 30-second timeouts with IRP cancellation on timeout,
matching the pattern used in block.c.
@HorusGod007 HorusGod007 reopened this Mar 4, 2026
@HorusGod007

Copy link
Copy Markdown
Author

This looks like a reasonable change, are you finished so I should aply it?

yes just finished and tested in test mode. all good now.

@bobranten
bobranten merged commit 60927ae into bobranten:master Mar 5, 2026
@God-damnit-all

Copy link
Copy Markdown

Excuse me, my system is having this issue. Do you plan on releasing an update to Ext4Fsd sometime in the near future? I would really like to take advantage of this fix.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants