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.
The existing implementation around
connect()
anddisconnect()
has a bit of an odd control flow. I couldn't find any likely race conditions, but this still cleans up the implementation a bit to be easier to reason about. In particular, some of the things this addresses:connect()
, we were not doing this fordisconnect()
. So if we callconnect()
and then immediately calldisconnect()
, it's possible thatdisconnect()
does nothing because theAbortController
has not yet been set.reconnect()
doesn't actually check if we still need to connect, so adisconnect()
call followed by an error on the isolate could cause anotherconnect()
that is not stopped.onExit
port when spawning the isolate, but there's a separateIsolate.current.addOnExitListener
command in the background isolate. The two trigger different behavior, this has now been cleaned up.Isolate.spawn
call is not awaited, meaning that anotherconnect()
call could begin before the earlier one started setting up the sync client. I don't think this causes any issues, but it's still better to restrict concurrency aroundconnect()
.This also adds a lot of internal assertions to ensure we're not calling the internal connect methods concurrently. I'm still not sure how the problem with duplicate sync streams was caused though, the existing implementation should have guarded against that too.