← index #4824Issue #2947
Related · medium · value 1.983
QUERY · ISSUE

non-blocking socket blocks and raises EAGAIN

openby c0d3z3r0opened 2019-05-30updated 2019-10-16
port-esp8266

from https://github.com/pfalcon/micropython/issues/30

Problem: a non-blocking socket seems to block and raises EAGAIN (was EWOULDBLOCK some time ago)

paste mode; Ctrl-C to cancel, Ctrl-D to finish
=== import socket as _socket
=== ai = _socket.getaddrinfo("127.0.0.1", 85, 0, _socket.SOCK_STREAM)
=== ai = ai[0]
=== s = _socket.socket(ai[0], ai[1], ai[2])
=== s.setblocking(False)
=== s.setsockopt(_socket.SOL_SOCKET, _socket.SO_REUSEADDR, 1)
=== s.bind(ai[-1])
=== s.listen(0)
=== 
>>> s2, client_addr = s.accept()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
OSError: [Errno 11] EAGAIN

This problem only appears, when AP mode is disabled. Connecting to a WiFi does not help.
See https://github.com/pfalcon/micropython/issues/30#issuecomment-497322149

Catching EAGAIN works but this only circumvents that bug...

paste mode; Ctrl-C to cancel, Ctrl-D to finish
=== import socket as _socket
=== ai = _socket.getaddrinfo("127.0.0.1", 85, 0, _socket.SOCK_STREAM)
=== ai = ai[0]
=== s = _socket.socket(ai[0], ai[1], ai[2])
=== s.setblocking(False)
=== s.setsockopt(_socket.SOL_SOCKET, _socket.SO_REUSEADDR, 1)
=== s.bind(ai[-1])
=== s.listen(0)
=== 
>>> try:
>>>   s2, client_addr = s.accept()
>>> except OSError as e:
>>>   if e.args[0] == uerrno.EAGAIN:
>>>     continue
>>>


CANDIDATE · ISSUE

esp8266: in non-blocking mode, socket.accept raises ETIMEDOUT instead of EAGAIN

closedby dpgeorgeopened 2017-03-10updated 2017-06-21
bug

Test code:

import socket
s = socket.socket()
s.bind(socket.getaddrinfo("0.0.0.0", 8000)[0][-1])
s.setblocking(False)
s.listen(1)
s.accept()

On CPy and uPy unix the accept raises EAGAIN. On esp8266 it raises ETIMEDOUT.

Note that if the socket is in a blocking mode with a finite timeout (eg s.settimeout(1)) then ETIMEDOUT is the correct error code to raise (matches CPy's socket.timeout exception for this case).

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