Skip to content

Fix BlackSheep cross-task database access - #2248

Open
Sanjays2402 wants to merge 2 commits into
tortoise:developfrom
Sanjays2402:fix/blacksheep-global-context
Open

Fix BlackSheep cross-task database access#2248
Sanjays2402 wants to merge 2 commits into
tortoise:developfrom
Sanjays2402:fix/blacksheep-global-context

Conversation

@Sanjays2402

Copy link
Copy Markdown

Description

Enable the global context fallback when the BlackSheep integration initializes Tortoise.

Motivation and Context

BlackSheep startup and request handlers run in separate asyncio tasks. Without the fallback, every request-side database query raises No TortoiseContext is currently active. Fixes #2244.

How Has This Been Tested?

The regression test fails without the flag and passes with it. A BlackSheep lifecycle probe also initialized the ORM in one task and successfully queried SQLite from another.

Checklist:

  • My code follows the code style of this project.
  • My change requires a change to the documentation. (Not required.)
  • I have updated the documentation accordingly. (Not required.)
  • I have added the changelog accordingly. (Not required.)
  • I have read the CONTRIBUTING document.
  • I have added tests to cover my changes.
  • All new and existing tests passed. (The focused test passed; the full suite was not run.)

BlackSheep initializes the ORM in its lifespan task, but request tasks could not see that context because the global fallback was disabled. Enable the same fallback used by the other ASGI integrations and add a regression test for the startup configuration.
@codspeed-hq

codspeed-hq Bot commented Jul 21, 2026

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 24 untouched benchmarks


Comparing Sanjays2402:fix/blacksheep-global-context (9d35ff7) with develop (8c12adc)

Open in CodSpeed

@waketzheng

Copy link
Copy Markdown
Contributor

Please fix lint issue.

@Sanjays2402

Copy link
Copy Markdown
Author

The failing job was check — mypy, not ruff:

tests/contrib/test_blacksheep.py:24: error: Call to untyped function "start" in typed context  [no-untyped-call]
tests/contrib/test_blacksheep.py:33: error: Call to untyped function "stop" in typed context  [no-untyped-call]

disallow_untyped_calls = true applies to tests too (the tests.* override only relaxes check_untyped_defs/disallow_untyped_defs), and blacksheep's Application.start/stop are unannotated upstream. Added # type: ignore[no-untyped-call] on the two call sites, matching how tests elsewhere in the repo handle third-party typing gaps.

mypy tests/contrib/test_blacksheep.py and ruff format --check/ruff check are clean, and pytest tests/contrib/test_blacksheep.py is 1 passed. Pushed 9d35ff7.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

2 participants