Skip to content

fix(usb_host): Handle disconnect after device free#485

Open
igi540 wants to merge 1 commit into
masterfrom
fix/hub_root_port_disconnect_race
Open

fix(usb_host): Handle disconnect after device free#485
igi540 wants to merge 1 commit into
masterfrom
fix/hub_root_port_disconnect_race

Conversation

@igi540
Copy link
Copy Markdown
Collaborator

@igi540 igi540 commented Apr 27, 2026

Description

Fixes a USB Host race where a device can be freed while it is still physically connected. In this flow, the recycle path can remove the dev tree node before the root port disconnect event is handled, which made dev_tree_node_dev_gone() return ESP_ERR_NOT_FOUND and abort through ESP_ERROR_CHECK.

This change treats ESP_ERR_NOT_FOUND as benign for that already-recycled disconnect path while keeping error checking for unexpected failures. It also adds a no-client regression test for disconnect-after-device-free and documents the fix in the changelog.

Related

Closes espressif/esp-idf#18366
Closes IDFGH-17399

Testing

  • Added target regression test: Test USB Host disconnect after device free (no client)

@igi540 igi540 self-assigned this Apr 27, 2026
@igi540 igi540 force-pushed the fix/hub_root_port_disconnect_race branch 2 times, most recently from af2beef to b9eea2e Compare April 28, 2026 14:37
@igi540 igi540 changed the title fix(host/usb): Fix root port disconnect race after device free fix(usb_host): Fix root port disconnect race after device free Apr 28, 2026
@igi540 igi540 force-pushed the fix/hub_root_port_disconnect_race branch 2 times, most recently from 28445e2 to 81266f2 Compare April 28, 2026 17:07
@igi540 igi540 changed the title fix(usb_host): Fix root port disconnect race after device free fix(usb_host): Handle disconnect after device free Apr 28, 2026
@igi540 igi540 added the bug 🐞 Inconsistencies or issues which will cause a problem for users or implementers. label Apr 28, 2026
@igi540 igi540 requested a review from tore-espressif April 28, 2026 17:23
Copy link
Copy Markdown
Collaborator

@tore-espressif tore-espressif left a comment

Choose a reason for hiding this comment

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

@igi540 Thank you for the well documented test case!

@igi540 igi540 force-pushed the fix/hub_root_port_disconnect_race branch from 81266f2 to 8942736 Compare April 29, 2026 09:01
@igi540 igi540 marked this pull request as ready for review April 29, 2026 10:18
Copilot AI review requested due to automatic review settings April 29, 2026 10:18
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Fixes a USB Host race where a root-port disconnect event can be handled after the device has already been freed/recycled, previously causing an abort when dev_tree_node_dev_gone() returned ESP_ERR_NOT_FOUND.

Changes:

  • Treat ESP_ERR_NOT_FOUND from dev_tree_node_dev_gone() as benign in the root-port disconnect path.
  • Add a no-client target regression test that reproduces disconnect-after-device-free timing.
  • Document the fix in the USB Host component changelog.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

File Description
host/usb/src/hub.c Avoid aborting on ESP_ERR_NOT_FOUND when a disconnect arrives after the device node has already been recycled.
host/usb/test/target_test/usb_host/main/test_usb_host_plugging.c Adds a no-client regression test covering the disconnect-after-free flow.
host/usb/CHANGELOG.md Adds an Unreleased changelog entry documenting the race fix.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread host/usb/src/hub.c
Comment thread host/usb/test/target_test/usb_host/main/test_usb_host_plugging.c
When a device is freed while still connected, the recycle path can remove the dev tree node before the root port disconnect event is processed. Treat ESP_ERR_NOT_FOUND from dev_tree_node_dev_gone() as benign in that case.

Add a regression test for the no-client path and document the fix in the changelog.

Closes espressif/esp-idf#18366
@igi540 igi540 force-pushed the fix/hub_root_port_disconnect_race branch from 8942736 to 7e9d13d Compare May 6, 2026 20:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug 🐞 Inconsistencies or issues which will cause a problem for users or implementers.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

USB hub (root_port_handle_events) cannot find device on disconnect and aborts due to ESP_ERR_NOT_FOUND (0x105)

3 participants