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

from_thread.run(_sync)? does not set sniffio on asyncio #523

Closed
gschaffner opened this issue Jan 18, 2023 · 2 comments · Fixed by #524
Closed

from_thread.run(_sync)? does not set sniffio on asyncio #523

gschaffner opened this issue Jan 18, 2023 · 2 comments · Fixed by #524

Comments

@gschaffner
Copy link
Collaborator

gschaffner commented Jan 18, 2023

note that from_thread.run(_sync)? does set sniffio on trio, however.

this causes a few bugs:

  1. from_thread.run(_sync)? fails when used to call sniffio-dependent functions when the to_thread worker is running a non-asyncio async backend known to sniffio (i.e. trio, curio, or (situationally) trio-asyncio) and the thread that started the worker is on asyncio.

    reproducer: bc3cc49.

  2. because BlockingPortal._spawn_task_from_thread shares its underlying implementation with from_thread.run_sync on asyncio, using a BlockingPortal that was started by start_blocking_portal("asyncio") in a non-asyncio async backend known to sniffio deadlocks.

    reproducer: cd9deb1.

@tapetersen
Copy link

At least for sync callbacks in asyncio this is an issue I also ran into and wanted to solve by detecting asyncio as backend if we were running a sync task directly in sniffIO. Would be interested in either this solution or mine going forward

issue python-trio/sniffio#35
PR python-trio/sniffio#36

@gschaffner
Copy link
Collaborator Author

gschaffner commented Mar 10, 2023

@tapetersen i think python-trio/sniffio#36 would still not fix this in all cases. even if sniffio properly detected asyncio callbacks that aren't part of a task, sniffio won't run that detection code if sniffio.thread_local or sniffio.current_async_library_cvar are set. and an incorrect sniffio.current_async_library_cvar can be inherited from the code that calls from_thread.run(_sync)?!

(linking #501 for convenience—#524 would fix it!)

gschaffner added a commit to gschaffner/anyio that referenced this issue Mar 10, 2023
This also fixes a deadlock when using start_blocking_portal("asyncio")
on a non-asyncio async backend known to sniffio.

Fixes agronholm#523.
gschaffner added a commit to gschaffner/anyio that referenced this issue Mar 14, 2023
This also fixes a deadlock when using start_blocking_portal("asyncio")
on a non-asyncio async backend known to sniffio.

Fixes agronholm#523.
gschaffner added a commit to gschaffner/anyio that referenced this issue Mar 14, 2023
This also fixes a deadlock when using start_blocking_portal("asyncio")
on a non-asyncio async backend known to sniffio.

Fixes agronholm#523.
gschaffner added a commit to gschaffner/anyio that referenced this issue Mar 14, 2023
This also fixes a deadlock when using start_blocking_portal("asyncio")
on a non-asyncio async backend known to sniffio.

Fixes agronholm#523.
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

Successfully merging a pull request may close this issue.

2 participants