Skip to content

Commit

Permalink
Fix LiveServer for in-memory SQLite database
Browse files Browse the repository at this point in the history
`allow_thread_sharing` is no longer writeable since Django 2.2.
  • Loading branch information
Michael Howitz authored and bluetech committed Dec 1, 2021
1 parent 46c7d61 commit 2bba6eb
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 1 deletion.
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ Donald Stufft <donald@stufft.io>
Nicolas Delaby <ticosax@free.fr>
Daniel Hahler <https://twitter.com/blueyed>
Hasan Ramezani <hasan.r67@gmail.com>
Michael Howitz
10 changes: 10 additions & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
Changelog
=========

unreleased
----------

Bugfixes
^^^^^^^^

* Fix :fixture:`live_server` when using an in-memory SQLite database on
Django >= 3.0.


v4.4.0 (2021-06-06)
-------------------

Expand Down
6 changes: 5 additions & 1 deletion pytest_django/live_server_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def __init__(self, addr: str) -> None:
and conn.settings_dict["NAME"] == ":memory:"
):
# Explicitly enable thread-shareability for this connection
conn.allow_thread_sharing = True
conn.inc_thread_sharing()
connections_override[conn.alias] = conn

liveserver_kwargs["connections_override"] = connections_override
Expand Down Expand Up @@ -60,8 +60,12 @@ def __init__(self, addr: str) -> None:

def stop(self) -> None:
"""Stop the server"""
# Terminate the live server's thread.
self.thread.terminate()
self.thread.join()
# Restore shared connections' non-shareability.
for conn in self.thread.connections_override.values():
conn.dec_thread_sharing()

@property
def url(self) -> str:
Expand Down

0 comments on commit 2bba6eb

Please sign in to comment.