Fix stale async queue pid after closePosition + add regression test#198
Merged
nialexsan merged 4 commits intonialexsan/close-positionfrom Mar 4, 2026
Merged
Conversation
Contributor
Author
|
Performance note on queue cleanup: Current fix is safe and linear, but Two paths:
Option 2 gives predictable O(1)-style enqueue/close behavior and avoids costly array shifts/scans under load. |
…n-stale-async-queue
…n-stale-async-queue
nialexsan
approved these changes
Mar 4, 2026
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
Fix stale async queue entries after
closePositionsoasyncUpdate()cannot panic when a previously queued position has already been removed.Previous behavior (bug example)
Example sequence:
pid=0.pid=0is queued inpositionsNeedingUpdates.pid=0before async callbacks drain the queue.asyncUpdate().Before this fix:
closePositiondeleted the position fromself.positions.pidcould still remain inpositionsNeedingUpdates.asyncUpdate()could pop that stalepidand attempt to update a non-existent position, causing a panic.Why this fix is needed
This is a production stability issue: a valid user flow (queue then close) could break async processing by introducing stale queue state. Async callbacks must keep progressing even when positions are closed between queueing and processing.
What changed
FlowALPv0.cdc:closePositionnow removes itspidfrompositionsNeedingUpdatesbefore destroying the position.asyncUpdate()now defensively skips stale pids that are no longer present inself.positions.cadence/tests/close_position_async_queue_stale_test.cdccadence/tests/transactions/flow-alp/pool-management/async_update_all.cdcTests
test_closePosition_clearsQueuedAsyncUpdateEntryflow test cadence/tests/close_position_async_queue_stale_test.cdcflow test cadence/tests/async_update_position_test.cdcflow test cadence/tests/close_position_with_queued_deposits_test.cdc