Skip to content

Local Zotero: withZoteroDb closes the DB before an async callback finishes (missing await) #1138

Description

@nealrichardson

Bug (latent)

withZoteroDbExclusive() in packages/editor-server/src/core/zotero/local/db.ts:74 does:

try {
  db = new Database(...);
  ...
  return f(db);        // f: (db) => Promise<T>
} finally {
  if (db) db.close();  // runs as soon as f returns its promise
  ...
}

Because f(db) is returned without await, the finally runs as soon as f hands back its promise, not when that promise settles. If f ever awaits anything before it finishes using db, the database is closed out from under it, and the next db.all(...) throws.

Current status

It doesn't bite today. The node-sqlite3-wasm API is synchronous, and all three callers in source.ts (lines ~36, ~95, ~120) are async callbacks that never await, so their DB work finishes synchronously before the promise is returned. The trap is that anyone who adds an await inside one of those callbacks (a network call to the Zotero web API, a file read, etc.) breaks it, and the resulting "database closed" error will be confusing.

Fix

return await f(db);

That's the whole change. withZoteroDbExclusive is already async, so this just moves close() until after f settles. Consider adding a unit test with a callback that awaits (e.g. await Promise.resolve()) before querying.

Found during the dependency-upgrade research.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions