Page 1 of 1

Feeding watchdog from a thread

Posted: Sun May 22, 2022 8:44 pm
by jedo-tm
When I feed the watchdog from thread that is not __main__ it has no effect and the board (ESP32 / micropython 1.18) reset.

I the example below I created a class "service" which executed a function at a certain interval. In the example below the wathcdog feed function is caled every second, however the board resets after 10 sec ( the watchdogs timeout )

Must the watchdog be feed from the main thread?

Code: Select all

import machine
import time
import _thread

class service:
    def __init__(self,function,delay=10):
        self.function = function
        self.delay=delay
        self.shutdown=False
        
    def main_loop(self):
        while not self.shutdown:
            self.function()
            for i in range(self.delay*10):
                time.sleep_ms(100)
    def start(self):
        self.shutdown=False
        try:
            print("Starting service. Function '%s' executed every %i sec" % (self.function.__name__,self.delay))
        except:
            print("Starting service. Function '%s' executed every %i sec" % ("bound method",self.delay))
        self.thread=_thread.start_new_thread(self.main_loop,())
    
    def stop(self):
        self.shutdown=True
        
wd_timeout = 10000
wd = machine.WDT(id=0, timeout=wd_timeout)
service_wd=service(function=wd.feed,delay=1)
service_wd.start()


Re: Feeding watchdog from a thread

Posted: Mon May 23, 2022 8:26 am
by pythoncoder
The purpose of a WDT is to reset the board if the program has stopped running. Feeding the WDT in a separate thread defeats this aim: the main thread may have hung while the WDT thread continues to execute.

wd.feed() commands should be distributed around your main program in such a way that, if the program halts for any reason, the WDT will time out.

Re: Feeding watchdog from a thread

Posted: Mon May 23, 2022 11:17 am
by karfas
Depending upon the way the micropython threads are implemented (I have no idea) it might be required to create the watchdog instance per thread.
The underlying esp_task_wdt_add() gets NULL for the "task" to monitor, meaning the current task. In the case a thread is a "task" on its own (?) this will obviously not work. See ports/esp32/machine_WDT.c for the micropython implementation and https://docs.espressif.com/projects/esp ... /wdts.html for the ESP-IDF calls.

Re: Feeding watchdog from a thread

Posted: Tue May 24, 2022 12:08 pm
by pythoncoder
My comment was to caution against adding a thread (to a single-threaded program) whose only purpose was to feed the dog. Using a WDT in a multi-threaded program provides difficulties of its own. How do you detect if one thread has stopped running? Not an insoluble problem, but one which would demand careful analysis.