diff --git a/include/grpcpp/impl/codegen/server_context_impl.h b/include/grpcpp/impl/codegen/server_context_impl.h index 37c03573b74..02049360559 100644 --- a/include/grpcpp/impl/codegen/server_context_impl.h +++ b/include/grpcpp/impl/codegen/server_context_impl.h @@ -303,11 +303,12 @@ class ServerContextBase { /// /// WARNING: This is experimental API and could be changed or removed. ::grpc_impl::ServerUnaryReactor* DefaultReactor() { - Reactor* reactor = reinterpret_cast(&default_reactor_); + // Short-circuit the case where a default reactor was already set up by + // the TestPeer. if (test_unary_ != nullptr) { - return reactor; + return reinterpret_cast(&default_reactor_); } - new (reactor) Reactor; + new (&default_reactor_) Reactor; #ifndef NDEBUG bool old = false; assert(default_reactor_used_.compare_exchange_strong( @@ -315,7 +316,7 @@ class ServerContextBase { #else default_reactor_used_.store(true, std::memory_order_relaxed); #endif - return reactor; + return reinterpret_cast(&default_reactor_); } /// Constructors for use by derived classes