@@ -71,7 +71,7 @@ init(Args) ->
7171 lists :map (fun ({Scheme , Host , Port }) -> {Scheme , Host , Port , ssl_options (Scheme , SslOptions )}
7272 end , Endpoints ),
7373 Channel = iolist_to_binary ([PoolName , " :" , integer_to_binary (WorkerId )]),
74- {ok , _ } = grpcbox_channel_sup : start_child (Channel , Channels , Options ),
74+ {ok , _ } = start_channel (Channel , Channels , Options ),
7575 logger :debug (" [GreptimeDB] genserver has started (~s )~n " , [Channel ]),
7676 {ok , # state {channel = Channel , hints = Hints , requests = #{ pending => queue :new (), pending_count => 0 }}}.
7777
@@ -134,6 +134,28 @@ terminate(Reason, #state{channel = Channel} = State) ->
134134% %%===================================================================
135135% %% Helper functions
136136% %%===================================================================
137+ start_channel (Channel , Channels , Options ) ->
138+ case grpcbox_channel_sup :start_child (Channel , Channels , Options ) of
139+ {error , {already_started , StaleChannel }} ->
140+ % % ecpool may force-kill a worker without invoking terminate/2, leaving its
141+ % % grpcbox channel alive under the global supervisor. Remove that orphan
142+ % % before reusing the deterministic channel name.
143+ logger :warning (
144+ " [GreptimeDB] removing stale grpc channel ~s (~p )" ,
145+ [Channel , StaleChannel ]),
146+ ok = stop_stale_channel (Channel ),
147+ grpcbox_channel_sup :start_child (Channel , Channels , Options );
148+ Result ->
149+ Result
150+ end .
151+
152+ stop_stale_channel (Channel ) ->
153+ try grpcbox_channel :stop (Channel )
154+ catch
155+ exit :noproc -> ok ;
156+ exit :{noproc , _ } -> ok
157+ end .
158+
137159ssl_options (https , []) ->
138160 % % https://www.erlang.org/doc/man/ssl#type-client_option
139161 [
0 commit comments