Skip to content

Fixing race in FSTWriteStream #510

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Dec 1, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Firestore/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Unreleased

- [fixed] Addressed race condition during the teardown of idle streams (#490).

# v0.9.3
- [changed] Improved performance loading documents matching a query.
- [changed] Cleanly shut down idle write streams.
Expand Down
20 changes: 14 additions & 6 deletions Firestore/Source/Remote/FSTStream.m
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,9 @@ - (void)tearDown {
- (void)closeWithFinalState:(FSTStreamState)finalState error:(nullable NSError *)error {
FSTAssert(finalState == FSTStreamStateError || error == nil,
@"Can't provide an error when not in an error state.");
FSTAssert(self.delegate,
@"closeWithFinalState should only be called for a started stream that has an active "
@"delegate.");

[self.workerDispatchQueue verifyIsCurrentQueue];
[self cancelIdleCheck];
Expand Down Expand Up @@ -388,9 +391,6 @@ - (void)closeWithFinalState:(FSTStreamState)finalState error:(nullable NSError *
}

// Clear the delegates to avoid any possible bleed through of events from GRPC.
FSTAssert(_delegate,
@"closeWithFinalState should only be called for a started stream that has an active "
@"delegate.");
_delegate = nil;
}

Expand Down Expand Up @@ -515,7 +515,11 @@ - (void)handleStreamMessage:(id)value {
*/
- (void)handleStreamClose:(nullable NSError *)error {
FSTLog(@"%@ %p close: %@", NSStringFromClass([self class]), (__bridge void *)self, error);
FSTAssert([self isStarted], @"Can't handle server close in non-started state.");

if (![self isStarted]) { // The stream could have already been closed by the idle close timer.
FSTLog(@"%@ Ignoring server close for already closed stream.", NSStringFromClass([self class]));
return;
}

// In theory the stream could close cleanly, however, in our current model we never expect this
// to happen because if we stop a stream ourselves, this callback will never be called. To
Expand Down Expand Up @@ -615,7 +619,9 @@ - (void)notifyStreamOpen {
}

- (void)notifyStreamInterruptedWithError:(nullable NSError *)error {
[self.delegate watchStreamWasInterruptedWithError:error];
id<FSTWatchStreamDelegate> delegate = self.delegate;
self.delegate = nil;
[delegate watchStreamWasInterruptedWithError:error];
}

- (void)watchQuery:(FSTQueryData *)query {
Expand Down Expand Up @@ -701,7 +707,9 @@ - (void)notifyStreamOpen {
}

- (void)notifyStreamInterruptedWithError:(nullable NSError *)error {
[self.delegate writeStreamWasInterruptedWithError:error];
id<FSTWriteStreamDelegate> delegate = self.delegate;
self.delegate = nil;
[delegate writeStreamWasInterruptedWithError:error];
}

- (void)tearDown {
Expand Down