Maximum recursion depth #16269
Replies: 10 comments 2 replies
-
Do you ever read your own posts after you've sent them? You are not the first or the last person to format code incorrectly. The code must be enclosed with code tags. You can also edit your original post. |
Beta Was this translation helpful? Give feedback.
-
I posted issue first time on gitbub . sorry for mistakes |
Beta Was this translation helpful? Give feedback.
-
Just put your code in Code Tags, that other users could help. |
Beta Was this translation helpful? Give feedback.
-
@Vishal-Birajdar Thanks for the clear description of the problem. I've changed the It's not possible to investigate a bug report without the code to reproduce it, and the code posted above is not complete or runnable - the My best guess is there are two likely possibilities for what is happening here:
If that's not enough to solve the problem, suggest you remove or disable code in your program and re-run until you find a version which doesn't crash. If you do this carefully, you may find the root cause of the problem yourself. If you don't succeed to find the root cause, you may end up with a minimal working example that we can use to diagnose the bug. |
Beta Was this translation helpful? Give feedback.
-
Oh, I looked again and there is actually recursion here: def mqtt_connect():
try:
print("Attempting to connect to MQTT broker...")
# (removed some lines)
except Exception as e:
mqtt_connect()
# (removed more lines) The exception handler for |
Beta Was this translation helpful? Give feedback.
-
That suggested structure would look something like def mqtt_connect():
while True:
try:
print("Attempting to connect to MQTT broker...")
# (removed some lines)
break # Exit the loop if the connection is successful
except Exception as e:
print(f"Connection failed with error: {e}. Retrying...")
# wait some time
# (removed more lines) |
Beta Was this translation helpful? Give feedback.
-
this is my another file where i declare callback function. class call_loop:
def __init__(self):
pass
def callback(self,topic_cmd,msg):
decoded_topic = topic_cmd.decode('utf-8').strip()
decoded_msg = msg.decode('utf-8').strip()
#print("decoded_topic:"+decoded_topic)
#print("decoded_msg:"+decoded_msg)
if decoded_topic.endswith("FD"):
print("decoded_topic:"+decoded_topic)
if decoded_msg == "1105":
shared.buffer["FD"] = "1105"
shared.FG_Arm = True
print("shared.FG_Arm ",shared.FG_Arm)
shared.string_json = json.dumps(shared.buffer)
elif decoded_msg == "0105":
shared.buffer["FD"] == "0105"
shared.FG_Arm = False
print("shared.FG_Arm ",shared.FG_Arm)
shared.string_json = json.dumps(shared.buffer)
else:
print("Invalid Input !")
shared.string_json = json.dumps(shared.buffer)
shared.client.publish(shared.topic_json,shared.string_json) I am remove the try and exception but same issue again |
Beta Was this translation helpful? Give feedback.
-
What your
This is called recursion. In this special case, a recursion without limit. The only limit are the resources on the Microcontroller/SoC/Computer. Stack based languages requires memory for the stack of a function. After the function is left, the memory is freed. But if the function calls another function, the memory is still in use. On a Microcontroller, you can hit the recursion limit very fast because they do not have much RAM. If you remove the import machine
import time
import sys
from umqtt.robust import MQTTClient
import shared
# the next two functions should be implemented in shared
def led_set_green():
"""
Illuminate green
"""
shared.pix[3] = shared.GREEN
shared.pix.write()
def led_set_red():
"""
Illuminate red
"""
shared.pix[3] = shared.RED
shared.pix.write()
def mqtt_connect():
# from where does config come?!
shared.client = MQTTClient(
config["device_info"]["device_id"],
config["mqtt"]["broker"],
user=config["mqtt"]["user"],
password=config["mqtt"]["password"],
keepalive=90,
ssl=True,
)
# x does not exist
# shared.client.set_callback(x.message_callback)
shared.client.set_last_will(shared.topic_hb, "-1", False, qos=0)
try:
print("Attempting to connect to MQTT broker...")
shared.client.connect()
print(f'Connected to {config["mqtt"]["broker"]} MQTT Broker')
for topic in ("D", "FD", "R1", "R2", "R4", "RR", "MR"):
shared.client.subscribe(f"{shared.topic_cmd}/{topic}")
# use simple functions to show the state
led_set_green()
except Exception as e:
time.sleep(0.2)
print("Failed to connect to MQTT broker. Reconnecting...")
sys.print_exception(e) # print the original exception
shared.event("\n Failed to connect to MQTT broker. Reconnecting...")
# use simple functions to show the state
led_set_red()
time.sleep(0.3)
machine.reset() |
Beta Was this translation helpful? Give feedback.
-
Beta Was this translation helpful? Give feedback.
-
Converted this to a discussion as there's not an actionable bug report here. |
Beta Was this translation helpful? Give feedback.
-
Port, board and/or hardware
Pi Pico w
MicroPython version
MicroPython v1.22.2 on 2024-02-22; Raspberry Pi Pico W with RP2040
Expected behaviour
No response
maximum recursion depth exceeded occurdue to mqtt connection problem and reconnection problem. after hours code stuck automatically and mqtt not reconnected
I am not using any recursion or thread in my code but running a code after some time occuremaximum recursion depth error .
Additional Information
I am working on Iot project there is three .py file one is for main logic and other is varibale declared which used in all three file. and third file is main.py here i impleted wifi,mqtt and in loop I check the 4 gpio input (for any detection) and then call the main logic function which is define in another file,and checking the wifi connection status continously.
after running a code 10-15 min working ok then i got two error
Code of Conduct
Yes, I agree
Beta Was this translation helpful? Give feedback.
All reactions