Skip to content

Commit

Permalink
Fix for race condition in refresh_schemas(), closes #1231
Browse files Browse the repository at this point in the history
  • Loading branch information
simonw authored Jul 16, 2021
1 parent dd5ee8e commit c00f29a
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
7 changes: 7 additions & 0 deletions datasette/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,7 @@ def __init__(
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")
Expand Down Expand Up @@ -332,6 +333,12 @@ def __init__(
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)
Expand Down
10 changes: 5 additions & 5 deletions datasette/utils/internal_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand Down

0 comments on commit c00f29a

Please sign in to comment.