-
Notifications
You must be signed in to change notification settings - Fork 263
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: incorrect concurrent usage of connection and transaction (#546)
* fix: incorrect concurrent usage of connection and transaction * refactor: rename contextvar class attributes, add some explaination comments * fix: contextvar.get takes no keyword arguments * test: add concurrent task tests * feat: use ContextVar[dict] to track connections and transactions per task * test: check multiple databases in the same task use independant connections * chore: changes for linting and typechecking * chore: use typing.Tuple for lower python version compatibility * docs: update comment on _connection_contextmap * Update `Connection` and `Transaction` to be robust to concurrent use * chore: remove optional annotation on asyncio.Task * test: add new tests for upcoming contextvar inheritance/isolation and weakref cleanup * feat: reimplement concurrency system with contextvar and weakmap * chore: apply corrections from linters * fix: quote WeakKeyDictionary typing for python<=3.7 * docs: add examples for async transaction context and nested transactions * fix: remove connection inheritance, add more tests, update docs Connections are once again stored as state on the Database instance, keyed by the current asyncio.Task. Each task acquires it's own connection, and a WeakKeyDictionary allows the connection to be discarded if the owning task is garbage collected. TransactionBackends are still stored as contextvars, and a connection must be explicitly provided to descendant tasks if active transaction state is to be inherited. --------- Co-authored-by: Zanie <contact@zanie.dev>
- Loading branch information
Showing
3 changed files
with
521 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.