Skip to content

Commit a18a392

Browse files
committed
background_callback: Add gc collect callback
A background callback must never outlive its related object. By collecting the head of the linked list of background tasks, this will not happen. One hypothetical case where this could happen is if an MP3Decoder is deleted while its callback to fill its buffer is scheduled.
1 parent 81105cb commit a18a392

File tree

3 files changed

+13
-0
lines changed

3 files changed

+13
-0
lines changed

main.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -493,6 +493,8 @@ void gc_collect(void) {
493493
// have lost their references in the VM even though they are mounted.
494494
gc_collect_root((void**)&MP_STATE_VM(vfs_mount_table), sizeof(mp_vfs_mount_t) / sizeof(mp_uint_t));
495495

496+
background_callback_gc_collect();
497+
496498
#if CIRCUITPY_DISPLAYIO
497499
displayio_gc_collect();
498500
#endif

supervisor/background_callback.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,4 +79,9 @@ void background_callback_reset(void);
7979
void background_callback_begin_critical_section(void);
8080
void background_callback_end_critical_section(void);
8181

82+
/*
83+
* Background callbacks may stop objects from being collected
84+
*/
85+
void background_callback_gc_collect(void);
86+
8287
#endif

supervisor/shared/background_callback.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
* THE SOFTWARE.
2525
*/
2626

27+
#include "py/gc.h"
2728
#include "py/mpconfig.h"
2829
#include "supervisor/background_callback.h"
2930
#include "supervisor/shared/tick.h"
@@ -105,3 +106,8 @@ void background_callback_reset() {
105106
in_background_callback = false;
106107
CALLBACK_CRITICAL_END;
107108
}
109+
110+
void background_callback_gc_collect(void) {
111+
background_callback_t *cb = (background_callback_t*)callback_head;
112+
gc_collect_ptr(cb);
113+
}

0 commit comments

Comments
 (0)