From 4bbba3060d451d4dfa147ebc793acd1cc7364e5a Mon Sep 17 00:00:00 2001 From: Damien George Date: Wed, 27 May 2020 17:53:31 +1000 Subject: [PATCH] lib/utils: Lock the scheduler when executing hard callback functions. Otherwise scheduled functions may execute during the hard callback and then fail if they try to allocate heap memory. --- lib/utils/mpirq.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/lib/utils/mpirq.c b/lib/utils/mpirq.c index d04fab68bb..8de13b0b6a 100644 --- a/lib/utils/mpirq.c +++ b/lib/utils/mpirq.c @@ -62,8 +62,10 @@ mp_irq_obj_t *mp_irq_new(const mp_irq_methods_t *methods, mp_obj_t parent) { void mp_irq_handler(mp_irq_obj_t *self) { if (self->handler != mp_const_none) { if (self->ishard) { - // When executing code within a handler we must lock the GC to prevent - // any memory allocations. + // When executing code within a handler we must lock the scheduler to + // prevent any scheduled callbacks from running, and lock the GC to + // prevent any memory allocations. + mp_sched_lock(); gc_lock(); nlr_buf_t nlr; if (nlr_push(&nlr) == 0) { @@ -77,6 +79,7 @@ void mp_irq_handler(mp_irq_obj_t *self) { mp_obj_print_exception(&mp_plat_print, MP_OBJ_FROM_PTR(nlr.ret_val)); } gc_unlock(); + mp_sched_unlock(); } else { // Schedule call to user function mp_sched_schedule(self->handler, self->parent);