From f5d46a88aaea61f6c0248c0ac2c5583e7011d634 Mon Sep 17 00:00:00 2001
From: Damien George <damien.p.george@gmail.com>
Date: Sat, 13 Oct 2018 16:21:08 +1100
Subject: [PATCH] lib/utils/pyexec: Forcefully unlock the heap if locked and
 REPL active.

Otherwise there is really nothing that can be done, it can't be unlocked by
the user because there is no way to allocate memory to execute the unlock.

See issue #4205 and #4209.
---
 docs/library/micropython.rst | 3 +++
 lib/utils/pyexec.c           | 6 ++++++
 2 files changed, 9 insertions(+)

diff --git a/docs/library/micropython.rst b/docs/library/micropython.rst
index a6ea738ed..d9f913bff 100644
--- a/docs/library/micropython.rst
+++ b/docs/library/micropython.rst
@@ -90,6 +90,9 @@ Functions
    in a row and the lock-depth will increase, and then `heap_unlock()` must be
    called the same number of times to make the heap available again.
 
+   If the REPL becomes active with the heap locked then it will be forcefully
+   unlocked.
+
 .. function:: kbd_intr(chr)
 
    Set the character that will raise a `KeyboardInterrupt` exception.  By
diff --git a/lib/utils/pyexec.c b/lib/utils/pyexec.c
index 5d72419d1..d8dc60bfe 100644
--- a/lib/utils/pyexec.c
+++ b/lib/utils/pyexec.c
@@ -419,6 +419,12 @@ friendly_repl_reset:
         }
         #endif
 
+        // If the GC is locked at this point there is no way out except a reset,
+        // so force the GC to be unlocked to help the user debug what went wrong.
+        if (MP_STATE_MEM(gc_lock_depth) != 0) {
+            MP_STATE_MEM(gc_lock_depth) = 0;
+        }
+
         vstr_reset(&line);
         int ret = readline(&line, ">>> ");
         mp_parse_input_kind_t parse_input_kind = MP_PARSE_SINGLE_INPUT;
-- 
GitLab