[Libwebsockets] How to drop sessions which don't request nothing

Alexandre Erwin Ittner alexandre at ittner.com.br
Thu Jan 30 04:58:36 CET 2014

Hi folks,

Is there any established way to disconnect TCP sessions which connect to 
libwebsocket and sit idle, never starting an HTTP request nor sending any 
data, but still holding up a file descriptor, making poll queue longer,
and, maybe, being kept up by TCP keep-alive?

To get into this situation, fire up a telnet to the address/port of a 
libwebsockets-test-server and do nothing. For comparison, do the same to 
an Apache/nginx webserver -- it will drop the connection after a while, 
freeing up the resources.

Currently, I plan to handle this in my application by adding the file
descriptor to a pending list once it is received in callback
LWS_CALLBACK_FILTER_NETWORK_CONNECTION and removing it once it performs
some request or gets removed by LWS_CALLBACK_DEL_POLL_FD (I'm using an
external poll loop). From time to time, my application will scan the list
and inject a fake POLLERR flag in the "revents" field before passing it
to LWS, so the library can close and release any associated resource.
Since my application handles timeouts after the connection reaches
LWS_CALLBACK_ESTABLISHED, no special procedures are needed after this

I also tried using the function libwebsocket_set_timeout -- setting a
timeout to 10s in LWS_CALLBACK_FILTER_NETWORK_CONNECTION and setting it
misunderstood the mechanism -- it only resulted in a segfault with a
possible stack corruption which prevents GDB from getting to the failure
point inside libwebsocket_service_fd().

My questions:

- Is there a better way to do this with LWS infrastructure? Is this the
  idea behind libwebsocket_set_timeout ?

- If not, is there any interest from the LWS user community in a patch
  to handle this situation inside the library? I can change my approach
  and  write it.

Thanks in advance,

Alexandre Erwin Ittner - alexandre at ittner.com.br
OpenPGP pubkey 0x0041A1FB @ http://pgp.mit.edu

More information about the Libwebsockets mailing list