[#504] Guard asyncValidator setting error with _statusChanges stream status#505
Open
LahaLuhem wants to merge 4 commits into
Open
[#504] Guard asyncValidator setting error with _statusChanges stream status#505LahaLuhem wants to merge 4 commits into
_statusChanges stream status#505LahaLuhem wants to merge 4 commits into
Conversation
There was a problem hiding this comment.
Code Review
This pull request adds a check to prevent a crash when _statusChanges is closed during the onDone callback in AbstractControl. However, the review feedback points out that this only addresses the symptom of a larger issue. The root cause is a subscription leak caused by a race condition in the asynchronous _cancelExistingSubscription() method, which gets overwritten with null and orphaned. The reviewer recommends making _cancelExistingSubscription() synchronous and ensuring the _debounceTimer is cancelled in dispose() to properly resolve the leak.
_statusChanges stream status
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.
Connection with issue(s)
Close #504
Solution description
The
StateError: Cannot add new events after calling closefrom #504 is the visible symptom of an orphan-subscription race in_cancelExistingSubscription(). The same race can also cause stale validator results to overwrite newer ones, even withoutdispose().Root cause
_cancelExistingSubscription()wasasyncand called withoutawaitfromupdateValueAndValidity():awaitsuspends — even when_asyncValidationSubscriptionisnull,await nullstill yields via a microtask.updateValueAndValidity(), which synchronously runs_runAsyncValidators()and stores a fresh subscription in_asyncValidationSubscription._asyncValidationSubscription = null, orphaning the just-assigned subscription.dispose()can cancel it. When it eventually completes, itsonDone:_statusChangeshas been closed (the original Use-after-free bug possible for AsyncValidators #504 symptom), orsetErrors(...).addAll(errors).addAll(asyncValidationErrors), overwriting whatever the latest validator settled on.Fix
_cancelExistingSubscriptionsynchronous.StreamSubscription.cancel()stops event delivery synchronously; the returned future only signals cleanup completion, so awaiting it serves no purpose here and is what introduces the suspension point._debounceTimerindispose(). Without this, disposing a control while a debounce window is open leaks the timer (and the closure holdingthis).With both changes the symptom-level
if (_statusChanges.isClosed) return;guard is unnecessary — the orphan that produced the badonDonecan no longer be created.Tests
test/src/validators/async_validator_test.dart:Dispose during in-flight async validation— six tests covering dispose at various points during validation forFormControl/FormGroup, single and multiple validators, including the debounce window. Also assertsdispose()cancels the pending debounce timer viaFakeAsync.pendingTimers.Async validator subscription cancellation— setsvalue = 'bad'(slow validator, returns an error), thenvalue = 'good'(fast, returns no error); asserts the stale'bad'error does not survive into the'good'state. Fails on the original code because the orphaned'bad'subscription is never cancelled and re-injects its error after'good'has already settled.Each test was verified to fail when the corresponding part of the fix is reverted.
To Do