micropython / micropython

MicroPython - a lean and efficient Python implementation for microcontrollers and constrained systems
https://micropython.org
Other
19.44k stars 7.77k forks source link

Maximum recursion depth #16239

Open Vishal-Birajdar opened 1 day ago

Vishal-Birajdar commented 1 day ago

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

import network
import json
from umqtt.robust import MQTTClient
import machine
import ssl
import time
import shared
from ota import OTAUpdater
def mqtt_connect():
    try:
        print("Attempting to connect to MQTT broker...")
        shared.client = MQTTClient(config['device_info']['device_id'],config['mqtt']['broker'],user=config['mqtt'] 
  ['user'],password=config['mqtt']['password'],keepalive=90,ssl=True)
        shared.client.set_callback(x.message_callback)
        shared.client.set_last_will(shared.topic_hb,"-1",False,qos=0)
        shared.client.connect()
        print('Connected to %s MQTT Broker' % config['mqtt']['broker'])
        shared.client.subscribe(shared.topic_cmd+"/D")
        shared.client.subscribe(shared.topic_cmd+"/FD")
        shared.client.subscribe(shared.topic_cmd+"/R1")
        shared.client.subscribe(shared.topic_cmd+"/R2")
        shared.client.subscribe(shared.topic_cmd+"/R3")
        shared.client.subscribe(shared.topic_cmd+"/R4")
        shared.client.subscribe(shared.topic_cmd+"/RR")
        shared.client.subscribe(shared.topic_cmd+"/MR")
        shared.pix[3] = shared.GREEN
        shared.pix.write()
        #return shared.client  
    except Exception as e:
        mqtt_connect()
        time.sleep(0.2)
        print('Failed to connect to MQTT broker. Reconnecting...',e)
        shared.event('\n Failed to connect to MQTT broker. Reconnecting...')
        shared.pix[3] = shared.RED
        shared.pix.write()
        time.sleep(0.3)
        machine.reset()
#p1 = Connection()       
def check_updates():
    try:
        firmware_url = "https://github.com/Vishal-Birajdar/Micropython-/"
        files = ["shared.py", "esecurity.py", "config.json"]
        ota_updater = OTAUpdater(config['wifi']['ssid'],config['wifi']['password'] , firmware_url, files)
        ota_updater.download_and_install_update_if_available()
    except:
        shared.event("Error in updating device \n")
        machine.reset()

key_data = read_certificate('privkey4.pem')
cert_data = read_certificate('cert4.pem')
ca_data = read_certificate('chain4.pem')
ssl_params = {
    "certfile": cert_data,
    "keyfile": key_data,
    "cadata": ca_data,
    "server_hostname": config['mqtt']['broker'],
    "cert_reqs": ssl.CERT_REQUIRED
}
#check_updates()
#p1.mqtt_connect()
try:
    wifi_connect()
    mqtt_connect()
except Exception as e:
    time.sleep(0.3)
    mqtt_connect()
    print('Failed to connect to MQTT broker. Reconnecting...',e)
    shared.event('\n Failed to connect to MQTT broker. Reconnecting...')
    machine.reset()

while True:
    try:
        shared.client.publish(shared.topic_hb,'1')
        currentMillis = time.ticks_ms()
        for i, s_pin in enumerate(sensor_pin):
            shared.buffer["S"+str(i+1)] = str(s_pin.value())+str(i+1)
            if s_pin.value()==1:shared.active_sensor[i]=1
            else:shared.active_sensor[i]=0
        shared.act_sensor_cnt=shared.active_sensor.count(1)
        print("active sensor",shared.act_sensor_cnt)
        print("sensor buffer",shared.active_sensor)
        time.sleep(0.1)
        x.main()
        if time.ticks_diff(currentMillis,previousMillis)>=interval:
            previousMillis = currentMillis
            if not wlan.isconnected():
                shared.pix[1] = shared.YELLOW
                shared.pix.write()
                wifi_connect()
                #time.sleep(.4)
    except Exception as e:
        print("Error with ",e)
        shared.event("\n Error in the main flie in loop")
        machine.reset()

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 1) maximum recursion depth 2) mqtt connection lost automatically

Code of Conduct

Yes, I agree

sosi-deadeye commented 1 day ago

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. https://docs.github.com/en/get-started/writing-on-github/getting-started-with-writing-and-formatting-on-github/basic-writing-and-formatting-syntax#quoting-code

You can also edit your original post.

Vishal-Birajdar commented 1 day ago

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. https://docs.github.com/en/get-started/writing-on-github/getting-started-with-writing-and-formatting-on-github/basic-writing-and-formatting-syntax#quoting-code

You can also edit your original post.

I posted issue first time on gitbub . sorry for mistakes

sosi-deadeye commented 1 day ago

Just put your code in Code Tags, that other users could help.

projectgus commented 23 hours ago

@Vishal-Birajdar Thanks for the clear description of the problem. I've changed the """ in your post to ```py so the code highlights correctly.

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 ota, shared, and x modules are missing, for example.

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.

projectgus commented 23 hours ago

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 mqtt_connect() calls mqtt_connect(), so each time the connection fails you are pushing a frame on the stack. If the network goes down then it could easily call into itself many times. Try restructuring the code with a while loop around the outside of the try/except instead of using recursion.

Josverl commented 22 hours ago

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)
Vishal-Birajdar commented 19 hours ago

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 Is this error hardware related?, because I have soldered the controller on my I/O board.