|  | @@ -589,8 +589,9 @@ CallbackTestServiceImpl::RequestStream() {
 | 
	
		
			
				|  |  |     public:
 | 
	
		
			
				|  |  |      Reactor() {}
 | 
	
		
			
				|  |  |      void OnStarted(ServerContext* context, EchoResponse* response) override {
 | 
	
		
			
				|  |  | -      ctx_ = context;
 | 
	
		
			
				|  |  | -      response_ = response;
 | 
	
		
			
				|  |  | +      // Assign ctx_ and response_ as late as possible to increase likelihood of
 | 
	
		
			
				|  |  | +      // catching any races
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  |        // If 'server_try_cancel' is set in the metadata, the RPC is cancelled by
 | 
	
		
			
				|  |  |        // the server by calling ServerContext::TryCancel() depending on the
 | 
	
		
			
				|  |  |        // value:
 | 
	
	
		
			
				|  | @@ -602,22 +603,26 @@ CallbackTestServiceImpl::RequestStream() {
 | 
	
		
			
				|  |  |        server_try_cancel_ = GetIntValueFromMetadata(
 | 
	
		
			
				|  |  |            kServerTryCancelRequest, context->client_metadata(), DO_NOT_CANCEL);
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -      response_->set_message("");
 | 
	
		
			
				|  |  | +      response->set_message("");
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |        if (server_try_cancel_ == CANCEL_BEFORE_PROCESSING) {
 | 
	
		
			
				|  |  | -        ServerTryCancelNonblocking(ctx_);
 | 
	
		
			
				|  |  | -        return;
 | 
	
		
			
				|  |  | -      }
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -      if (server_try_cancel_ == CANCEL_DURING_PROCESSING) {
 | 
	
		
			
				|  |  | -        ctx_->TryCancel();
 | 
	
		
			
				|  |  | -        // Don't wait for it here
 | 
	
		
			
				|  |  | +        ServerTryCancelNonblocking(context);
 | 
	
		
			
				|  |  | +        ctx_ = context;
 | 
	
		
			
				|  |  | +      } else {
 | 
	
		
			
				|  |  | +        if (server_try_cancel_ == CANCEL_DURING_PROCESSING) {
 | 
	
		
			
				|  |  | +          context->TryCancel();
 | 
	
		
			
				|  |  | +          // Don't wait for it here
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +        ctx_ = context;
 | 
	
		
			
				|  |  | +        response_ = response;
 | 
	
		
			
				|  |  | +        StartRead(&request_);
 | 
	
		
			
				|  |  |        }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -      StartRead(&request_);
 | 
	
		
			
				|  |  | +      on_started_done_ = true;
 | 
	
		
			
				|  |  |      }
 | 
	
		
			
				|  |  |      void OnDone() override { delete this; }
 | 
	
		
			
				|  |  |      void OnCancel() override {
 | 
	
		
			
				|  |  | +      EXPECT_TRUE(on_started_done_);
 | 
	
		
			
				|  |  |        EXPECT_TRUE(ctx_->IsCancelled());
 | 
	
		
			
				|  |  |        FinishOnce(Status::CANCELLED);
 | 
	
		
			
				|  |  |      }
 | 
	
	
		
			
				|  | @@ -657,6 +662,7 @@ CallbackTestServiceImpl::RequestStream() {
 | 
	
		
			
				|  |  |      int server_try_cancel_;
 | 
	
		
			
				|  |  |      std::mutex finish_mu_;
 | 
	
		
			
				|  |  |      bool finished_{false};
 | 
	
		
			
				|  |  | +    bool on_started_done_{false};
 | 
	
		
			
				|  |  |    };
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |    return new Reactor;
 | 
	
	
		
			
				|  | @@ -673,8 +679,9 @@ CallbackTestServiceImpl::ResponseStream() {
 | 
	
		
			
				|  |  |      Reactor() {}
 | 
	
		
			
				|  |  |      void OnStarted(ServerContext* context,
 | 
	
		
			
				|  |  |                     const EchoRequest* request) override {
 | 
	
		
			
				|  |  | -      ctx_ = context;
 | 
	
		
			
				|  |  | -      request_ = request;
 | 
	
		
			
				|  |  | +      // Assign ctx_ and request_ as late as possible to increase likelihood of
 | 
	
		
			
				|  |  | +      // catching any races
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  |        // If 'server_try_cancel' is set in the metadata, the RPC is cancelled by
 | 
	
		
			
				|  |  |        // the server by calling ServerContext::TryCancel() depending on the
 | 
	
		
			
				|  |  |        // value:
 | 
	
	
		
			
				|  | @@ -691,19 +698,23 @@ CallbackTestServiceImpl::ResponseStream() {
 | 
	
		
			
				|  |  |            kServerResponseStreamsToSend, context->client_metadata(),
 | 
	
		
			
				|  |  |            kServerDefaultResponseStreamsToSend);
 | 
	
		
			
				|  |  |        if (server_try_cancel_ == CANCEL_BEFORE_PROCESSING) {
 | 
	
		
			
				|  |  | -        ServerTryCancelNonblocking(ctx_);
 | 
	
		
			
				|  |  | -        return;
 | 
	
		
			
				|  |  | -      }
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -      if (server_try_cancel_ == CANCEL_DURING_PROCESSING) {
 | 
	
		
			
				|  |  | -        ctx_->TryCancel();
 | 
	
		
			
				|  |  | -      }
 | 
	
		
			
				|  |  | -      if (num_msgs_sent_ < server_responses_to_send_) {
 | 
	
		
			
				|  |  | -        NextWrite();
 | 
	
		
			
				|  |  | +        ServerTryCancelNonblocking(context);
 | 
	
		
			
				|  |  | +        ctx_ = context;
 | 
	
		
			
				|  |  | +      } else {
 | 
	
		
			
				|  |  | +        if (server_try_cancel_ == CANCEL_DURING_PROCESSING) {
 | 
	
		
			
				|  |  | +          context->TryCancel();
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +        ctx_ = context;
 | 
	
		
			
				|  |  | +        request_ = request;
 | 
	
		
			
				|  |  | +        if (num_msgs_sent_ < server_responses_to_send_) {
 | 
	
		
			
				|  |  | +          NextWrite();
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  |        }
 | 
	
		
			
				|  |  | +      on_started_done_ = true;
 | 
	
		
			
				|  |  |      }
 | 
	
		
			
				|  |  |      void OnDone() override { delete this; }
 | 
	
		
			
				|  |  |      void OnCancel() override {
 | 
	
		
			
				|  |  | +      EXPECT_TRUE(on_started_done_);
 | 
	
		
			
				|  |  |        EXPECT_TRUE(ctx_->IsCancelled());
 | 
	
		
			
				|  |  |        FinishOnce(Status::CANCELLED);
 | 
	
		
			
				|  |  |      }
 | 
	
	
		
			
				|  | @@ -753,6 +764,7 @@ CallbackTestServiceImpl::ResponseStream() {
 | 
	
		
			
				|  |  |      int server_responses_to_send_;
 | 
	
		
			
				|  |  |      std::mutex finish_mu_;
 | 
	
		
			
				|  |  |      bool finished_{false};
 | 
	
		
			
				|  |  | +    bool on_started_done_{false};
 | 
	
		
			
				|  |  |    };
 | 
	
		
			
				|  |  |    return new Reactor;
 | 
	
		
			
				|  |  |  }
 | 
	
	
		
			
				|  | @@ -764,7 +776,9 @@ CallbackTestServiceImpl::BidiStream() {
 | 
	
		
			
				|  |  |     public:
 | 
	
		
			
				|  |  |      Reactor() {}
 | 
	
		
			
				|  |  |      void OnStarted(ServerContext* context) override {
 | 
	
		
			
				|  |  | -      ctx_ = context;
 | 
	
		
			
				|  |  | +      // Assign ctx_ as late as possible to increase likelihood of catching any
 | 
	
		
			
				|  |  | +      // races
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  |        // If 'server_try_cancel' is set in the metadata, the RPC is cancelled by
 | 
	
		
			
				|  |  |        // the server by calling ServerContext::TryCancel() depending on the
 | 
	
		
			
				|  |  |        // value:
 | 
	
	
		
			
				|  | @@ -778,18 +792,20 @@ CallbackTestServiceImpl::BidiStream() {
 | 
	
		
			
				|  |  |        server_write_last_ = GetIntValueFromMetadata(
 | 
	
		
			
				|  |  |            kServerFinishAfterNReads, context->client_metadata(), 0);
 | 
	
		
			
				|  |  |        if (server_try_cancel_ == CANCEL_BEFORE_PROCESSING) {
 | 
	
		
			
				|  |  | -        ServerTryCancelNonblocking(ctx_);
 | 
	
		
			
				|  |  | -        return;
 | 
	
		
			
				|  |  | -      }
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -      if (server_try_cancel_ == CANCEL_DURING_PROCESSING) {
 | 
	
		
			
				|  |  | -        ctx_->TryCancel();
 | 
	
		
			
				|  |  | +        ServerTryCancelNonblocking(context);
 | 
	
		
			
				|  |  | +        ctx_ = context;
 | 
	
		
			
				|  |  | +      } else {
 | 
	
		
			
				|  |  | +        if (server_try_cancel_ == CANCEL_DURING_PROCESSING) {
 | 
	
		
			
				|  |  | +          context->TryCancel();
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +        ctx_ = context;
 | 
	
		
			
				|  |  | +        StartRead(&request_);
 | 
	
		
			
				|  |  |        }
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -      StartRead(&request_);
 | 
	
		
			
				|  |  | +      on_started_done_ = true;
 | 
	
		
			
				|  |  |      }
 | 
	
		
			
				|  |  |      void OnDone() override { delete this; }
 | 
	
		
			
				|  |  |      void OnCancel() override {
 | 
	
		
			
				|  |  | +      EXPECT_TRUE(on_started_done_);
 | 
	
		
			
				|  |  |        EXPECT_TRUE(ctx_->IsCancelled());
 | 
	
		
			
				|  |  |        FinishOnce(Status::CANCELLED);
 | 
	
		
			
				|  |  |      }
 | 
	
	
		
			
				|  | @@ -839,6 +855,7 @@ CallbackTestServiceImpl::BidiStream() {
 | 
	
		
			
				|  |  |      int server_write_last_;
 | 
	
		
			
				|  |  |      std::mutex finish_mu_;
 | 
	
		
			
				|  |  |      bool finished_{false};
 | 
	
		
			
				|  |  | +    bool on_started_done_{false};
 | 
	
		
			
				|  |  |    };
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |    return new Reactor;
 |