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.
@@ -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);
