← index #16008Issue #16554
Related · high · value 3.261
QUERY · ISSUE

Using ntptime.settime() while _thread is running can break sleep timing

openby GM-Script-Writer-62850opened 2024-10-14updated 2026-03-24
bugport-rp2

Port, board and/or hardware

Raspberry Pi PICO W

MicroPython version

MicroPython v1.23.0 on 2024-06-02; Raspberry Pi Pico W with RP2040

Reproduction

  • _thread_work() - measure how long time.sleep takes
  • wifi() - connect to wifi and reconnect if it does down
    • wifi_auth just has the wifi login variables
  • setTime() - this sets the clock using ntptime
    • this was unreliable in micropython 1.2, hence the try blocks
  • wait4clock() - just some busy work for asyncio, can be used to start process that depend on the clock being set
import _thread
import ntptime
import asyncio
import time

sleep=asyncio.sleep_ms
#ntptime.host="10.0.0.69"
wlan = 0

def _thread_work():
	while 1:
		then=time.ticks_ms()
		time.sleep(1)
		then=time.ticks_diff(time.ticks_ms(),then)
		print("Core 1:",then)

async def wifi():
	from wifi_auth import ssid, password
	import network

	global wlan
	wlan = network.WLAN(network.STA_IF)
	wlan.active(True)
	wlan.config(pm = 0xa11140)# Power management is very very bad, ping time is around 1000x worse and packet loss insane
	wlan.connect(ssid, password)

	while 1:
		while 1:
			wstat=wlan.status()
			if wstat < 0 or wstat >= 3:
				break
			print('Waiting for WiFi connection...')
			await sleep(1000)

		if wlan.status() == 3:
			status = wlan.ifconfig()
			print('Wifi Connected; ip =',status[0])
			while wlan.isconnected():
				await sleep(30000)
			print('WiFi Down')
		else:
			print("Failed to connect to wifi; retry in 30 seconds")
			await sleep(30000)
		wlan.connect(ssid, password)
#END wifi()

async def setTime():
	#ntptime.settime() failure: [Errno 110] ETIMEDOUT
	#ntptime.settime() failure: overflow converting long int to machine word
	print('Attempt to set time')
	success=0
	try:
		print("calling ntptime.settime()")
		ntptime.settime()
		success=1
	except OverflowError:
		# it is not going to work
		print("overflow error; settime is borked")
	except OSError:
		print("ntptime.settime() failure")
	if not success:
		await sleep(10000)
		asyncio.create_task(setTime())
	else:
		print("END of setTime() process")
#END setTime()

async def wait4clock():
	delay=2000
	while time.time() < 1640995200: # Wait for clock to set or a 1 year...)
		print("Waiting for clock...")
		await sleep(3000)
	print('Clock set:',time.time())
	#_thread.start_new_thread(_thread_work,())
#END wait4clock()

async def main():
	asyncio.create_task(wait4clock())
	asyncio.create_task(wifi())
	while not wlan or not wlan.isconnected():
		then=time.ticks_ms()
		await sleep(1000)
		then=time.ticks_diff(time.ticks_ms(),then)
		print("Core 0:",then,"Waiting for wifi")
	asyncio.create_task(setTime())
	while 1:
		then=time.ticks_ms()
		await sleep(1000)
		then=time.ticks_diff(time.ticks_ms(),then)
		print("Core 0:",then)

_thread.start_new_thread(_thread_work,())

asyncio.run(main())

Expected behaviour

Calls to time.sleep(1) and asyncio.sleep_ms(1000) should be near 1 second long

Observed behaviour

output of example code:

Waiting for clock...
Waiting for WiFi connection...
Core 1: 1000
Core 0: 1000 Waiting for wifi
Waiting for WiFi connection...
Core 1: 1000
Core 0: 1001 Waiting for wifi
Waiting for WiFi connection...
Core 1: 1000
Waiting for clock...
Core 0: 1003 Waiting for wifi
Wifi Connected; ip = 10.0.0.190
Core 1: 1000
Core 0: 1001 Waiting for wifi
Attempt to set time
calling ntptime.settime()
END of setTime() process
Core 1: 22526
Core 0: 22522
Clock set: 1728875328
Core 1: 5324
Core 0: 5324
Core 1: 7473
Core 0: 7474
Core 1: 17208
Core 0: 17208
Core 1: 5319
Core 0: 5319
Core 1: 24678
Core 0: 24678
Core 1: 5325
Core 0: 5325
Core 1: 24678
Core 0: 24678
Core 1: 5330
Core 0: 5330
Core 1: 24673
Core 0: 24673
Core 1: 5325
Core 0: 5325
Core 1: 24684
Core 0: 24684
Core 1: 5319
Core 0: 5319
Core 1: 24682
Core 0: 24683
Core 1: 5324
Core 0: 5324
Core 1: 1939
Core 0: 1939
Core 1: 5734
Core 0: 5734
Core 1: 5836
Core 0: 5836
Core 1: 11162
Core 0: 11162
Core 1: 5329
Core 0: 5330
Core 1: 7059
Core 0: 7059
Core 1: 13925
Core 0: 13926
Core 1: 3687
Core 0: 3687
Core 1: 5325
Core 0: 5325
Core 1: 7269
Core 0: 7269
Core 1: 17413
Core 0: 17413
Core 1: 5325
Core 0: 5325
Core 1: 1018
Core 0: 1018
Core 1: 23655
Core 0: 23655
Core 1: 5324
Core 0: 5324

Additional Information

  • Note this does not have a 100% reproduction rate, feels near 50%
  • This is a regression since micropython 1.2 (rp2-pico-w-20230426-v1.20.0.u2f)
  • Note that Thonny is set to NOT synchronize device's real time clock (Tools -> Options-> Interpreter)

Code of Conduct

Yes, I agree

CANDIDATE · ISSUE

Multithread breaks file operations on pico2w.

openby tudorapoopened 2025-01-08updated 2025-03-03
bugport-rp2

Port, board and/or hardware

RPI_PICO2_W

MicroPython version

MicroPython v1.25.0-preview.180.g495ce91ca on 2025-01-06; Raspberry Pi Pico 2 W with RP2350

Reproduction

  1. copy the below threads using code and run it:
import time
import _thread

global killme

def threadtest():
    global killme
    print("Other starts")
    while True:
        if killme:
            break
        print("másik thread")
        time.sleep(3.17)
    print("Other ends")

killme = False

try:
    new_thread = _thread.start_new_thread(threadtest, ())
    print("Main starts")
    while True:
        print("Main thread")
        time.sleep(1)
except KeyboardInterrupt as theInterrupt:
    print("Main ends")
    killme = True
  1. run it
  2. stop it
  3. try to copy or delete any file with mpremote, it will hang
  4. powercycle (pull out the usb conenctor and back) and things will work until I run the above code again.

Expected behaviour

Expected to be able to upload code.

Observed behaviour

Was not able to upload code.

Additional Information

When I copied/ran non-threaded code, a simple hello world the problem did not happen.

Code of Conduct

Yes, I agree

Keyboard

j / / n
next pair
k / / p
previous pair
1 / / h
show query pane
2 / / l
show candidate pane
c
copy suggested comment
r
toggle reasoning
g i
go to index
?
show this help
esc
close overlays

press ? or esc to close

copied