Skip to content

ports/unix: uasyncio.ThreadSafeFlag appears broken #7965

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
crzcrz opened this issue Nov 3, 2021 · 1 comment
Open

ports/unix: uasyncio.ThreadSafeFlag appears broken #7965

crzcrz opened this issue Nov 3, 2021 · 1 comment

Comments

@crzcrz
Copy link

crzcrz commented Nov 3, 2021

The minimal program to reproduce the issue:

import uasyncio as asyncio

f = asyncio.ThreadSafeFlag()

async def main():
    await f.wait()

asyncio.run(main())

The result:

Traceback (most recent call last):
  File "t.py", line 8, in <module>
  File "/usr/lib/micropython/uasyncio/core.py", line 222, in run
  File "/usr/lib/micropython/uasyncio/core.py", line 191, in run_until_complete
  File "/usr/lib/micropython/uasyncio/core.py", line 176, in run_until_complete
  File "t.py", line 6, in main
  File "/usr/lib/micropython/uasyncio/event.py", line 57, in wait
  File "/usr/lib/micropython/uasyncio/core.py", line 94, in queue_read
  File "/usr/lib/micropython/uasyncio/core.py", line 79, in _enqueue
TypeError: can't convert NoneType to int

I poked around it a bit, and the flow to the failure is as follows:

self.poller.register(s, select.POLLIN if idx == 0 else select.POLLOUT)

int fd = get_fd(args[1]);

mp_uint_t res = stream_p->ioctl(fdlike, MP_STREAM_GET_FILENO, 0, &err);

This ends up in ThreadSafeFlag.ioctl(), which returns None for any request other than MP_STREAM_POLL and causes the exception above.

def ioctl(self, req, flags):
if req == 3: # MP_STREAM_POLL
return self._flag * flags
return None

@jimmo
Copy link
Member

jimmo commented Nov 3, 2021

On everything-except-unix, we use a built-in implementation of select() (in extmod/moduselect.c), which knows how to poll Python objects implementing the stream protocol. However, on Unix it uses the "native" (i.e. posix) select, which only knows about file descriptors (hence why None can't be converted to int).

The correct fix is to make the Unix select implementation also support Python objects (i.e. a hybrid of extmod/moduselect.c and ports/unix/moduselect.c), but in the meantime you can build the Unix port with the built-in select implementation instead by adding the following to mpconfigport.h (or mpconfigvariant.h).

#define MICROPY_PY_USELECT_POSIX (0)
#define MICROPY_PY_USELECT (1)

I have a variant that I use for BLE testing and aioble development that sets this.

I've wondered about proposing to make this the default for the unix port... (generally my view is that the default unix port should mirror the bare-metal port, and a "more CPython like" build can be enabled via a variant) but other use cases depend on the posix behavior so this would be a breaking change.

Longer term we plan to improve uasyncio's event-waiting (see #6125 and linked PRs) this will work the same across the different ports.

In the meantime, it's probably an oversight that https://docs.micropython.org/en/latest/library/uasyncio.html#class-threadsafeflag doesn't mention this behavior.

peterhinch added a commit to peterhinch/micropython that referenced this issue Nov 15, 2022
As per micropython#7965 this class does not work on the Unix build.
dpgeorge pushed a commit to peterhinch/micropython that referenced this issue Nov 16, 2022
As per Issue micropython#7965, this class does not work on the Unix build.
karfas pushed a commit to karfas/micropython that referenced this issue Apr 23, 2023
As per Issue micropython#7965, this class does not work on the Unix build.
alphonse82 pushed a commit to alphonse82/micropython-wch-ch32v307 that referenced this issue May 8, 2023
As per Issue micropython#7965, this class does not work on the Unix build.
tannewt added a commit to tannewt/circuitpython that referenced this issue May 12, 2023
STM: monotonic time even when RTC is changed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants