socket.recv() causing hardware WDT rst cause 4 crash?

All ESP8266 boards running MicroPython.
Official boards are the Adafruit Huzzah and Feather boards.
Target audience: MicroPython users with an ESP8266 board.
Post Reply
HRD
Posts: 4
Joined: Sat Dec 24, 2016 1:52 am

socket.recv() causing hardware WDT rst cause 4 crash?

Post by HRD » Sat Apr 24, 2021 3:39 am

I have be playing with a simple web server and reading/writing to HTTP pages but have been plagued by a mysterious hardware WDT 'rst cause 4' crash across previous MPy builds. I finally nailed it down to an issue with socket.recv() statement and the simplified program below which reproduces the problem (for me anyway). My code is far from perfect, but the example is pretty much right out of the documentation (less all the added debugging statements). It eventually hangs after step 'F' and resets with a 'rst cause 4' (although sometimes a 'rst cause 2') after a number of cycles (varies, usually <200 cycles for the website listed in the code). The number of bytes returned from the website is constant, but the number of cycles does appear to vary if the website is changed. I put in garbage collection and display free memory, but it doesn't appear to be memory related (25696-30976 free before crashing). Same thing on different modules (of the same type).

Thoughts or suggestions?

Thanks and keep up the great work.

Board: ESP8266 Node MCU LoLin 1.0
Micropython Version 1.14
Connected via USB/Serial

Code: Select all

# http_get() from https://docs.micropython.org/en/latest/esp8266/tutorial/network_tcp.html
import time
from machine import Pin
import gc

def http_get(url):
    byte_count = 0
    import socket
    _, _, host, path = url.split('/', 3)
    print(' A ',end='') # Make it here?
    addr = socket.getaddrinfo(host, 80)[0][-1]
    print(' B ',end='') # Make it here?
    s = socket.socket()
    print(' C ',end='') # Make it here?
    s.connect(addr)
    print(' D ',end='') # Make it here?
    s.send(bytes('GET /%s HTTP/1.0\r\nHost: %s\r\n\r\n' % (path, host), 'utf8'))
    print(' E ',end='') # Make it here?
    while True:
        print(' (',gc.mem_free(),') ',end='') # free mem
        print(' F ',end='') # Make it here?
        data = s.recv(100)  # Crashes here eventually <<<<<<<<<
        print(' G ',end='') # Make it here? (Not if it crashes!)
        if data:
            byte_count = byte_count + len(str(data, 'utf8')) # count bytes read
            print(' H ',end='') # Make it here?
        else:
            break
    print(' I ',end='') # Make it here?
    s.close()
    print(' J ',end='') # Make it here?

    print(' Bytes:',byte_count)

    gc.collect() # do a garbage collection to clean thing up just for kicks...


# Set up LED activity indicator
led=Pin(2,Pin.OUT) # This will turn the on-board LED on as it is active LOW
led_stat = 0
led.value(0)

exit_button = Pin(0, Pin.IN) ## GPIO0 attached to 'Flash' button

counter = 0 # Count cycles

while exit_button.value() != 0 :  #Until Crash or 'Flash' button is pressed and held

    counter += 1 # count cycles
    print(counter,'> ',end = '')

    try:
        http_get('http://www.www.com/')  # website that doesn't return much, but is real

    except Exception as e:
        print('Error ',e)
        pass

    # Flash LED to show still alive
    led_stat = led_stat ^ 1
    led.value(led_stat)

    time.sleep(5) # wait 5 seconds before trying again

Post Reply