← index #9713Issue #3636
Duplicate · high · value 3.441
QUERY · ISSUE

Periodic machine timer blocked while waiting socket.connect()

openby flis-mateuszopened 2022-10-22updated 2022-11-07
bug

Hi,

I am using micropython version 1.19.1 on the esp32 platform

The problem is that when connecting to the socket, the timer is blocked.
After the socket connecting is interrupted, the callback function is called several times at a time

Here's a fragment of code where you can see the problem:

import socket
import network
from machine import Timer
import time

sta_if = network.WLAN(network.STA_IF)
if not sta_if.isconnected():
    sta_if.active(True)
    time.sleep_us(200)
    sta_if.connect('xxx', 'xxx')

while not sta_if.isconnected():
    time.sleep(1)

t = Timer(-1)
t.init(period=1000, mode=Timer.PERIODIC, callback=lambda t:print(time.time()))

def connect():
    try:
        print('connecting')
        addr = socket.getaddrinfo('192.168.1.41', 1886)[0][-1]
        s = socket.socket()
        s.connect(addr)
        s.send(bytes('.....\n', 'utf8'))
        s.read()
        s.close()
    except Exception as e:
        print(e)
time.sleep(10)
connect()

Output:

719797401
719797402
719797403
719797404
719797405
719797406
719797407
719797408
719797409
connecting
719797428
[Errno 113] ECONNABORTED
719797428
719797428
719797428
719797428
719797428
719797428
719797428
719797429
719797430
719797431
719797432
719797433
...

As you can see, time.time() should be printed every 1sec, but when you call socket.connect() it stops printing. When the connecting is interrupted, the function is suddenly called several times with the same result

CANDIDATE · ISSUE

periodic timer blocked while waiting socket read()

closedby gdallaireopened 2018-02-24updated 2018-05-02

Hello,

the same code (see below) running on ESP8266 and ESP32 produces different results.

This code creates a periodic timer calling a simple print() at 5 sec intervals, opens a socket, sends some bytes and then waits for a reply.

Under ESP8266, the timer continues to run while the read() is blocking.

Under ESP32, the timer is "paused" while the read() is blocking :(

any idea ?

import socket
import network
from machine import Timer

sta_if = network.WLAN(network.STA_IF)
sta_if.active(True)
sta_if.connect('xxxx', 'xxxxx')

t = Timer(3)
t.init(period=5000, mode=Timer.PERIODIC, callback=lambda t:print("hello"))

def connect():
    addr = socket.getaddrinfo('192.168.0.40', 4242)[0][-1]
    s = socket.socket()
    s.connect(addr)
    s.send(bytes('.....\n', 'utf8'))
    s.read()
    s.close()

OUTPUT:

ESP8266:


hello
hello
>>> connect()
hello
hello

ESP32:


hello
hello
>>> connect()

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