Repository navigation
Conversation
stop_mp() unlinks queued callbacks on the heap, then runs finalisers and frees the heap. Until a finaliser stops its hardware, an interrupt can queue a callback that lives in a heap object again, and it would still be pending after the heap is freed. On ESP32-S3, the I2SOut on_sent interrupt did this and caused hard faults at soft reload (adafruit#11822). background_callback_reset() now sets a flag that makes background_callback_add() drop callbacks on the heap until background_callback_heap_ready() is called after the next gc_init(). gc_ptr_on_heap() moves to ITCM, because on i.MX RT the USB interrupt can queue a callback during a flash write. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Guard common-hal/audiobusio/PDMIn.c and the EVSYS handler's call into it with CIRCUITPY_AUDIOBUSIO_PDMIN, as I2SOut.c is guarded with CIRCUITPY_AUDIOBUSIO_I2SOUT. metro_m0_express and sparkfun_redboard_turbo de_DE builds overflowed flash by 28 bytes. Turning off PDMIn leaves about 2.6 KB free. audiobusio.I2SOut is still available. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
mikeysklar
approved these changes
Nov 21, 2026
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.
Diagnosis by @nucino. Fix by Claude, reviewed by @dhalbert.
Problem
On soft reload,
stop_mp()callsbackground_callback_reset(), which unlinks queued callbacks that are on the heap. It then callsgc_deinit(), which runs finalisers, and frees the heap.But hardware owned by a heap object will keep interrupting until its finaliser stops it. On ESP32-S3, the
I2SOuton_sentinterrupt can queue a callback that lives inside theI2SOutobject between those two calls. The callback is still queued after the heap is freed, and running it causes a hard fault.Fix
The fix is to prevent heap-related callbacks from running while the heap is being torn down.
background_callback_reset()now sets a flag. While it is set,background_callback_add()drops any callback that is on the heap or whose data is on the heap.background_callback_heap_ready()clears the flag.start_mp()calls it aftergc_init().gc_ptr_on_heap()is now put in ITCM. On i.MX RT, the USB interrupt can run during a flash write, and it callsbackground_callback_add(), which now callsgc_ptr_on_heap()during heap teardown. So that routine shouldn't be in flash.This fix will work for any driver whose interrupt queues a callback stored in a heap object, not only
I2SOut.Testing
Metro ESP32-S3, running the reproducer from #11822 with 20,000 small bytearrays allocated before
I2SOut. The extra heap objects make theI2SOutfinaliser run later, which widens the window.main, unpatchedA debug build showed that the remaining fault is a separate bug: the
mdns.Serverfinaliser asserts inheap_caps_free()insidemdns_free(). It is not addressed here.