Skip to content

Commit b016729

Browse files
authored
Revert "Revert "Adding grpc_init/grpc_shutdown to subchannel ctor/dtor to solve bugs related to async destruction of subchannels (#26965)" (#27004)" (#27191)
This reverts commit 5f00f9c.
1 parent 7653464 commit b016729

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

src/core/ext/filters/client_channel/subchannel.cc

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -657,6 +657,15 @@ Subchannel::Subchannel(SubchannelKey key,
657657
key_(std::move(key)),
658658
connector_(std::move(connector)),
659659
backoff_(ParseArgsForBackoffValues(args, &min_connect_timeout_ms_)) {
660+
// A grpc_init is added here to ensure that grpc_shutdown does not happen
661+
// until the subchannel is destroyed. Subchannels can persist longer than
662+
// channels because they maybe reused/shared among multiple channels. As a
663+
// result the subchannel destruction happens asynchronously to channel
664+
// destruction. If the last channel destruction triggers a grpc_shutdown
665+
// before the last subchannel destruction, then there maybe race conditions
666+
// triggering segmentation faults. To prevent this issue, we call a grpc_init
667+
// here and a grpc_shutdown in the subchannel destructor.
668+
grpc_init();
660669
GRPC_STATS_INC_CLIENT_SUBCHANNELS_CREATED();
661670
pollset_set_ = grpc_pollset_set_create();
662671
grpc_resolved_address* addr =
@@ -707,6 +716,8 @@ Subchannel::~Subchannel() {
707716
grpc_channel_args_destroy(args_);
708717
connector_.reset();
709718
grpc_pollset_set_destroy(pollset_set_);
719+
// grpc_shutdown is called here because grpc_init is called in the ctor.
720+
grpc_shutdown();
710721
}
711722

712723
RefCountedPtr<Subchannel> Subchannel::Create(

0 commit comments

Comments
 (0)