From bf01459be5d17a0e0db9b377870acba5085cecd8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A1=D0=B5=D1=80=D0=B3=D0=B5=D0=B9=20=D0=9F=D1=80=D0=BE?= =?UTF-8?q?=D1=85=D0=BE=D1=80=D0=BE=D0=B2?= Date: Sun, 20 Jan 2019 20:55:22 +0100 Subject: [PATCH] Handle some of 'unknown_upstream' errors as expected race-condition --- src/mtp_handler.erl | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/src/mtp_handler.erl b/src/mtp_handler.erl index 3b16b62..133c910 100644 --- a/src/mtp_handler.erl +++ b/src/mtp_handler.erl @@ -129,7 +129,7 @@ handle_cast({proxy_ans, Down, Data}, #state{down = Down} = S) -> handle_cast({close_ext, Down}, #state{down = Down, sock = USock, transport = UTrans} = S) -> lager:debug("asked to close connection by downstream"), ok = UTrans:close(USock), - {stop, normal, S}; + {stop, normal, S#state{down = undefined}}; handle_cast({simple_ack, Down, Confirm}, #state{down = Down} = S) -> lager:info("Simple ack: ~p, ~p", [Down, Confirm]), {noreply, S}; @@ -289,8 +289,24 @@ up_send(Packet, #state{stage = tunnel, down_send(Packet, #state{down = Down} = S) -> %% lager:debug(">Down: ~p", [Packet]), - ok = mtp_down_conn:send(Down, Packet), - {ok, S}. + case mtp_down_conn:send(Down, Packet) of + ok -> + {ok, S}; + {error, unknown_upstream} -> + handle_unknown_upstream(S) + end. + +handle_unknown_upstream(#state{down = Down, sock = USock, transport = UTrans} = S) -> + %% there might be a race-condition between packets from upstream socket and + %% downstream's 'close_ext' message. Most likely because of slow up_send + ok = UTrans:close(USock), + receive + {'$gen_cast', {close_ext, Down}} -> + lager:debug("asked to close connection by downstream"), + throw({stop, normal, S#state{down = undefined}}) + after 0 -> + throw({stop, got_unknown_upstream, S}) + end. %% Internal