mirror of
https://github.com/Pasta-Devs/Marinara-Engine.git
synced 2026-12-03 19:00:50 +07:00
[GH-ISSUE #2621] [Issue]: Knowledge-sources meta.json lost-update / TOCTOU — write serialization does not protect the read-modify-write #1293
Labels
No labels
android
bug
bugfix
ci
client
code freeze pause
container
dependencies
docs
documentation
duplicate
enhancement
feature
feature
feature
feature-creep
fixed-in-refactor
fixed-in-staging
good first issue
help wanted
in-pr
in-pr
needs-codeowner-decision
normal-scope
old-codebase
old-codebase
pull-request
qol
question
refactor
regression
revisit later
roadmap
scope-risk
scope-risk
server
server
shared
soup
upstream
windows
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
Pasta-Devs/Marinara-Engine#1293
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
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 thewrite()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
readMeta()is synchronous, but the handlersawaitbetween reading and writing (the delete handler'sawait unlink(filePath)is the clearest yield point). Two concurrent mutations whosereadMeta()snapshots both predate the other'swriteMeta()produce a classic lost update: the last queued writer persists its stale snapshot. Result: an orphaned file (just-uploaded entry vanishes frommeta.jsonbut the file stays on disk) or a resurrected entry (just-deleted entry reappears pointing at an unlinked file; a later/:id/textreturns empty). The in-code comment claiming the chain prevents metadata being "corrupt or overwrite[n]" overstates the guarantee.Steps to reproduce
readMeta()snapshots both predate the other'swriteMeta().meta.jsonentry.Environment
staging @ 56f700cd(v2.0.0)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
writeMetaaccept a mutator (current => next) that re-readsmetaunder 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-chatwithMetadataPatchQueuemerge inchats.storage.ts.)Regression notes. Not a regression —
mainis byte-identical for this file (samewriteMetachain, same comment, same upload/delete RMW). Low severity: self-hosted, predominantly single-user app; manual low-frequency action; narrow window; blast radius one stalemeta.jsonentry.Template check
@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.
@Romuromylus commented on GitHub (Jun 18, 2026):
Fixed in #2623, merged to
staging(e1132e5b). Closing manually since the PR targetedstagingrather than the default branch.