[Libwebsockets] trouble with external POLL array handling

Andy Green andy at warmcat.com
Mon Jan 14 17:01:05 CET 2013


Hi -

Thanks I'm glad you like the code.

You'll need to subscribe to the ml to post here, it's a bit inconvenient initially but it raises the bar on spam somewhat hopefully.

I think the delete code itself is OK, is the problem that the main loop loses track of which is which somehow after we messed with the array?

I would have thought it should be ok, if we wrongly act like the revent is 0 effectively, and ignore the pollin or pollout was set, next time around it will be set still by the next poll().  Am I missing the point, sorry if so.

-Andy

Edwin van den Oetelaar <oetelaar.automatisering at gmail.com> wrote:

>Summary:
>
>in the handling of "libwebsocket_service_fd(context,  &pollfds[n]))"
>the poll array is changed which lead to trouble, eg. unhandled sockets.
>
>Is anyone else having trouble with this? How to handle it, would using
>epoll() help?
>If you need more info about this, please just ask.
>
>Thanks for your time and effort already,
>Best regards, and I think it is a beautiful piece of code anyways.
>Edwin
>
>
>snip of code from :
>/* libwebsockets-test-server-extpoll - libwebsockets external poll loop
>sample
>/*
>	 * callbacks for managing the external poll() array appear in
>	 * protocol 0 callback
>	 */
>
>	case LWS_CALLBACK_ADD_POLL_FD:
>		pollfds[count_pollfds].fd = (int)(long)user;
>		pollfds[count_pollfds].events = (int)len;
>		pollfds[count_pollfds++].revents = 0;
>		break;
>
>	case LWS_CALLBACK_DEL_POLL_FD:
>		for (n = 0; n < count_pollfds; n++)
>			if (pollfds[n].fd == (int)(long)user)
>				while (n < count_pollfds) {
>					pollfds[n] = pollfds[n + 1]; // << TROUBLE
>					n++;
>				}
>		count_pollfds--; // << TROUBLE
>		break;
>
>	case LWS_CALLBACK_SET_MODE_POLL_FD:
>		for (n = 0; n < count_pollfds; n++)
>			if (pollfds[n].fd == (int)(long)user)
>				pollfds[n].events |= (int)(long)len;
>		break;
>
>	case LWS_CALLBACK_CLEAR_MODE_POLL_FD:
>		for (n = 0; n < count_pollfds; n++)
>			if (pollfds[n].fd == (int)(long)user)
>				pollfds[n].events &= ~(int)(long)len;
>		break;
>
>// later the mainloop is checking
>// but the pollfds[] array changed, leading to fd's not being handled,
>since they moved to a lower index
>
>n = poll(pollfds, count_pollfds, 25);
>		if (n < 0)
>			goto done;
>
>		if (n)
>			for (n = 0; n < count_pollfds; n++)
>				if (pollfds[n].revents)
>					/*
>					* returns immediately if the fd does not
>					* match anything under libwebsockets
>					* control
>					*/
>					if (libwebsocket_service_fd(context,
>								  &pollfds[n]))
>						goto done;
>
>// -- end of code
>_______________________________________________
>Libwebsockets mailing list
>Libwebsockets at ml.libwebsockets.org
>http://ml.libwebsockets.org/mailman/listinfo/libwebsockets
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://libwebsockets.org/pipermail/libwebsockets/attachments/20130115/4aa3a96b/attachment-0001.html>


More information about the Libwebsockets mailing list