Skip to content
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

Event.set() with an EventAdapter does not work unless already running in an async event loop #819

Open
2 tasks done
fyellin opened this issue Nov 5, 2024 · 6 comments
Open
2 tasks done
Labels
bug Something isn't working

Comments

@fyellin
Copy link

fyellin commented Nov 5, 2024

Things to check first

  • I have searched the existing issues and didn't find my bug already reported there

  • I have checked that my bug is still present in the latest release

AnyIO version

4.6.2

Python version

3.12

What happened?

Unexpected exception. Code that runs fine with trio and asyncio fails with anyio.

How can we reproduce the bug?

In a newly started Python:

from anyio import Event
event = Event()
event.set()

This code works fine if we replace anyio with trio or asyncio. On anyio I get the error message AsyncLibraryNotFound.

It seems strange that anyio has no problem creating the Event, but fails in setting it.

@fyellin fyellin added the bug Something isn't working label Nov 5, 2024
@agronholm
Copy link
Owner

Well, you're not running any async event loop here, so it doesn't know how to perform the notification, that's all. In an earlier version you wouldn't have gotten even that far, as already the creation of an Event would have failed for the same reason.

@agronholm
Copy link
Owner

On asyncio or trio, their own implementations know to use an asyncio or trio event loop, respectively, but with AnyIO, which event loop should it even try here? If this is a pressing use case, the current event proxy class could be extended to allow the set() operation to just flip the flag if there's no backing implementation.

@fyellin
Copy link
Author

fyellin commented Nov 5, 2024 via email

@agronholm
Copy link
Owner

Do you mean you have a callback that sets an event from a thread that is not running an event loop?

@fyellin
Copy link
Author

fyellin commented Nov 5, 2024 via email

@agronholm
Copy link
Owner

If the event is supposed to notify code running in an event loop thread, then setting the event from just any thread is a big no-no! These events (or asyncio or trio events for that matter) are NOT thread safe! It's imperative to call one of the thread-safe entry mechanisms in order to correctly notify waiters in an async event loop.

@agronholm agronholm changed the title anyu Event.set() with an EventAdapter does not work unless already running in an async event loop Nov 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants