diff --git a/docs/library/micropython.rst b/docs/library/micropython.rst
index a6ea738ed5f2148417b05249cb6f679f3f8fc425..d9f913bffdad51de0d8341ddb97d503f839edb06 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 5d72419d1a5599e87b0c2a5615255644832d5ce0..d8dc60bfe5967656c5fe227349668cc5a80e8d11 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;