fix: auto-submit silently drops a change made during a follow-up submit - #102
Merged
Conversation
When a submit completes with pendingChanges queued and no debounce is configured, the follow-up get.set(submitAtom) synchronously re-enters the submitAtom subscription (batch commit notifies listeners in place). The re-entrant call sets wasSubmitting = true, but the outer callback then ran its trailing wasSubmitting = isSubmitting with the stale false, clobbering it. The next completion failed the wasSubmitting guard and the queued change was silently dropped. Assign wasSubmitting before triggering the follow-up submit.
Merged
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
High-severity lost-update in
autoSubmitAtom(onChange + autoSubmit, no debounce), found in a deep review with executed Red→Green validation.When a submit completes with
pendingChangesqueued, the follow-upget.set(submitAtom)runs synchronously and the batch commit re-enters thesubmitAtomsubscription in place. The re-entrant invocation correctly setswasSubmitting = true, but control then returns to the outer callback whose trailingwasSubmitting = isSubmittingstill holds the stalefalse— clobbering it. From then on the tracker believes no submit is in flight, so when the follow-up submit completes, thewasSubmitting && !isSubmittingguard fails and any change queued during it is silently never submitted (stale server state).Interleaving: submit A in flight → edit "c" queued → A completes → follow-up B("c") fires and corrupts the flag → edit "d" queued during B → B completes → guard false → "d" never submits.
Re-entry mechanics verified in the installed effect source (batch write/refresh: Atom.ts:1244-1253; synchronous listener notify in commit: AtomRegistry.ts:1069-1073).
Fix
Assign
wasSubmittingbefore triggering the follow-up submit, so the re-entrantwaiting=trueassignment is the last write.Tests
New regression test drives the exact interleaving with gated
Effect.callbacksubmits: Red on unmodified code (expected [ 'b', 'c' ] to deeply equal [ 'b', 'c', 'd' ]), Green after. Full suite 302 passed, types + lint clean. Independently re-validated (Red re-confirmed on clean main before applying the fix).