[GH-ISSUE #3002] [Issue]: Retry illustrator attachment uses a non-atomic read-modify-write instead of the atomic append helpers #1576

Closed
opened 2026-10-21 05:39:32 +07:00 by bsakaguchi · 1 comment
Owner

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

Originally assigned to: @SpicyMarinara on GitHub.

What's wrong
The retry illustrator handler appends a generated image to the swipe (and conditionally the message) by manually reading the swipe/message extra, pushing onto the attachments array, and writing it back. The read happens OUTSIDE the per-message/per-swipe patch queue while only the write re-enters the queue. The canonical generate route uses dedicated atomic append helpers that perform the whole read-push-write inside the queue. Because the retry path writes back a full-array overwrite captured from a stale read, any attachment appended by a concurrent writer between the read and the queued write is silently clobbered (lost update). In a strictly sequential single retry this does not trigger; impact is limited to concurrent/overlapping attachment writes on the same message.

How to reproduce (try it yourself)
Difficult to trigger deterministically: requires a second attachment write to the same retryMessageId/retrySwipeIndex to interleave between the getSwipes read and the updateSwipeExtra write. Under normal single-retry sequential flow it does not reproduce.

You'll see: under a concurrent second attachment write, one append is lost (overwritten by the stale-array write).
Expected: both appends survive, as they do on the main generate route via the atomic helpers.

Where it is
packages/server/src/routes/generate/retry-agents-route.ts:2811-2828 (illustrator attachment persistence). Atomic helpers: chats.storage.ts:964-977 (appendMessageAttachment) and :1205-1218 (appendSwipeAttachment). Canonical usage: generate.routes.ts:9049/9055.

What's happening
Hand-rolled read-modify-write of the attachments array outside the storage layer's atomic append helpers. The read is not serialized with the write under the patch queue, so concurrent appends race and the full-array overwrite clobbers the other writer's entry.

Suggested fix
Replace the manual read-push-write block (2811-2828) with the atomic helpers, mirroring generate.routes.ts:9049/9055. Always append to the swipe; append to the message only when this swipe is still active (the message helper has no internal active-swipe guard, so keep the getMessage check):

  await chatsDb.appendSwipeAttachment(retryMessageId, retrySwipeIndex, attachment);
  const msgRow = await chatsDb.getMessage(retryMessageId);
  if (msgRow && (msgRow.activeSwipeIndex ?? 0) === retrySwipeIndex) {
    await chatsDb.appendMessageAttachment(retryMessageId, attachment);
  }

This routes both appends through the patch queue, making them atomic against concurrent extra writes while preserving the existing swipe-exists and active-swipe semantics. The active-swipe guard MUST be kept: appendMessageAttachment appends unconditionally, so dropping the guard would leak a non-active-swipe illustration onto the live message row. appendSwipeAttachment internally no-ops when the swipe index doesn't exist, equivalent to the current if (swipeRow) guard.


AI-investigated against v2.0.6; cited lines read and confirmed. Please verify before fixing.

Originally created by @kolacheee on GitHub (Jun 29, 2026). Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/3002 Originally assigned to: @SpicyMarinara on GitHub. **What's wrong** The retry illustrator handler appends a generated image to the swipe (and conditionally the message) by manually reading the swipe/message extra, pushing onto the attachments array, and writing it back. The read happens OUTSIDE the per-message/per-swipe patch queue while only the write re-enters the queue. The canonical generate route uses dedicated atomic append helpers that perform the whole read-push-write inside the queue. Because the retry path writes back a full-array overwrite captured from a stale read, any attachment appended by a concurrent writer between the read and the queued write is silently clobbered (lost update). In a strictly sequential single retry this does not trigger; impact is limited to concurrent/overlapping attachment writes on the same message. **How to reproduce (try it yourself)** Difficult to trigger deterministically: requires a second attachment write to the same retryMessageId/retrySwipeIndex to interleave between the getSwipes read and the updateSwipeExtra write. Under normal single-retry sequential flow it does not reproduce. You'll see: under a concurrent second attachment write, one append is lost (overwritten by the stale-array write). Expected: both appends survive, as they do on the main generate route via the atomic helpers. **Where it is** `packages/server/src/routes/generate/retry-agents-route.ts:2811-2828` (illustrator attachment persistence). Atomic helpers: `chats.storage.ts:964-977` (`appendMessageAttachment`) and `:1205-1218` (`appendSwipeAttachment`). Canonical usage: `generate.routes.ts:9049/9055`. **What's happening** Hand-rolled read-modify-write of the attachments array outside the storage layer's atomic append helpers. The read is not serialized with the write under the patch queue, so concurrent appends race and the full-array overwrite clobbers the other writer's entry. **Suggested fix** Replace the manual read-push-write block (2811-2828) with the atomic helpers, mirroring generate.routes.ts:9049/9055. Always append to the swipe; append to the message only when this swipe is still active (the message helper has no internal active-swipe guard, so keep the getMessage check): ```ts await chatsDb.appendSwipeAttachment(retryMessageId, retrySwipeIndex, attachment); const msgRow = await chatsDb.getMessage(retryMessageId); if (msgRow && (msgRow.activeSwipeIndex ?? 0) === retrySwipeIndex) { await chatsDb.appendMessageAttachment(retryMessageId, attachment); } ``` This routes both appends through the patch queue, making them atomic against concurrent extra writes while preserving the existing swipe-exists and active-swipe semantics. The active-swipe guard MUST be kept: `appendMessageAttachment` appends unconditionally, so dropping the guard would leak a non-active-swipe illustration onto the live message row. `appendSwipeAttachment` internally no-ops when the swipe index doesn't exist, equivalent to the current `if (swipeRow)` guard. --- *AI-investigated against v2.0.6; cited lines read and confirmed. Please verify before fixing.*
bsakaguchi 2026-10-21 05:39:32 +07:00
Author
Owner

@SpicyMarinara commented on GitHub (Jun 29, 2026):

Fixed in merged staging PR #3004.

<!-- gh-comment-id:4832284292 --> @SpicyMarinara commented on GitHub (Jun 29, 2026): Fixed in merged staging PR #3004.
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#1576
No description provided.