-
-
Notifications
You must be signed in to change notification settings - Fork 700
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
Race condition errors in new refresh_schemas() mechanism #1231
Comments
I started trying to use locks to resolve this but I've not figured out the right way to do that yet - here's my first experiment: diff --git a/datasette/app.py b/datasette/app.py
index 9e15a16..1681c9d 100644
--- a/datasette/app.py
+++ b/datasette/app.py
@@ -217,6 +217,7 @@ class Datasette:
self.inspect_data = inspect_data
self.immutables = set(immutables or [])
self.databases = collections.OrderedDict()
+ self._refresh_schemas_lock = threading.Lock()
if memory or not self.files:
self.add_database(Database(self, is_memory=True), name="_memory")
# memory_name is a random string so that each Datasette instance gets its own
@@ -324,6 +325,13 @@ class Datasette:
self.client = DatasetteClient(self)
async def refresh_schemas(self):
+ return
+ if self._refresh_schemas_lock.locked():
+ return
+ with self._refresh_schemas_lock:
+ await self._refresh_schemas()
+
+ async def _refresh_schemas(self):
internal_db = self.databases["_internal"]
if not self.internal_db_created:
await init_internal_db(internal_db) |
Ideally I'd figure out a way to replicate this error in a concurrent unit test. |
This just broke the |
Here's the traceback I got from
|
The race condition happens inside this method - initially with the call to Lines 334 to 359 in dd5ee8e
|
The only place that calls datasette/datasette/views/base.py Lines 120 to 124 in dd5ee8e
Ideally only one call to |
https://stackoverflow.com/a/25799871/6083 has a good example of using stuff_lock = asyncio.Lock()
async def get_stuff(url):
async with stuff_lock:
if url in cache:
return cache[url]
stuff = await aiohttp.request('GET', url)
cache[url] = stuff
return stuff |
Second attempt at this: diff --git a/datasette/app.py b/datasette/app.py
index 5976d8b..5f348cb 100644
--- a/datasette/app.py
+++ b/datasette/app.py
@@ -224,6 +224,7 @@ class Datasette:
self.inspect_data = inspect_data
self.immutables = set(immutables or [])
self.databases = collections.OrderedDict()
+ self._refresh_schemas_lock = asyncio.Lock()
self.crossdb = crossdb
if memory or crossdb or not self.files:
self.add_database(Database(self, is_memory=True), name="_memory")
@@ -332,6 +333,12 @@ class Datasette:
self.client = DatasetteClient(self)
async def refresh_schemas(self):
+ if self._refresh_schemas_lock.locked():
+ return
+ async with self._refresh_schemas_lock:
+ await self._refresh_schemas()
+
+ async def _refresh_schemas(self):
internal_db = self.databases["_internal"]
if not self.internal_db_created:
await init_internal_db(internal_db) |
The test suite passes with that change. |
I can't replicate the race condition locally with or without this patch. I'm going to push the commit and then test the CI run from |
That fixed the race condition in the |
I tried running a Locust load test against Datasette and hit an error message about a failure to create tables because they already existed. I think this means there are race conditions in the new
refresh_schemas()
mechanism added in #1150.The text was updated successfully, but these errors were encountered: