diff --git a/py/gc.c b/py/gc.c
index 65bfea0b35a3404312b6cfa565980896fe1fb3d3..7aa5bc326eef8bdcb4563ed67c64128b36472423 100644
--- a/py/gc.c
+++ b/py/gc.c
@@ -172,6 +172,10 @@ void gc_unlock(void) {
     gc_lock_depth--;
 }
 
+bool gc_is_locked(void) {
+    return gc_lock_depth != 0;
+}
+
 #define VERIFY_PTR(ptr) ( \
         (ptr & (BYTES_PER_BLOCK - 1)) == 0          /* must be aligned on a block */ \
         && ptr >= (machine_uint_t)gc_pool_start     /* must be above start of pool */ \
diff --git a/py/gc.h b/py/gc.h
index cea368f480ddf8c55b781e3309b52433263bb880..3b3b7dbb50e81ba05d6604eab22eaa9a081e7764 100644
--- a/py/gc.h
+++ b/py/gc.h
@@ -30,6 +30,7 @@ void gc_init(void *start, void *end);
 // They can be used to prevent the GC from allocating/freeing.
 void gc_lock(void);
 void gc_unlock(void);
+bool gc_is_locked(void);
 
 // A given port must implement gc_collect by using the other collect functions.
 void gc_collect(void);
diff --git a/py/objexcept.c b/py/objexcept.c
index 9f421373bbd702f3324e4b36854d9228c4185af4..b4a831c628c917cb04a6be36c2bf10e31f22c445 100644
--- a/py/objexcept.c
+++ b/py/objexcept.c
@@ -37,6 +37,7 @@
 #include "objtype.h"
 #include "runtime.h"
 #include "runtime0.h"
+#include "gc.h"
 
 typedef struct _mp_obj_exception_t {
     mp_obj_base_t base;
@@ -335,6 +336,10 @@ void mp_obj_exception_clear_traceback(mp_obj_t self_in) {
 }
 
 void mp_obj_exception_add_traceback(mp_obj_t self_in, qstr file, machine_uint_t line, qstr block) {
+    if (gc_is_locked()) {
+        // We can't allocate memory, so don't bother to try
+        return;
+    }
     GET_NATIVE_EXCEPTION(self, self_in);
 
     // for traceback, we are just using the list object for convenience, it's not really a list of Python objects