Subject: Re: [patch] use poll when available on blocking API

Re: [patch] use poll when available on blocking API

From: Simon Josefsson <simon_at_josefsson.org>
Date: Tue, 23 Mar 2010 19:12:55 +0100

Paul Querna <pquerna_at_apache.org> writes:

> Hello,
>
> We use libssh2 as part of Reconnoiter:
> <https://labs.omniti.com/trac/reconnoiter>
>
> Recently, we kept seeing libssh2 crashing once we passed a certain
> level of concurrency inside noitd, and eventually we traced it down to
> libssh2 using select, with sockets > FD_SETSIZE.
>
> The attached patch uses poll inside _libssh2_wait_socket, and fixes
> the issue for us.
>
> Ideally Reconnoiter will switch to the async API methods, but fixing
> this was quicker than rewriting the noitd module.

Thanks, I think the patch looks good. Objections from anyone else?

/Simon

> Thanks,
>
> Paul
>
> --- session.c.original 2010-03-22 18:21:33.000000000 -0700
> +++ session.c 2010-03-22 18:20:53.000000000 -0700
> @@ -518,11 +518,28 @@
> */
> int _libssh2_wait_socket(LIBSSH2_SESSION *session)
> {
> + int rc;
> + int dir;
> +#if HAVE_POLL
> + struct pollfd sockets[1];
> + sockets[0].fd = session->socket_fd;
> + sockets[0].events = 0;
> + sockets[0].revents = 0;
> +
> + /* now make sure we wait in the correct direction */
> + dir = libssh2_session_block_directions(session);
> +
> + if(dir & LIBSSH2_SESSION_BLOCK_INBOUND)
> + sockets[0].events |= POLLIN;
> +
> + if(dir & LIBSSH2_SESSION_BLOCK_OUTBOUND)
> + sockets[0].events |= POLLOUT;
> +
> + rc = poll(sockets, 1, -1);
> +#else
> fd_set fd;
> fd_set *writefd = NULL;
> fd_set *readfd = NULL;
> - int dir;
> - int rc;
>
> FD_ZERO(&fd);
> FD_SET(session->socket_fd, &fd);
> @@ -539,6 +556,7 @@
> /* Note that this COULD be made to use a timeout that perhaps could be
> customizable by the app or something... */
> rc = select(session->socket_fd + 1, readfd, writefd, NULL, NULL);
> +#endif
>
> if(rc <= 0) {
> /* timeout (or error), bail out with a timeout error */
> _______________________________________________
> 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 2010-03-23