From a18a39210919a886d4efb4c52b85a309b3403abf Mon Sep 17 00:00:00 2001 From: Jeff Epler Date: Fri, 17 Jul 2020 08:36:26 -0500 Subject: [PATCH] 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. --- main.c | 2 ++ supervisor/background_callback.h | 5 +++++ supervisor/shared/background_callback.c | 6 ++++++ 3 files changed, 13 insertions(+) diff --git a/main.c b/main.c index ce95de95f0..f928d0d62f 100755 --- a/main.c +++ b/main.c @@ -493,6 +493,8 @@ void gc_collect(void) { // have lost their references in the VM even though they are mounted. gc_collect_root((void**)&MP_STATE_VM(vfs_mount_table), sizeof(mp_vfs_mount_t) / sizeof(mp_uint_t)); + background_callback_gc_collect(); + #if CIRCUITPY_DISPLAYIO displayio_gc_collect(); #endif diff --git a/supervisor/background_callback.h b/supervisor/background_callback.h index 82025f6b7a..535dd656be 100644 --- a/supervisor/background_callback.h +++ b/supervisor/background_callback.h @@ -79,4 +79,9 @@ void background_callback_reset(void); void background_callback_begin_critical_section(void); void background_callback_end_critical_section(void); +/* + * Background callbacks may stop objects from being collected + */ +void background_callback_gc_collect(void); + #endif diff --git a/supervisor/shared/background_callback.c b/supervisor/shared/background_callback.c index e45c9b5c3d..1be3cae2ba 100644 --- a/supervisor/shared/background_callback.c +++ b/supervisor/shared/background_callback.c @@ -24,6 +24,7 @@ * THE SOFTWARE. */ +#include "py/gc.h" #include "py/mpconfig.h" #include "supervisor/background_callback.h" #include "supervisor/shared/tick.h" @@ -105,3 +106,8 @@ void background_callback_reset() { in_background_callback = false; CALLBACK_CRITICAL_END; } + +void background_callback_gc_collect(void) { + background_callback_t *cb = (background_callback_t*)callback_head; + gc_collect_ptr(cb); +}