From 232f83c7af03f36c52b73af2af7e9ea0de2e043c Mon Sep 17 00:00:00 2001 From: Nick Vatamaniuc Date: Thu, 6 May 2021 17:19:09 -0400 Subject: [PATCH] Introduce an option to not trap process exits in worker processes (#171) When worker processes are part of an external connection pool it may make sense to rely on automatic cleanup based on links. Since by default worker processes trap exits, linked proceses would fail to terminate the ibrowse worker and instead it would generate an '{'EXIT', Pid, Reason}' which is handled in handle_info/2 with a warning to stdout. Add a new option to allow users to control worker process link behavior via an ibrowse config parameter. The default stays the same (=true). --- src/ibrowse_http_client.erl | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/ibrowse_http_client.erl b/src/ibrowse_http_client.erl index e59228c..5443fe7 100644 --- a/src/ibrowse_http_client.erl +++ b/src/ibrowse_http_client.erl @@ -133,7 +133,7 @@ send_req(Conn_Pid, Url, Headers, Method, Body, Options, Timeout) -> %% {stop, Reason} %%-------------------------------------------------------------------- init({Lb_Tid, #url{host = Host, port = Port}, {SSLOptions, Is_ssl}}) -> - process_flag(trap_exit, true), + maybe_trap_exits(), State = #state{host = Host, port = Port, ssl_options = SSLOptions, @@ -143,7 +143,7 @@ init({Lb_Tid, #url{host = Host, port = Port}, {SSLOptions, Is_ssl}}) -> put(my_trace_flag, ibrowse_lib:get_trace_status(Host, Port)), {ok, set_inac_timer(State)}; init(Url) when is_list(Url) -> - process_flag(trap_exit, true), + maybe_trap_exits(), case catch ibrowse_lib:parse_url(Url) of #url{protocol = Protocol} = Url_rec -> init({undefined, Url_rec, {[], Protocol == https}}); @@ -151,7 +151,7 @@ init(Url) when is_list(Url) -> {error, invalid_url} end; init({Host, Port}) -> - process_flag(trap_exit, true), + maybe_trap_exits(), State = #state{host = Host, port = Port}, put(ibrowse_trace_token, [Host, $:, integer_to_list(Port)]), @@ -2187,3 +2187,9 @@ get_header_value(Name, Headers, Default_val) -> delayed_stop_timer() -> erlang:send_after(500, self(), delayed_stop). + +maybe_trap_exits() -> + case ibrowse:get_config_value(worker_trap_exits, true) of + true -> process_flag(trap_exit, true); + false -> ok + end.