usb-midi-device: Fix handling of incoming MIDI messages under load. - #1163
Open
projectgus wants to merge 2 commits into
Open
projectgus wants to merge 2 commits into
projectgus wants to merge 2 commits into
Conversation
Prevents situation where a high MIDI RX load fills up the schedule() queue with redundant calls to _on_rx() and causes a RuntimeError. Also refactor the callback handler so that a RuntimeError when scheduling doesn't stop RX endpoint transfers from continuing. This work was funded through GitHub Sponsors. Signed-off-by: Angus Gratton <angus@redyak.com.au>
If the _rx buffer is full then no OUT transfer is submitted to TinyUSB. This meant that completely filling the RX buffer could permanently stall the OUT direction endpoint, as the scheduled callback handler wouldn't re-queue it. Signed-off-by: Angus Gratton <angus@redyak.com.au>
This was referenced Sep 16, 2026
dpgeorge
approved these changes
Sep 16, 2026
dpgeorge
left a comment
Member
There was a problem hiding this comment.
Thanks, this looks like a good fix.
Maybe (separately) it's worth improving micropython.schedule() so you can ask it to only queue the given function at most once? Or even better, preallocate the queue slot from Python so it's guaranteed to always be able to schedule.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #1158 (RuntimeError raised in the USB transfer callback under high load of incoming MIDI messages), in three parts:
micropython.schedule()queue by not scheduling redundant callbacks.micropython.schedule()does fail due to a full queue then the ongoing USB OUT endpoint transfer is still re-queued, to avoid stalling the device permanently.Testing
Note there is still an issue when USB-MIDI data is sent at max line rate to ESP32-S3. That issue is tracked in #1162.
Generative AI
I did not use generative AI tools when creating this PR.