[GH-ISSUE #73] SQLite crashes when importing large number of files from SillyTavern #10

Closed
opened 2026-10-20 17:24:01 +07:00 by bsakaguchi · 5 comments
Owner

Originally created by @Limezero on GitHub (Apr 9, 2026).
Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/73

OS: Windows 10
Node: v20.18.1
Marinara-Engine Release: v1.4.8
Marinara-Engine Build: 0d7d59d

When trying to use the "Import from SillyTavern" feature with a large SillyTavern instance (2883 chats in this case), node.exe will randomly crash in the middle of the import with error code 0xC0000005 Access Violation (Segmentation Fault) and no error message. This is a pain in the ass to debug since the crash happens before anything has a chance of intercepting it, but WinDbg claims it's coming from INVALID_POINTER_READ_c0000005_index.node!Unknown. index.node is likely Marinara-Engine\node_modules\.pnpm\@libsql+win32-x64-msvc@0.5.22\node_modules\@libsql\win32-x64-msvc\index.node, i.e. the libsql driver.

Adding a timeout of 100ms before every loop or using a transaction does nothing, but forcing a different driver by putting DATABASE_DRIVER=better-sqlite3 into .env seems to fix it. I don't know if this is happening on a different Node version, different OS or different libsql, I don't have the energy to debug this further. Just thought I'd report it, since it might bite somebody in the ass later.

As an aside, restarting a failed import like this also duplicates every single card and chatlog, which is very bad UX. You should probably check if an identical entry already exists, otherwise people will end up with 2-3 copies of the same card, potentially with hundreds of branches duplicated across all of them, and no good way of knowing what's linked to what.

Originally created by @Limezero on GitHub (Apr 9, 2026). Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/73 **OS**: Windows 10 **Node**: v20.18.1 **Marinara-Engine Release**: v1.4.8 **Marinara-Engine Build**: 0d7d59d When trying to use the "Import from SillyTavern" feature with a large SillyTavern instance (2883 chats in this case), node.exe will randomly crash in the middle of the import with error code `0xC0000005 Access Violation (Segmentation Fault)` and no error message. This is a pain in the ass to debug since the crash happens before anything has a chance of intercepting it, but WinDbg claims it's coming from `INVALID_POINTER_READ_c0000005_index.node!Unknown`. `index.node` is likely `Marinara-Engine\node_modules\.pnpm\@libsql+win32-x64-msvc@0.5.22\node_modules\@libsql\win32-x64-msvc\index.node`, i.e. the libsql driver. Adding a timeout of 100ms before every loop or using a transaction does nothing, but forcing a different driver by putting `DATABASE_DRIVER=better-sqlite3` into `.env` seems to fix it. I don't know if this is happening on a different Node version, different OS or different libsql, I don't have the energy to debug this further. Just thought I'd report it, since it might bite somebody in the ass later. As an aside, restarting a failed import like this also duplicates every single card and chatlog, which is very bad UX. You should probably check if an identical entry already exists, otherwise people will end up with 2-3 copies of the same card, potentially with hundreds of branches duplicated across all of them, and no good way of knowing what's linked to what.
Author
Owner

@Limezero commented on GitHub (Apr 9, 2026):

Never mind, apparently all of the code is async, so better-sqlite3 doesn't work. No crashes, but none of the chats get imported with it either.

Image
<!-- gh-comment-id:4215649754 --> @Limezero commented on GitHub (Apr 9, 2026): Never mind, apparently all of the code is async, so better-sqlite3 doesn't work. No crashes, but none of the chats get imported with it either. <img width="524" height="218" alt="Image" src="https://github.com/user-attachments/assets/d9a87e8c-bfcd-439b-bad0-e3141be65cf7" />
Author
Owner

@Limezero commented on GitHub (Apr 9, 2026):

Gemini came up with this:

--- a/packages/server/src/services/storage/chats.storage.ts
+++ b/packages/server/src/services/storage/chats.storage.ts
@@ -247,7 +247,5 @@ export function createChatsStorage(db: DB) {
-      await db.transaction(async (tx) => {
-        for (let i = 0; i < msgRows.length; i += CHUNK) {
-          await tx.insert(messages).values(msgRows.slice(i, i + CHUNK));
-          await tx.insert(messageSwipes).values(swipeRows.slice(i, i + CHUNK));
-        }
-        await tx.update(chats).set({ updatedAt: lastTimestamp }).where(eq(chats.id, chatId));
-      });
+      for (let i = 0; i < msgRows.length; i += CHUNK) {
+        await db.insert(messages).values(msgRows.slice(i, i + CHUNK));
+        await db.insert(messageSwipes).values(swipeRows.slice(i, i + CHUNK));
+      }
+      await db.update(chats).set({ updatedAt: lastTimestamp }).where(eq(chats.id, chatId));

Removing the transaction and sending everything to the DB one by one fixes it, for whatever reason. The robot claims it's a "Use-After-Free or Race Condition when managing stateful transaction objects on Windows in libSQL", which sounds like a thing that someone who isn't me needs to look into.

<!-- gh-comment-id:4216069430 --> @Limezero commented on GitHub (Apr 9, 2026): Gemini came up with this: ```diff --- a/packages/server/src/services/storage/chats.storage.ts +++ b/packages/server/src/services/storage/chats.storage.ts @@ -247,7 +247,5 @@ export function createChatsStorage(db: DB) { - await db.transaction(async (tx) => { - for (let i = 0; i < msgRows.length; i += CHUNK) { - await tx.insert(messages).values(msgRows.slice(i, i + CHUNK)); - await tx.insert(messageSwipes).values(swipeRows.slice(i, i + CHUNK)); - } - await tx.update(chats).set({ updatedAt: lastTimestamp }).where(eq(chats.id, chatId)); - }); + for (let i = 0; i < msgRows.length; i += CHUNK) { + await db.insert(messages).values(msgRows.slice(i, i + CHUNK)); + await db.insert(messageSwipes).values(swipeRows.slice(i, i + CHUNK)); + } + await db.update(chats).set({ updatedAt: lastTimestamp }).where(eq(chats.id, chatId)); ``` Removing the transaction and sending everything to the DB one by one fixes it, for whatever reason. The robot claims it's a "Use-After-Free or Race Condition when managing stateful transaction objects on Windows in libSQL", which sounds like a thing that someone who isn't me needs to look into.
Author
Owner

@SpicyMarinara commented on GitHub (Apr 16, 2026):

addressed in v1.5.0.

<!-- gh-comment-id:4261980708 --> @SpicyMarinara commented on GitHub (Apr 16, 2026): addressed in v1.5.0.
Author
Owner

@Limezero commented on GitHub (Apr 16, 2026):

It... doesn't seem to be? I'm not sure what "v1.5.0" is, but the issue is still happening with the latest commit on main (8ebd810). I've reinstalled and updated Node since then, which hasn't fixed the issue either.

<!-- gh-comment-id:4262572160 --> @Limezero commented on GitHub (Apr 16, 2026): It... doesn't seem to be? I'm not sure what "v1.5.0" is, but the issue is still happening with the latest commit on main (8ebd810). I've reinstalled and updated Node since then, which hasn't fixed the issue either.
Author
Owner

@SpicyMarinara commented on GitHub (Apr 16, 2026):

I mean, I'd be surprised if the fix was applied already, given that v1.5.0 is not released yet.

<!-- gh-comment-id:4262610099 --> @SpicyMarinara commented on GitHub (Apr 16, 2026): I mean, I'd be surprised if the fix was applied already, given that v1.5.0 is not released yet.
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
Pasta-Devs/Marinara-Engine#10
No description provided.