Skip to content
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

fix: queries against closed database #3295

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .changeset/strange-fishes-applaud.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@lix-js/sdk": patch
---

fix: queries against closed database

closes https://github.com/opral/lix-sdk/issues/226
19 changes: 11 additions & 8 deletions packages/lix-sdk/src/file-queue/file-queue-process.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import type { SqliteDatabase } from "sqlite-wasm-kysely";
import {
handleFileUpdate,
handleFileInsert,
Expand All @@ -8,9 +7,8 @@ import type { Lix } from "../lix/open-lix.js";

export async function initFileQueueProcess(args: {
lix: Pick<Lix, "db" | "plugin" | "sqlite">;
rawDatabase: SqliteDatabase;
}): Promise<void> {
args.rawDatabase.createFunction({
args.lix.sqlite.createFunction({
name: "triggerFileQueue",
arity: 0,
// @ts-expect-error - dynamic function
Expand All @@ -29,6 +27,12 @@ export async function initFileQueueProcess(args: {
let hasMoreEntriesSince: number | undefined = undefined;

async function queueWorker(trail = false) {

if (args.lix.sqlite.isOpen() === false) {
console.log("sqlite is closed");
return;
}

try {
if (pending && !trail) {
hasMoreEntriesSince = runNumber;
Expand Down Expand Up @@ -81,15 +85,14 @@ export async function initFileQueueProcess(args: {
!hasMoreEntriesSince ||
(numEntries === 0 && hasMoreEntriesSince < runNumber)
) {
resolve!(); // TODO: fix type
resolve!();
hasMoreEntriesSince = undefined;
pending = undefined;
// console.log("resolving");
} else {
// there are more entries to process
queueWorker(true);
}

// TODO: handle endless tries on failing quee entries
// we either execute the queue immediately if we know there is more work or fall back to polling
setTimeout(() => queueWorker(true), hasMoreEntriesSince ? 0 : 1000);
} catch (e) {
console.error("file queue failed ", e);
}
Expand Down
8 changes: 8 additions & 0 deletions packages/lix-sdk/src/lix/close-lix.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import type { Lix } from "./open-lix.js";

/**
* Closes the lix.
*/
export async function closeLix(args: { lix: Pick<Lix, "db"> }): Promise<void> {
await args.lix.db.destroy();
}
5 changes: 2 additions & 3 deletions packages/lix-sdk/src/lix/new-lix.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
contentFromDatabase,
} from "sqlite-wasm-kysely";
import { initDb } from "../database/init-db.js";
import { closeLix } from "./close-lix.js";

/**
* Creates a new lix file.
Expand All @@ -23,8 +24,6 @@ export async function newLixFile(): Promise<Blob> {
} catch (e) {
throw new Error(`Failed to create new Lix file: ${e}`, { cause: e });
} finally {
// in any case destroy the memory db
sqlite.close();
await db.destroy();
closeLix({ lix: { db } });
}
}
5 changes: 1 addition & 4 deletions packages/lix-sdk/src/lix/open-lix.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,7 @@ export async function openLix(args: {
getAll: async () => plugins,
};

initFileQueueProcess({
lix: { db, plugin, sqlite: args.database },
rawDatabase: args.database,
});
initFileQueueProcess({ lix: { db, plugin, sqlite: args.database } });

initSyncProcess({ lix: { db, plugin, sqlite: args.database } });

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { fileQueueSettled } from "../../file-queue/file-queue-settled.js";
import { closeLix } from "../../lix/close-lix.js";
import { openLixInMemory } from "../../lix/open-lix-in-memory.js";
import type { Lix } from "../../lix/open-lix.js";
import { toBlob } from "../../lix/to-blob.js";
Expand Down Expand Up @@ -101,8 +103,9 @@ export const createLsaInMemoryEnvironment = (): LsaEnvironment => {
if (connections.size === 0) {
// TODO no concurrency guarantees
const lix = openLixes.get(args.id);
await fileQueueSettled({ lix: lix! });
const blob = await toBlob({ lix: lix! });
lix?.sqlite.close();
await closeLix({ lix: lix! });
openConnections.delete(args.id);
openLixes.delete(args.id);
store.set(args.id, blob);
Expand Down
2 changes: 0 additions & 2 deletions packages/lix-sdk/src/server-api-handler/routes/get-v1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,6 @@ export const route: LixServerApiHandlerRoute = async (context) => {

const blob2 = new Blob([contentFromDatabase(sqlite)]);

sqlite.close();

return new Response(blob2, {
status: 200,
headers: {
Expand Down
17 changes: 2 additions & 15 deletions packages/lix-sdk/src/sync/sync-process.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,13 @@ import { toBlob } from "../lix/to-blob.js";

export async function initSyncProcess(args: {
lix: Pick<Lix, "db" | "plugin" | "sqlite">;
}): Promise<
| {
stop: () => void;
}
| undefined
> {
}): Promise<void> {
const lixId = await args.lix.db
.selectFrom("key_value")
.where("key", "=", "lix_id")
.select("value")
.executeTakeFirstOrThrow();

let stoped = false;

const pullAndPush = async () => {
const shouldSync = await args.lix.db
.selectFrom("key_value")
Expand Down Expand Up @@ -78,7 +71,7 @@ export async function initSyncProcess(args: {
// naive implementation that syncs every second

function schedulePullAndPush() {
if (!stoped) {
if (args.lix.sqlite.isOpen()) {
pullAndPush().catch((e) => {
console.error("Error in sync process", e);
});
Expand All @@ -89,10 +82,4 @@ export async function initSyncProcess(args: {
}

schedulePullAndPush();

return {
stop: () => {
stoped = true;
},
};
}
Loading