libssh2 session cleanup without blocking?












0















My app uses libssh2 to communicate over SSH, and generally works fine. One problem I have is when the remote host dies unexpectedly -- the remote host in this case is an embedded device that can lose power at any time, so this isn't uncommon.



When that happens, my app detects that the remote computer has stopped responding to pings, and tears down the local end of the SSH connection like this:



void SSHSession :: CleanupSession()
{
if (_uploadFileChannel)
{
libssh2_channel_free(_uploadFileChannel);
_uploadFileChannel = NULL;
}

if (_sendCommandsChannel)
{
libssh2_channel_free(_sendCommandsChannel);
_sendCommandsChannel = NULL;
}

if (_session)
{
libssh2_session_disconnect(_session, "bye bye");
libssh2_session_free(_session);
_session = NULL;
}
}


Pretty straightforward, but the problem is that the libssh2_channel_free() calls can block for a long time waiting for the remote end to respond to the "I'm going away now" message, which it will never do because it's powered off... but in the meantime, my app is frozen (blocked in the cleanup-routine), which isn't good.



Is there any way (short of hacking libssh2) to avoid this? I'd like to just tear down the local SSH data structures, and never block during this tear-down. (I suppose I could simply leak the SSH session memory, or delegate it to a different thread, but those seem like ugly hacks rather than proper solutions)










