I'm currently using an ESP devkit C v4 board to host a minimal website, locally accessible over mDNS. I've been using uasyncio v3 to handle multitasking and scheduling, and so far it performs great. However, on occasion an OSError EADDRINUSE is thrown, which I understand will happen when attempting to open a socket on an address already bound to another socket. My question pertains to handling this specific exception in the context of a uasyncio task.
The usual approach to managing the EADDRINUSE error is to use setsocketopt() to set SO_REUSEADDR. However, uasyncio handles the socket creation in the background, and I was unable to find any way to access the socket object. With that off the table, my admittedly crude solution was to use a GPIO pin to trigger a monostable that hard resets the board. The issue here is that I seem to be unable to trap the specific exception no matter what I try. The uasyncio v3 tutorial seems to suggest that exceptions be handled in the following way:
Code: Select all
import uerrno as errno
...
try:
asyncio.run(server.run())
except OSError as oserr:
if oserr.args[0] == errno.EADDRINUSE:
p13.on() # Previously instantiated machine.Pin object controlling reset
except KeyboardInterrupt:
print('Interrupted') # This mechanism doesn't work on Unix build.
finally:
server.close()
_ = asyncio.new_event_loop()
Cheers,
-Erik