← index #7473PR #3395
Likely Duplicate · high · value 0.847
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 · PULL REQUEST

extmod/modussl_axtls: socket_read: Handle EAGAIN.

closedby pfalconopened 2017-10-29updated 2017-11-01

If SSL_EAGAIN is returned (which is a feature of MicroPython's axTLS fork),
return EAGAIN.

Original axTLS returns SSL_OK both when there's no data to return to user
yet and when the underlying stream returns EAGAIN. That's not distinctive
enough, for example, original module code works well for blocking stream,
but will infinite-loop for non-blocking socket with EAGAIN. But if we fix
non-blocking case, blocking calls to .read() will return few None's initially
(while axTLS progresses thru handshake).

Using SSL_EAGAIN allows to fix non-blocking case without regressing the
blocking one.

Note that this only handles case of non-blocking reads of application data.
Initial handshake and writes still don't support non-blocking mode and must
be done in the blocking way.

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