After reviewing RFC 4254 I can see that there is definitely a bug in ssh_connection:close().  Looking at the code I see that close() will cause a <span style="white-space:pre-wrap">SSH_MSG_CHANNEL_CLOSE to be sent, but no state is changed to record that it happened.  When the peer responds with a </span><span style="white-space:pre-wrap">CLOSE, erlang incorrectly responds with another CLOSE.</span><div>
<span style="white-space:pre-wrap"><br></span></div><div><span style="white-space:pre-wrap">In record "channel" there is a field called "sent_close" that appears to be intended for recording a locally initiated close().  I suspect this needs to be set to true in ssh_connection_manager:handle_call({close,...}).</span></div>
<div><span style="white-space:pre-wrap"><br></span></div><div><span style="white-space:pre-wrap">For my application I will work around this by just not calling close().</span></div><div><span style="white-space:pre-wrap"><br>
</span></div><div><span style="white-space:pre-wrap">Dan.<br></span><br><div class="gmail_quote">On Fri, Apr 13, 2012 at 4:28 PM, Daniel Goertzen <span dir="ltr"><<a href="mailto:daniel.goertzen@gmail.com">daniel.goertzen@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">I am having problems using erlang ssh against an OpenSSH server.  I am establishing an ssh connection and then in a freshly spawned process I will do this...<div>
<br></div><div><br></div><div>command(Conn, Cmd) -></div>
<div>   {ok,Chan} = ssh_connection:session_channel(Conn, infinity),</div><div>   success = ssh_connection:exec(Conn, Chan, Cmd, infinity),</div><div>   ... receive data messages until eof...</div><div>   ssh_connection:close(Conn, Chan),</div>

<div>   ok.</div><div><br></div><div><br></div><div><br></div><div>I run about 6 of these commands sequentially (new process for each), and most of the time OpenSSH gets angry with me and drops the whole connection.  The error given is...</div>

<div><br></div><div><barrage of debug messages talking about shutting down channel 1></div><div>channel_by_id: 1: bad id: channel free</div><div>Disconnecting: Received oclose for nonexistent channel 1.</div><div><br>

</div><div><br></div><div><br></div><div>But.... if I add a delay before the close() everything seems to work perfectly:</div><div><br></div><div><div>command(Conn, Cmd) -></div><div>   {ok,Chan} = ssh_connection:session_channel(Conn, infinity),</div>

<div>   success = ssh_connection:exec(Conn, Chan, Cmd, infinity),</div><div>   ... receive data messages until eof...</div><div>   timer:delay(1000),</div><div>   ssh_connection:close(Conn, Chan),</div><div>   ok.</div><br>

</div><div><br></div><div>Dan.</div>
</blockquote></div><br></div>