Skip to content

Commit

Permalink
Merge branch 'ia/ssh/connection-start-error-handling' into maint
Browse files Browse the repository at this point in the history
* ia/ssh/connection-start-error-handling:
  ssh: Correct error handling of ssh connect
  • Loading branch information
IngelaAndin committed Aug 29, 2012
2 parents d685373 + b9a7003 commit e3771ba
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 32 deletions.
31 changes: 1 addition & 30 deletions lib/ssh/src/ssh.erl
Expand Up @@ -91,41 +91,22 @@ do_connect(Host, Port, SocketOptions, SshOptions, Timeout, DisableIpv6) ->
{ok, ConnectionSup} ->
{ok, Manager} =
ssh_connection_sup:connection_manager(ConnectionSup),
MRef = erlang:monitor(process, Manager),
receive
{Manager, is_connected} ->
do_demonitor(MRef, Manager),
{ok, Manager};
%% When the connection fails
%% ssh_connection_sup:connection_manager
%% might return undefined as the connection manager
%% could allready have terminated, so we will not
%% match the Manager in this case
{_, not_connected, {error, econnrefused}} when DisableIpv6 == false ->
do_demonitor(MRef, Manager),
do_connect(Host, Port, proplists:delete(inet6, SocketOptions),
SshOptions, Timeout, true);
{_, not_connected, {error, Reason}} ->
do_demonitor(MRef, Manager),
{error, Reason};
{_, not_connected, Other} ->
do_demonitor(MRef, Manager),
{error, Other};
{'DOWN', MRef, _, Manager, Reason} when is_pid(Manager) ->
error_logger:warning_report([{ssh, connect},
{diagnose,
"Connection was closed before properly set up."},
{host, Host},
{port, Port},
{reason, Reason}]),
receive %% Clear EXIT message from queue
{'EXIT', Manager, _What} ->
{error, channel_closed}
after 0 ->
{error, channel_closed}
end
{error, Other}
after Timeout ->
do_demonitor(MRef, Manager),
ssh_connection_manager:stop(Manager),
{error, timeout}
end
Expand All @@ -134,16 +115,6 @@ do_connect(Host, Port, SocketOptions, SshOptions, Timeout, DisableIpv6) ->
{error, ssh_not_started}
end.

do_demonitor(MRef, Manager) ->
erlang:demonitor(MRef),
receive
{'DOWN', MRef, _, Manager, _} ->
ok
after 0 ->
ok
end.


%%--------------------------------------------------------------------
%% Function: close(ConnectionRef) -> ok
%%
Expand Down
10 changes: 8 additions & 2 deletions lib/ssh/src/ssh_connection_sup.erl
Expand Up @@ -48,8 +48,12 @@ start_manager_child(Sup, Args) ->
supervisor:start_child(Sup, Spec).

connection_manager(SupPid) ->
Children = supervisor:which_children(SupPid),
{ok, ssh_connection_manager(Children)}.
try supervisor:which_children(SupPid) of
Children ->
{ok, ssh_connection_manager(Children)}
catch exit:{noproc,_} ->
{ok, undefined}
end.

%%%=========================================================================
%%% Supervisor callback
Expand Down Expand Up @@ -107,6 +111,8 @@ handler_spec([Role, Socket, Opts]) ->
Type = worker,
{Name, StartFunc, Restart, Shutdown, Type, Modules}.

ssh_connection_manager([]) ->
undefined;
ssh_connection_manager([{_, Child, _, [ssh_connection_manager]} | _]) ->
Child;
ssh_connection_manager([_ | Rest]) ->
Expand Down

0 comments on commit e3771ba

Please sign in to comment.