modwlan.c: Setting ip with ifconfig() after connect() leads to core dump and device locks #99
Description
This simple scripts works for a freshly set-up device only once. If then a soft-reset is done with Ctrl-D, it does not connect any more, and also may cause a core dump. Erasing and reloading flash helps to reestablish WiFi. When the device is powered off, this lock state is released after a while too, indicating some timeouts which cure the lock. The issue occurred with version 1.10.1.b1. Previous versions worked fine.
import network
import time
# setup as a station
wlan = network.WLAN(mode=network.WLAN.STA)
wlan.connect(ssid="my_AP", auth=(network.WLAN.WPA2, "my_key"))
for _ in range (200):
if wlan.isconnected():
wlan.ifconfig(config=("10.0.0.141", "255.255.255.0", "10.0.0.240", "10.0.0.240"))
print(wlan.ifconfig())
break
else:
time.sleep_ms(50)
else:
print("Could not connect")
Placing the ifconfig() call before connect() or adding a delay of a second between the connect() and ifconfig call aviods the issue.
Trying to understand what's going on by looking into modwlan.c makes me wonder, what actually happens. There seems to be a condition mix-up between the local state of the wlan_obj.disconnected variable and the connection status in the wifi_event_group variable. ifconfig(), when called, disconnects first, which causes a SYSTEM_EVENT_STA_DISCONNECTED event, which attempts to reconnect, while the dhcp client is shut down and the new ip values are set, and the timing relations are not obvious. This might lead to trouble seen.
Crash log:
PYB: soft reboot
Guru Meditation Error of type LoadProhibited occurred on core 0. Exception was unhandled.
Register dump:
PC : 0x4010df00 PS : 0x00060e30 A0 : 0x80119e6e A1 : 0x3ffbd590
A2 : 0x3ffc8ca8 A3 : 0x3ffd5cb9 A4 : 0x3ffd5ce3 A5 : 0x00000000
A6 : 0x00000000 A7 : 0x00000000 A8 : 0x8010df00 A9 : 0x3ffbd580
A10 : 0x3ffc8cf7 A11 : 0x00000000 A12 : 0x0000000f A13 : 0x3ffc8d06
A14 : 0x02ac0f00 A15 : 0x00000000 SAR : 0x00000010 EXCCAUSE: 0x0000001c
EXCVADDR: 0x000000f1 LBEG : 0x400976c4 LEND : 0x400976cf LCOUNT : 0xffffffff
Backtrace: 0x4010df00:0x3ffbd590 0x40119e6b:0x3ffbd5c0 0x401169e1:0x3ffbd610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