Skip to content

Commit 5f874a6

Browse files
committed
Revert
1 parent 922615d commit 5f874a6

File tree

4 files changed

+40
-34
lines changed

4 files changed

+40
-34
lines changed

Firestore/core/src/firebase/firestore/auth/firebase_credentials_provider_apple.mm

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -120,26 +120,19 @@
120120
}
121121
};
122122

123-
bool force_refresh = false;
124-
{
125-
std::lock_guard<std::mutex> lock{contents_->mutex};
126-
force_refresh = contents_->force_refresh;
127-
contents_->force_refresh = false;
128-
}
129-
130123
// TODO(wilhuff): Need a better abstraction over a missing auth provider.
131124
if (contents_->auth) {
132-
[contents_->auth getTokenForcingRefresh:force_refresh
125+
[contents_->auth getTokenForcingRefresh:contents_->force_refresh
133126
withCallback:get_token_callback];
134127
} else {
135128
// If there's no Auth provider, call back immediately with a nil
136129
// (unauthenticated) token.
137130
get_token_callback(nil, nil);
138131
}
132+
contents_->force_refresh = false;
139133
}
140134

141135
void FirebaseCredentialsProvider::InvalidateToken() {
142-
std::lock_guard<std::mutex> lock{contents_->mutex};
143136
contents_->force_refresh = true;
144137
}
145138

Firestore/core/src/firebase/firestore/remote/grpc_connection.mm

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -227,11 +227,6 @@ bool HasSpecialConfig(const std::string& host) {
227227
call->FinishAndNotify(Status{FirestoreErrorCode::Unavailable,
228228
"Network connectivity changed"});
229229
}
230-
// The old channel may hang for a long time trying to reestablish
231-
// connection before eventually failing. Note that gRPC Objective-C
232-
// client does the same thing:
233-
// https://github.com/grpc/grpc/blob/master/src/objective-c/GRPCClient/private/GRPCHost.m#L309-L314
234-
grpc_channel_.reset();
235230
});
236231
}
237232

Firestore/core/src/firebase/firestore/remote/grpc_stream.cc

Lines changed: 35 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -175,20 +175,27 @@ void GrpcStream::FinishAndNotify(const Status& status) {
175175
void GrpcStream::Shutdown() {
176176
MaybeUnregister();
177177
if (completions_.empty()) {
178-
// Nothing to cancel -- either the call was already finished, or it has
179-
// never been started.
178+
// Nothing to cancel.
180179
return;
181180
}
182181

183182
// Important: since the stream always has a pending read operation,
184183
// cancellation has to be called, or else the read would hang forever, and
185184
// finish operation will never get completed.
185+
//
186186
// (on the other hand, when an operation fails, cancellation should not be
187187
// called, otherwise the real failure cause will be overwritten by status
188188
// "canceled".)
189189
context_->TryCancel();
190-
FinishCall({});
191-
// Wait until "finish" is off the queue.
190+
191+
// The observer is not interested in this event -- since it initiated the
192+
// finish operation, the observer must know the reason.
193+
GrpcCompletion* completion = NewCompletion(Type::Finish, {});
194+
// TODO(varconst): is issuing a finish operation necessary in this case? We
195+
// don't care about the status, but perhaps it will make the server notice
196+
// client disconnecting sooner?
197+
call_->Finish(completion->status(), completion);
198+
192199
FastFinishCompletionsBlocking();
193200
}
194201

@@ -199,14 +206,6 @@ void GrpcStream::MaybeUnregister() {
199206
}
200207
}
201208

202-
void GrpcStream::FinishCall(const OnSuccess& callback) {
203-
// All completions issued by this call must be taken off the queue before
204-
// finish operation can be enqueued.
205-
FastFinishCompletionsBlocking();
206-
GrpcCompletion* completion = NewCompletion(Type::Finish, callback);
207-
call_->Finish(completion->status(), completion);
208-
}
209-
210209
void GrpcStream::FastFinishCompletionsBlocking() {
211210
// TODO(varconst): reset buffered_writer_? Should not be necessary, because it
212211
// should never be called again after a call to Finish.
@@ -277,10 +276,29 @@ void GrpcStream::OnWrite() {
277276
}
278277

279278
void GrpcStream::OnOperationFailed() {
280-
FinishCall([this](const GrpcCompletion* completion) {
281-
Status status = ConvertStatus(*completion->status());
282-
FinishAndNotify(status);
283-
});
279+
if (is_finishing_) {
280+
// `Finish` itself cannot fail. If another failed operation already
281+
// triggered `Finish`, there's nothing to do.
282+
return;
283+
}
284+
285+
is_finishing_ = true;
286+
287+
if (observer_) {
288+
GrpcCompletion* completion =
289+
NewCompletion(Type::Finish, [this](const GrpcCompletion* completion) {
290+
OnFinishedByServer(*completion->status());
291+
});
292+
call_->Finish(completion->status(), completion);
293+
} else {
294+
// The only reason to finish would be to get the status; if the observer is
295+
// no longer interested, there is no need to do that.
296+
Shutdown();
297+
}
298+
}
299+
300+
void GrpcStream::OnFinishedByServer(const grpc::Status& status) {
301+
FinishAndNotify(ConvertStatus(status));
284302
}
285303

286304
void GrpcStream::RemoveCompletion(const GrpcCompletion* to_remove) {
@@ -297,9 +315,7 @@ GrpcCompletion* GrpcStream::NewCompletion(Type tag,
297315
RemoveCompletion(completion);
298316

299317
if (ok) {
300-
if (on_success) {
301-
on_success(completion);
302-
}
318+
on_success(completion);
303319
} else {
304320
// Use the same error-handling for all operations; all errors are
305321
// unrecoverable.

Firestore/core/src/firebase/firestore/remote/grpc_stream.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,12 +191,12 @@ class GrpcStream : public GrpcCall {
191191
void OnRead(const grpc::ByteBuffer& message);
192192
void OnWrite();
193193
void OnOperationFailed();
194+
void OnFinishedByServer(const grpc::Status& status);
194195
void RemoveCompletion(const GrpcCompletion* to_remove);
195196

196197
using OnSuccess = std::function<void(const GrpcCompletion*)>;
197198
GrpcCompletion* NewCompletion(GrpcCompletion::Type type,
198199
const OnSuccess& callback);
199-
void FinishCall(const OnSuccess& callback);
200200

201201
// Blocks until all the completions issued by this stream come out from the
202202
// gRPC completion queue. Once they do, it is safe to delete this `GrpcStream`
@@ -227,6 +227,8 @@ class GrpcStream : public GrpcCall {
227227
internal::BufferedWriter buffered_writer_;
228228

229229
std::vector<GrpcCompletion*> completions_;
230+
231+
bool is_finishing_ = false;
230232
};
231233

232234
} // namespace remote

0 commit comments

Comments
 (0)