Skip to content

polling on a listening socket does not work #8964

Open
@seandepagnier

Description

@seandepagnier

CircuitPython version

Adafruit CircuitPython 9.0.0-beta.2 on 2024-02-20; Raspberry Pi Pico W with rp2040
Adafruit CircuitPython 8.2.10 on 2024-02-14; Raspberry Pi Pico W with rp2040

Code/REPL

import time
import wifi, ipaddress, socketpool

import select

ssid="ssid"
password="anything"

#Connect to WLAN                                                                                                                                                                             
pool = socketpool.SocketPool(wifi.radio)

server_connection = False

poller = select.poll()

wifi.radio.connect(ssid, password)
while not wifi.radio.connected:
    time.sleep(1)

print("connected")
# Open a socket                                                                                                                                                                              
host = str(wifi.radio.ipv4_address)

address = (host, 1234)
print('using address', *address)
server_connection = pool.socket(pool.AF_INET, pool.SOCK_STREAM)                                                                                                                                                            
server_connection.bind(address)
server_connection.listen(2)
poller.register(server_connection, select.POLLIN)
print('server socket', server_connection)

while True:
    events = poller.poll(1000)
    for event in events:
        conn = event[0]
        flags = event[1]
        print('event', conn, flags)
        client, addr = conn.accept()
        print('accept return')
        client.send("hi there\n")
        client.close()
    print("poll loop")

Behavior

In micropython it is possible to poll on listening sockets exactly like in regular python. In circuit python, poll always returns with an event on POLLIN, it should only do this when a subsequent call to accept will not block.

Description

This is a simple example, not meant to have practical use, where select.poll always returns immediately even if there is no connection causing conn.accept() to block.

Additional information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions