Subject: Re: Blocking/non-blocking and session disconnect

Re: Blocking/non-blocking and session disconnect

From: Paulo Caetano <paulo.a.o.caetano_at_gmail.com>
Date: Sun, 27 May 2012 00:18:27 +0100

> While this works, I hesitate to add this kind of busy-looping into any
> examples since it not a good piece of advice. It should rather use
> waitsocket() first if LIBSSH2_ERROR_EAGAIN is returned, to avoid 100% CPU
> usage during the EAGAIN phase.

OK. So, you mean this:

---
    while (libssh2_session_disconnect(session,
        "Normal Shutdown, Thank you for playing") == LIBSSH2_ERROR_EAGAIN)
              waitsocket(sock, session);
    while (libssh2_session_free(session) == LIBSSH2_ERROR_EAGAIN)
              waitsocket(sock, session);
---
BTW, I was following this code, on the same example:
---
 while ((rc = libssh2_session_handshake(session, sock)) ==
           LIBSSH2_ERROR_EAGAIN);
---
I suppose this also needs to call waitsocket(), right?
> Thus I don't think anyone should write
> code that assumes a certain subset of error codes from a specific function
> unless that is explicitly documented to be the only possible outcomes.
I'll add the check for the return code, then. But that leaves me with
a doubt. If I understood correctly session_free(), in session.c, there
seems to be no problem in calling libssh2_session_free() if
libssh2_session_disconnect() returns an error. So, I could just ouput
any libssh2_session_disconnect() error to stderr and call
libssh2_session_free(). But, as you said, this may change in the
future. So, what would be acceptable if libssh2_session_disconnect()
returns an error?
I'm working on a GUI application that will need to create/destroy SSH
sessions as needed, so I'd like to get this one right.
Thanks for your help.
Paulo Caetano
On Sat, May 26, 2012 at 10:02 PM, Daniel Stenberg <daniel_at_haxx.se> wrote:
> On Sat, 26 May 2012, Paulo Caetano wrote:
>
>>   while (libssh2_session_disconnect(session, "Normal Shutdown, Thank
>> you for playing") == LIBSSH2_ERROR_EAGAIN)
>>             ;
>
>
> While this works, I hesitate to add this kind of busy-looping into any
> examples since it not a good piece of advice. It should rather use
> waitsocket() first if LIBSSH2_ERROR_EAGAIN is returned, to avoid 100% CPU
> usage during the EAGAIN phase.
>
>
>> I've added no error cheking. Looking at session_disconnect() in session.c,
>> I was left with the impression that it only returns an error if description
>> (reason for disconnection) length > 256. Otherwise, it always returns either
>> LIBSSH2_ERROR_EAGAIN or 0.
>
>
> Right now that might be true, but all functions that are documented to
> return an error code may at some point return an actual error code. Even if
> the code flow currently only allows for a single specific return code we may
> do changes in the future that can introduce new codes or in some cases it
> might even have done so in the past. Thus I don't think anyone should write
> code that assumes a certain subset of error codes from a specific function
> unless that is explicitly documented to be the only possible outcomes.
>
> --
>
>  / daniel.haxx.se
> _______________________________________________
> libssh2-devel http://cool.haxx.se/cgi-bin/mailman/listinfo/libssh2-devel
_______________________________________________
libssh2-devel http://cool.haxx.se/cgi-bin/mailman/listinfo/libssh2-devel
Received on 2012-05-27