Skip to content

Reject generator functions in sync_to_async - #566

Open
lin-hongkuan wants to merge 1 commit into
django:mainfrom
lin-hongkuan:codex/reject-generator-sync-to-async
Open

Reject generator functions in sync_to_async#566
lin-hongkuan wants to merge 1 commit into
django:mainfrom
lin-hongkuan:codex/reject-generator-sync-to-async

Conversation

@lin-hongkuan

Copy link
Copy Markdown

Summary

  • reject synchronous and asynchronous generator functions in sync_to_async() with the existing TypeError path
  • apply the same detection to callable instances via their __call__ method
  • add regression tests for generator functions and generator callable instances

sync_to_async() currently calls a generator function in the worker thread only far enough to create the generator object. Iterating that generator then runs the body in the caller thread, which is the confusing behavior reported in the issue. Raising early is safer than returning an object that does not actually execute under the adapter.

Fixes #38

Tests

  • python -m pytest tests\test_sync.py -q
  • python -m pytest -q
  • python -m mypy asgiref tests\test_sync.py
  • git diff --check

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 this pull request may close these issues.

Genorators using database_sync_to_async are called on the main thread

1 participant