max3421 (ESP): the task-context interrupt handler can take mutexes hcd_deinit() deleted
Maintainers usually reply within 1 day
Nobody has claimed this yet.
Assessment
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Newbie friendliness
- 28/100
- Issue type
- Bug
- Clarity
- Clearly specified
- Activity status
- Active
- Tech stack
- c
- Domain
- embedded-iot
Research direction
Start by reading hcd_deinit() in src/portable/analog/max3421/hcd_max3421.c:571-589 alongside max3421_intr_task() in hw/bsp/espressif/boards/family.c:230-237, then trace where max3421_intr_sem is given and where the GPIO interrupt is disabled. The fix either stops and drains the task before the mutexes are deleted, or has the handler check a cleared running flag before locking; note osal_mutex_lock under FreeRTOS is xSemaphoreTake in src/osal/osal_freertos.h:224-226. Done means tuh_deinit() with a device mounted no longer reaches a NULL mutex, validated on a dual-core ESP32 with a MAX3421 attached.
Written by the indexing model from the issue text.
Description
Found while reviewing https://github.com/hathach/tinyusb/pull/4619 at 9b6be7461; outside that PR's scope. Source
trace by review, not reproduced on hardware.
Problem
hcd_deinit() (src/portable/analog/max3421/hcd_max3421.c:571-589) deletes spi_mutex and handler_mutex and sets
both to NULL. On Espressif the MAX3421 interrupt is serviced from a task: max3421_intr_task()
(hw/bsp/espressif/boards/family.c:230-237) loops on max3421_intr_sem and calls
tuh_int_handler(BOARD_TUH_RHPORT, false). Nothing stops or drains that task before the mutexes are deleted.
Two ways the handler runs after deletion:
- the semaphore was given before
tuh_max3421_int_api(rhport, false)disabled the GPIO interrupt, so the task wakes
afterhcd_deinit()returned; - on a dual-core ESP32 the task runs on the other core while
hcd_deinit()executes. With a device attached, FRAME
IRQs fire every 1 ms, so the window is not narrow.
The handler then calls osal_mutex_lock(NULL, ...), which is xSemaphoreTake(NULL) (src/osal/osal_freertos.h:224-226):
an assert or crash during teardown.
handler_mutex was added by #4619; spi_mutex is deleted the same way on master 1384420308, so the hazard predates
it.
Not affected
- usbh itself makes no HCD transfer call after
hcd_deinit():usbh_task_run()returns when!tuh_inited()
(src/host/usbh.c:869-871). - No in-tree example calls
tuh_deinit()on a MAX3421 build, so nothing in the tree triggers it today.
Severity: low.
Related: #4489 A3 — tuh_deinit() order of hcd_deinit() against the device-tree teardown; that is the usbh side, this
one is the ESP BSP's handler task.
Remaining work
In the ESP BSP, stop and drain max3421_intr_task before hcd_deinit() deletes the mutexes, or have the handler bail
out on a cleared running flag before locking. Validate on an ESP32 dual-core board with MAX3421 attached by calling
tuh_deinit() with a device mounted.
Why deferred
Lifecycle of a BSP task with no in-tree caller of tuh_deinit() on this configuration; it belongs in its own change
with hardware validation, not in #4619's device-removal handling.
- Dominant language
- C
- Stars
- 7.3k
- Forks
- 1.6k
- Avg merge
- 1d 19h
- Merged PRs (30d)
- 67
Getting set up
This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from hathach/tinyusb
-
follow-up Port WCH USBFS Prio 📌
Difficulty 2/5 1-3 hours Newbie friendliness 64/100
Maintainers usually reply within 1 day
-
follow-up Prio 📌
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
Maintainers usually reply within 1 day
-
hil-contract: a background hil_remote.py launch is killed at the Bash default 10 min timeoutPossibly taken @hathach claimed this 16 days ago. Openfollow-up Prio 📌
Difficulty 1/5 Under an hour Newbie friendliness 84/100
Maintainers usually reply within 1 day
-
ch32v30x: debug UART loses queued bytes when its TX ring fillsPossibly taken @hathach claimed this 16 days ago. Openfollow-up Prio 📌
Difficulty 2/5 1-3 hours Newbie friendliness 66/100
Maintainers usually reply within 1 day
-
followup Prio 📌
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
Maintainers usually reply within 1 day
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 69/100
-
Missing slice offset check in jvp_equal fast path corrupts slice comparison and aborts delpathsOpen
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
Maintainers usually reply within 3 days
-
Difficulty 1/5 Under an hour Newbie friendliness 83/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
area9innovation/flow9#1805 ·
Maintainers usually reply within 3 days
-
AI:Robustness triage
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
open5gs/open5gs#5246 · 1 comment ·
Maintainers usually reply within 1 day