[GH-ISSUE #2621] [Issue]: Knowledge-sources meta.json lost-update / TOCTOU — write serialization does not protect the read-modify-write #1293

Closed
opened 2026-10-21 05:30:52 +07:00 by bsakaguchi · 2 comments
Owner

Originally created by @Romuromylus on GitHub (Jun 18, 2026).
Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/2621

Originally assigned to: @Romuromylus on GitHub.

Summary

Severity: Low — concurrency (lost-update / TOCTOU). The knowledge-sources upload and delete handlers do a non-atomic read-modify-write on meta.json. The write-chain (metaWriteChain) serializes the write() calls but does not re-read under the lock, so two overlapping mutations each persist their own already-stale snapshot — the last writer clobbers the other's change, orphaning a just-uploaded file or resurrecting a just-deleted entry.

Expected behavior

Concurrent upload/delete of knowledge sources should each observe prior committed state. The read-modify-write must happen inside the serialized critical section.

Actual behavior

// packages/server/src/routes/knowledge-sources.routes.ts
// writeMeta (~50-62): serializes only the writeFile() via metaWriteChain — does NOT re-read meta
// upload (~139-148): const meta = readMeta(); meta[id] = entry; await writeMeta(meta);
// delete (~156-169): const meta = readMeta(); await unlink(filePath); delete meta[id]; await writeMeta(meta);

readMeta() is synchronous, but the handlers await between reading and writing (the delete handler's await unlink(filePath) is the clearest yield point). Two concurrent mutations whose readMeta() snapshots both predate the other's writeMeta() produce a classic lost update: the last queued writer persists its stale snapshot. Result: an orphaned file (just-uploaded entry vanishes from meta.json but the file stays on disk) or a resurrected entry (just-deleted entry reappears pointing at an unlinked file; a later /:id/text returns empty). The in-code comment claiming the chain prevents metadata being "corrupt or overwrite[n]" overstates the guarantee.

Steps to reproduce

  1. Issue two knowledge-source mutations close together (upload + delete, or two parallel uploads) so their readMeta() snapshots both predate the other's writeMeta().
  2. Observe one mutation lost: an orphaned on-disk file or a dangling meta.json entry.

Environment

  • Marinara Engine version: staging @ 56f700cd (v2.0.0)
  • Install type: source
  • OS + version: n/a — static source audit
  • Browser or app shell: n/a

Logs, screenshots, or video

Static source audit, cited to file:line above.

Additional context

Fix sketch. Perform the read-modify-write inside the serialized critical section: have writeMeta accept a mutator (current => next) that re-reads meta under the chain and applies the change, instead of accepting a pre-captured full snapshot, so each mutation observes prior committed state. (Same pattern as the per-chat withMetadataPatchQueue merge in chats.storage.ts.)

Regression notes. Not a regression — main is byte-identical for this file (same writeMeta chain, same comment, same upload/delete RMW). Low severity: self-hosted, predominantly single-user app; manual low-frequency action; narrow window; blast radius one stale meta.json entry.

Template check

  • I DID NOT read this template and provide the requested details.
Originally created by @Romuromylus on GitHub (Jun 18, 2026). Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/2621 Originally assigned to: @Romuromylus on GitHub. ## Summary **Severity: Low — concurrency (lost-update / TOCTOU).** The knowledge-sources upload and delete handlers do a non-atomic read-modify-write on `meta.json`. The write-chain (`metaWriteChain`) serializes the `write()` calls but does **not** re-read under the lock, so two overlapping mutations each persist their own already-stale snapshot — the last writer clobbers the other's change, orphaning a just-uploaded file or resurrecting a just-deleted entry. ## Expected behavior Concurrent upload/delete of knowledge sources should each observe prior committed state. The read-modify-write must happen inside the serialized critical section. ## Actual behavior ```ts // packages/server/src/routes/knowledge-sources.routes.ts // writeMeta (~50-62): serializes only the writeFile() via metaWriteChain — does NOT re-read meta // upload (~139-148): const meta = readMeta(); meta[id] = entry; await writeMeta(meta); // delete (~156-169): const meta = readMeta(); await unlink(filePath); delete meta[id]; await writeMeta(meta); ``` `readMeta()` is synchronous, but the handlers `await` between reading and writing (the delete handler's `await unlink(filePath)` is the clearest yield point). Two concurrent mutations whose `readMeta()` snapshots both predate the other's `writeMeta()` produce a classic lost update: the last queued writer persists its stale snapshot. Result: an orphaned file (just-uploaded entry vanishes from `meta.json` but the file stays on disk) or a resurrected entry (just-deleted entry reappears pointing at an unlinked file; a later `/:id/text` returns empty). The in-code comment claiming the chain prevents metadata being "corrupt or overwrite[n]" overstates the guarantee. ## Steps to reproduce 1. Issue two knowledge-source mutations close together (upload + delete, or two parallel uploads) so their `readMeta()` snapshots both predate the other's `writeMeta()`. 2. Observe one mutation lost: an orphaned on-disk file or a dangling `meta.json` entry. ## Environment - Marinara Engine version: `staging @ 56f700cd` (v2.0.0) - Install type: source - OS + version: n/a — static source audit - Browser or app shell: n/a ## Logs, screenshots, or video Static source audit, cited to file:line above. ## Additional context **Fix sketch.** Perform the read-modify-write inside the serialized critical section: have `writeMeta` accept a mutator (`current => next`) that re-reads `meta` under the chain and applies the change, instead of accepting a pre-captured full snapshot, so each mutation observes prior committed state. (Same pattern as the per-chat `withMetadataPatchQueue` merge in `chats.storage.ts`.) **Regression notes.** Not a regression — `main` is byte-identical for this file (same `writeMeta` chain, same comment, same upload/delete RMW). Low severity: self-hosted, predominantly single-user app; manual low-frequency action; narrow window; blast radius one stale `meta.json` entry. ## Template check - [ ] I DID NOT read this template and provide the requested details.
bsakaguchi 2026-10-21 05:30:52 +07:00
  • closed this issue
  • added the
    server
    bug
    labels
Author
Owner

@Romuromylus commented on GitHub (Jun 18, 2026):

Related to #2617 — both in the knowledge-sources subsystem. #2617 is the per-turn re-extract perf cost; this one is the meta.json read-modify-write race. The mutator-style fix here pairs naturally with the cache work there.

<!-- gh-comment-id:4740873910 --> @Romuromylus commented on GitHub (Jun 18, 2026): Related to #2617 — both in the knowledge-sources subsystem. #2617 is the per-turn re-extract perf cost; this one is the meta.json read-modify-write race. The mutator-style fix here pairs naturally with the cache work there.
Author
Owner

@Romuromylus commented on GitHub (Jun 18, 2026):

Fixed in #2623, merged to staging (e1132e5b). Closing manually since the PR targeted staging rather than the default branch.

<!-- gh-comment-id:4742297881 --> @Romuromylus commented on GitHub (Jun 18, 2026): Fixed in #2623, merged to `staging` (e1132e5b). Closing manually since the PR targeted `staging` rather than the default branch.
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#1293
No description provided.