RP2: global variable lost at maximum recursion depth in second thread
A global variable reportedly is not there (NameError: name 'a' isn't defined) although it should be there.
This occurs in a setting of a recursion test function that hits the limit of 19 recursions on the second thread/core on a RPi2040.
The global variable, a, is updated in this function.
Strangely this only occurs if also two other global variables are defined, sleep_ms is imported as a single import and the maximum recursion depth exception is catched with try/except.
The code is as follows:
import _thread
from time import sleep_ms #, sleep_us # if more than one functions imported there is no NameError
a = 0
b = 0
def recursionTest():
global a
a += 1
try:
recursionTest()
except Exception as errorMsg:
print(errorMsg)
_thread.start_new_thread(recursionTest,())
c = 0
sleep_ms(10)
print('recursion depth:', a)
The output is as follows:
Unhandled exception in thread started by <function recursionTest at 0x20008d50>
Traceback (most recent call last):
File "<stdin>", line 9, in recursionTest
NameError: name 'a' isn't defined
recursion depth: 0
If c = 0, for example is commented out the output changes to (correct);
maximum recursion depth exceeded
recursion depth: 19
This was on `MicroPython v1.19.1 on 2022-07-27; Raspberry Pi Pico with RP2040
Low recursion limit on Windows port with MSVC
I ran into a Maximum recursion depth exceeded error while developing on micropython on Windows. I was somewhat surprised as I did not think that my code was excessively complex.
I used the code from this forum post to test the recursion depth:
MicroPython v1.8.7 on 2017-03-02; win32 version
Use Ctrl-D to exit, Ctrl-E for paste mode
>>>
paste mode; Ctrl-C to cancel, Ctrl-D to finish
=== a = 0
=== fail = False
=== def foo():
=== global a, fail
=== a += 1
=== if not fail:
=== try:
=== foo()
=== except:
=== fail = True
===
=== foo()
=== print(a)
12
>>>
It seems the recursion depth is 12 on Windows? Can that be right? How is it controlled? The same forum post mentions that on the Unix build, the recursion depth is 166. Even on the esp8266 it seems to be 19.