fix: atomic position calculation in pin/channel creation#196
Open
gusye1234 wants to merge 1 commit into
Open
Conversation
…tion Previously, MAX(position) was read and then used in a separate INSERT, allowing concurrent calls to get duplicate positions. Now uses a subquery in the INSERT to atomically calculate the next position. Closes #149
gusye1234
commented
May 28, 2026
gusye1234
left a comment
Contributor
Author
There was a problem hiding this comment.
Review — PR #196: atomic position calculation in pin/channel creation
Good fix for a race condition in the position assignment logic. Moving from read-then-write to a single-statement subquery eliminates the window where concurrent inserts could read the same MAX and produce duplicate positions.
pinAgent: subquery computesMAX(position) + 1within the INSERT VALUES — evaluated atomically before the row is inserted.onConflictDoNothing()still handles duplicate pin attempts.createChannel: same pattern, scoped byworkspaceId.- The
COALESCE(..., -1) + 1default ensures first position is 0, consistent with the prior logic.
The subquery-in-VALUES pattern is valid in both SQLite (D1) and PostgreSQL — the subquery is evaluated before the insert executes, so referencing the same table is safe.
Checklist:
- Functionality: eliminates duplicate position assignments under concurrency
- Edge cases: first-ever pin/channel still gets position 0
- Code quality: actually simpler than before (fewer statements)
- Code simplification: removed the separate SELECT query — net reduction in code
- Tests: CI green
LGTM — ready to merge.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Changes
src/shared/src/db/queries/agent-pin.ts: pinAgent() now uses subquery for positionsrc/shared/src/db/queries/channel.ts: createChannel() now uses subquery for positionTest plan
Closes #149