Fix for race condition in refresh_schemas(), closes #1231

This commit is contained in:
Simon Willison 2021-07-16 12:44:58 -07:00 committed by GitHub
commit c00f29affc
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 12 additions and 5 deletions

View file

@ -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)

View file

@ -5,7 +5,7 @@ async def init_internal_db(db):
await db.execute_write(
textwrap.dedent(
"""
CREATE TABLE databases (
CREATE TABLE IF NOT EXISTS databases (
database_name TEXT PRIMARY KEY,
path TEXT,
is_memory INTEGER,
@ -18,7 +18,7 @@ async def init_internal_db(db):
await db.execute_write(
textwrap.dedent(
"""
CREATE TABLE tables (
CREATE TABLE IF NOT EXISTS tables (
database_name TEXT,
table_name TEXT,
rootpage INTEGER,
@ -33,7 +33,7 @@ async def init_internal_db(db):
await db.execute_write(
textwrap.dedent(
"""
CREATE TABLE columns (
CREATE TABLE IF NOT EXISTS columns (
database_name TEXT,
table_name TEXT,
cid INTEGER,
@ -54,7 +54,7 @@ async def init_internal_db(db):
await db.execute_write(
textwrap.dedent(
"""
CREATE TABLE indexes (
CREATE TABLE IF NOT EXISTS indexes (
database_name TEXT,
table_name TEXT,
seq INTEGER,
@ -73,7 +73,7 @@ async def init_internal_db(db):
await db.execute_write(
textwrap.dedent(
"""
CREATE TABLE foreign_keys (
CREATE TABLE IF NOT EXISTS foreign_keys (
database_name TEXT,
table_name TEXT,
id INTEGER,