share|improve this question





























    0















    My app uses libssh2 to communicate over SSH, and generally works fine. One problem I have is when the remote host dies unexpectedly -- the remote host in this case is an embedded device that can lose power at any time, so this isn't uncommon.



    When that happens, my app detects that the remote computer has stopped responding to pings, and tears down the local end of the SSH connection like this:



    void SSHSession :: CleanupSession()
    {
    if (_uploadFileChannel)
    {
    libssh2_channel_free(_uploadFileChannel);
    _uploadFileChannel = NULL;
    }

    if (_sendCommandsChannel)
    {
    libssh2_channel_free(_sendCommandsChannel);
    _sendCommandsChannel = NULL;
    }

    if (_session)
    {
    libssh2_session_disconnect(_session, "bye bye");
    libssh2_session_free(_session);
    _session = NULL;
    }
    }


    Pretty straightforward, but the problem is that the libssh2_channel_free() calls can block for a long time waiting for the remote end to respond to the "I'm going away now" message, which it will never do because it's powered off... but in the meantime, my app is frozen (blocked in the cleanup-routine), which isn't good.



    Is there any way (short of hacking libssh2) to avoid this? I'd like to just tear down the local SSH data structures, and never block during this tear-down. (I suppose I could simply leak the SSH session memory, or delegate it to a different thread, but those seem like ugly hacks rather than proper solutions)










    share|improve this question



























      0












      0








      0








      My app uses libssh2 to communicate over SSH, and generally works fine. One problem I have is when the remote host dies unexpectedly -- the remote host in this case is an embedded device that can lose power at any time, so this isn't uncommon.



      When that happens, my app detects that the remote computer has stopped responding to pings, and tears down the local end of the SSH connection like this:



      void SSHSession :: CleanupSession()
      {
      if (_uploadFileChannel)
      {
      libssh2_channel_free(_uploadFileChannel);
      _uploadFileChannel = NULL;
      }

      if (_sendCommandsChannel)
      {
      libssh2_channel_free(_sendCommandsChannel);
      _sendCommandsChannel = NULL;
      }

      if (_session)
      {
      libssh2_session_disconnect(_session, "bye bye");
      libssh2_session_free(_session);
      _session = NULL;
      }
      }


      Pretty straightforward, but the problem is that the libssh2_channel_free() calls can block for a long time waiting for the remote end to respond to the "I'm going away now" message, which it will never do because it's powered off... but in the meantime, my app is frozen (blocked in the cleanup-routine), which isn't good.



      Is there any way (short of hacking libssh2) to avoid this? I'd like to just tear down the local SSH data structures, and never block during this tear-down. (I suppose I could simply leak the SSH session memory, or delegate it to a different thread, but those seem like ugly hacks rather than proper solutions)










      share|improve this question
















      My app uses libssh2 to communicate over SSH, and generally works fine. One problem I have is when the remote host dies unexpectedly -- the remote host in this case is an embedded device that can lose power at any time, so this isn't uncommon.



      When that happens, my app detects that the remote computer has stopped responding to pings, and tears down the local end of the SSH connection like this:



      void SSHSession :: CleanupSession()
      {
      if (_uploadFileChannel)
      {
      libssh2_channel_free(_uploadFileChannel);
      _uploadFileChannel = NULL;
      }

      if (_sendCommandsChannel)
      {
      libssh2_channel_free(_sendCommandsChannel);
      _sendCommandsChannel = NULL;
      }

      if (_session)
      {
      libssh2_session_disconnect(_session, "bye bye");
      libssh2_session_free(_session);
      _session = NULL;
      }
      }


      Pretty straightforward, but the problem is that the libssh2_channel_free() calls can block for a long time waiting for the remote end to respond to the "I'm going away now" message, which it will never do because it's powered off... but in the meantime, my app is frozen (blocked in the cleanup-routine), which isn't good.



      Is there any way (short of hacking libssh2) to avoid this? I'd like to just tear down the local SSH data structures, and never block during this tear-down. (I suppose I could simply leak the SSH session memory, or delegate it to a different thread, but those seem like ugly hacks rather than proper solutions)







      c libssh






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Oct 19 '12 at 19:39







      Jeremy Friesner

















      asked Oct 19 '12 at 19:27









      Jeremy FriesnerJeremy Friesner

      39.3k1080161




      39.3k1080161
























          2 Answers
          2






          active

          oldest

          votes


















          0














          I'm not experienced with libssh2, but perhaps we can get different behavior out of libssh2 by using libssh2_session_disconnect_ex and a different disconnect reason: SSH_DISCONNECT_CONNECTION_LOST.



          libssh2_session_disconnect is equivalent to using libssh2_session_disconnect_ex with the reason SSH_DISCONNECT_BY_APPLICATION. If libssh2 knows that the connection is lost, maybe it won't try to talk to the other side.



          http://libssh2.sourceforge.net/doc/#libssh2sessiondisconnectex



          http://libssh2.sourceforge.net/doc/#sshdisconnectcodes






          share|improve this answer































            0














            Set to non-blocking mode and take the control of reading data from the socket to your hand by setting callback function to read data from the soket using libssh2_session_callback_set with LIBSSH2_CALLBACK_RECV for cbtype



            void *libssh2_session_callback_set(LIBSSH2_SESSION *session, int cbtype, void *callback);



            If you can't read data from the socket due to error ENOTCONN that means remote end has closed the socket or connection failed, then return -ENOTCONN in your callback function






            share|improve this answer























              Your Answer






              StackExchange.ifUsing("editor", function () {
              StackExchange.using("externalEditor", function () {
              StackExchange.using("snippets", function () {
              StackExchange.snippets.init();
              });
              });
              }, "code-snippets");

              StackExchange.ready(function() {
              var channelOptions = {
              tags: "".split(" "),
              id: "1"
              };
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function() {
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled) {
              StackExchange.using("snippets", function() {
              createEditor();
              });
              }
              else {
              createEditor();
              }
              });

              function createEditor() {
              StackExchange.prepareEditor({
              heartbeatType: 'answer',
              autoActivateHeartbeat: false,
              convertImagesToLinks: true,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: 10,
              bindNavPrevention: true,
              postfix: "",
              imageUploader: {
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              },
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              });


              }
              });














              draft saved

              draft discarded


















              StackExchange.ready(
              function () {
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f12981125%2flibssh2-session-cleanup-without-blocking%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              2 Answers
              2






              active

              oldest

              votes








              2 Answers
              2






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              0














              I'm not experienced with libssh2, but perhaps we can get different behavior out of libssh2 by using libssh2_session_disconnect_ex and a different disconnect reason: SSH_DISCONNECT_CONNECTION_LOST.



              libssh2_session_disconnect is equivalent to using libssh2_session_disconnect_ex with the reason SSH_DISCONNECT_BY_APPLICATION. If libssh2 knows that the connection is lost, maybe it won't try to talk to the other side.



              http://libssh2.sourceforge.net/doc/#libssh2sessiondisconnectex



              http://libssh2.sourceforge.net/doc/#sshdisconnectcodes






              share|improve this answer




























                0














                I'm not experienced with libssh2, but perhaps we can get different behavior out of libssh2 by using libssh2_session_disconnect_ex and a different disconnect reason: SSH_DISCONNECT_CONNECTION_LOST.



                libssh2_session_disconnect is equivalent to using libssh2_session_disconnect_ex with the reason SSH_DISCONNECT_BY_APPLICATION. If libssh2 knows that the connection is lost, maybe it won't try to talk to the other side.



                http://libssh2.sourceforge.net/doc/#libssh2sessiondisconnectex



                http://libssh2.sourceforge.net/doc/#sshdisconnectcodes






                share|improve this answer


























                  0












                  0








                  0







                  I'm not experienced with libssh2, but perhaps we can get different behavior out of libssh2 by using libssh2_session_disconnect_ex and a different disconnect reason: SSH_DISCONNECT_CONNECTION_LOST.



                  libssh2_session_disconnect is equivalent to using libssh2_session_disconnect_ex with the reason SSH_DISCONNECT_BY_APPLICATION. If libssh2 knows that the connection is lost, maybe it won't try to talk to the other side.



                  http://libssh2.sourceforge.net/doc/#libssh2sessiondisconnectex



                  http://libssh2.sourceforge.net/doc/#sshdisconnectcodes






                  share|improve this answer













                  I'm not experienced with libssh2, but perhaps we can get different behavior out of libssh2 by using libssh2_session_disconnect_ex and a different disconnect reason: SSH_DISCONNECT_CONNECTION_LOST.



                  libssh2_session_disconnect is equivalent to using libssh2_session_disconnect_ex with the reason SSH_DISCONNECT_BY_APPLICATION. If libssh2 knows that the connection is lost, maybe it won't try to talk to the other side.



                  http://libssh2.sourceforge.net/doc/#libssh2sessiondisconnectex



                  http://libssh2.sourceforge.net/doc/#sshdisconnectcodes







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Oct 19 '12 at 20:35









                  PataterPatater

                  1077




                  1077

























                      0














                      Set to non-blocking mode and take the control of reading data from the socket to your hand by setting callback function to read data from the soket using libssh2_session_callback_set with LIBSSH2_CALLBACK_RECV for cbtype



                      void *libssh2_session_callback_set(LIBSSH2_SESSION *session, int cbtype, void *callback);



                      If you can't read data from the socket due to error ENOTCONN that means remote end has closed the socket or connection failed, then return -ENOTCONN in your callback function






                      share|improve this answer




























                        0














                        Set to non-blocking mode and take the control of reading data from the socket to your hand by setting callback function to read data from the soket using libssh2_session_callback_set with LIBSSH2_CALLBACK_RECV for cbtype



                        void *libssh2_session_callback_set(LIBSSH2_SESSION *session, int cbtype, void *callback);



                        If you can't read data from the socket due to error ENOTCONN that means remote end has closed the socket or connection failed, then return -ENOTCONN in your callback function






                        share|improve this answer


























                          0












                          0








                          0







                          Set to non-blocking mode and take the control of reading data from the socket to your hand by setting callback function to read data from the soket using libssh2_session_callback_set with LIBSSH2_CALLBACK_RECV for cbtype



                          void *libssh2_session_callback_set(LIBSSH2_SESSION *session, int cbtype, void *callback);



                          If you can't read data from the socket due to error ENOTCONN that means remote end has closed the socket or connection failed, then return -ENOTCONN in your callback function






                          share|improve this answer













                          Set to non-blocking mode and take the control of reading data from the socket to your hand by setting callback function to read data from the soket using libssh2_session_callback_set with LIBSSH2_CALLBACK_RECV for cbtype



                          void *libssh2_session_callback_set(LIBSSH2_SESSION *session, int cbtype, void *callback);



                          If you can't read data from the socket due to error ENOTCONN that means remote end has closed the socket or connection failed, then return -ENOTCONN in your callback function







                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Nov 20 '13 at 4:27









                          Milinda PereraMilinda Perera

                          161




                          161






























                              draft saved

                              draft discarded




















































                              Thanks for contributing an answer to Stack Overflow!


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid



                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.


                              To learn more, see our tips on writing great answers.




                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function () {
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f12981125%2flibssh2-session-cleanup-without-blocking%23new-answer', 'question_page');
                              }
                              );

                              Post as a guest















                              Required, but never shown





















































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown

































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown







                              Popular posts from this blog

                              Callistus III

                              Plistias Cous

                              Index Sanctorum