← index #7473Issue #5759
Related · high · value 1.396
QUERY · ISSUE

esp8266 nonblocking ussl: `read(N)` never returns any data

openby vshymanskyyopened 2021-06-29updated 2021-06-30
port-esp8266

MicroPython version: esp8266-20210618-v1.16, esp8266-20210629-unstable-v1.16-39-g4ada56d4c

s = usocket.socket()
s.connect(usocket.getaddrinfo(server, port)[0][-1])
conn = ussl.wrap_socket(s, server_hostname=server)
s.settimeout(0)           # or s.setblocking(False)
conn.write(b"some data")  # OK: server gets the data and replies
while True:
    data = conn.read(1) # increasing count doesn't help
    print('>', data)

Outputs:

> None
> None
> None
> None
> None
> None
> None
> None
> None
> None
> None
> None
> b''
> b''
> b''
> b''
> b''
...

No data is received.

Observations:

  • If I set conn = s (i.e. use plain insecure TCP), it works
  • If I replace s.settimeout(0) with s.settimeout(1), it works for for some 5-10 seconds, then conn.read stops blocking for 1 second, returns quickly and no more data is received
  • If I replace s.settimeout(0) with s.settimeout(0.1), I get OSError: -261 on conn.read
  • Same code works on ESP32
CANDIDATE · ISSUE

Non-blocking UDP socket returns ETIMEDOUT on recv instead on EAGAIN on ESP8266

closedby adamplesopened 2020-03-14updated 2020-03-17
extmod

When reading from non-blocking UDP socket with no data available ETIMEDOUT error is returned. One would expect EAGAIN or EWOULDBLOCK in such situation.

For example:

>>> import usocket
>>> s = usocket.socket(usocket.AF_INET, usocket.SOCK_DGRAM)
>>> address = usocket.getaddrinfo('192.168.0.24', 4005)[0][-1]
>>> s.bind(address)
>>> s.settimeout(0)
>>> s
<socket state=0 timeout=0 incoming=0 off=0>
>>> s.recv(1)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
OSError: [Errno 110] ETIMEDOUT

The incorrect behaviour is observed for ESP8266 platform:

>>> sys.platform
'esp8266'
>>> sys.implementation
(name='micropython', version=(1, 12, 0), mpy=9733)

Looking at the modlwip.c code, there seems to be no special treatment applied for timeout value of 0:
https://github.com/micropython/micropython/blob/master/extmod/modlwip.c#L597

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