From: Denis Osvald Date: Mon, 2 Jan 2017 16:33:26 +0000 (+0100) Subject: server: check listen(2) return value X-Git-Tag: upstream/2.3.0~278 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=9b129c137ae4cb1232076ff2e3e43f6fdd2989a1;p=platform%2Fupstream%2Flibwebsockets.git server: check listen(2) return value The `listen` call can fail with EADDRINUSE after bind() succeeds, for example because another process called listen on that port in the meantime, or under some circumstances with IPv6-mapped-IPv4. This was causing EINVAL on accept, with an infinite loop in case of libuv. A reproducible example was to run nc -l -p 5555 ( OpenBSD netcat (Debian patchlevel 1)) before starting test-server Signed-off-by: Denis Osvald --- diff --git a/lib/server.c b/lib/server.c index 8f6652ca..02b04afb 100644 --- a/lib/server.c +++ b/lib/server.c @@ -164,7 +164,14 @@ lws_context_init_server(struct lws_context_creation_info *info, vhost->lserv_wsi = wsi; #if LWS_POSIX - listen(wsi->sock, LWS_SOMAXCONN); + n = listen(wsi->sock, LWS_SOMAXCONN); + if (n < 0) { + lwsl_err("listen failed with error %d\n", LWS_ERRNO); + vhost->lserv_wsi = NULL; + vhost->context->count_wsi_allocated--; + remove_wsi_socket_from_fds(wsi); + goto bail; + } } /* for each thread able to independently listen */ #else #if defined(LWS_WITH_ESP8266)