From b6ef48f90f935c39bcf0db92d65efcafc6c59713 Mon Sep 17 00:00:00 2001 From: Dominic Griesel Date: Tue, 14 Oct 2025 14:16:46 +0200 Subject: [PATCH] fix: catch serializer errors, prevent OOM on compress error --- src/lib/db.ts | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/lib/db.ts b/src/lib/db.ts index 4affc4a..1c6051e 100644 --- a/src/lib/db.ts +++ b/src/lib/db.ts @@ -679,7 +679,14 @@ export class JsonlDB { private entryToLine(key: string, value?: V, timestamp?: number): string { if (value !== undefined) { const k = key; - const v = this.options.serializer?.(key, value) ?? value; + let v: any; + try { + v = this.options.serializer?.(key, value); + } catch { + // This can happen when the application defines an invalid serializer + // In this case simply preserve the raw value, so the issue can be fixed without data loss + } + v ??= value; if (this.options.enableTimestamps && timestamp !== undefined) { return JSON.stringify({ k, v, ts: timestamp }); @@ -987,6 +994,11 @@ export class JsonlDB { lastCompress = Date.now(); task.done?.resolve(); } catch (e) { + // On error we're likely left with a closed DB file handle, causing further issues. + // Attempt to at least reopen the file + if (!this._handle) { + this._handle = await fs.open(this.filename, "a+"); + } task.done?.reject(e); } break;