[Libwebsockets] Spotted something suspicious in core-net/wsi-timeout.c

Andy Green andy at warmcat.com
Mon Mar 15 20:25:41 CET 2021



On 3/15/21 7:17 PM, Olivier Langlois wrote:
> In the function lws_set_timout(), there is this block of code:
> 
> |if (secs == LWS_TO_KILL_SYNC) { lwsl_debug("%s: TO_KILL_SYNC %s\n", 
> __func__, lws_wsi_tag(wsi)); lws_context_unlock(pt->context); 
> lws_close_free_wsi(wsi, LWS_CLOSE_STATUS_NOSTATUS, "to sync kill"); 
> return; } if (secs == LWS_TO_KILL_ASYNC) secs = 0;|
> 
> |
> |
> 
> is it me or the second if block is dead code and could be removed?

No... the whole thing is

void
lws_set_timeout(struct lws *wsi, enum pending_timeout reason, int secs)
{
	struct lws_context_per_thread *pt = &wsi->a.context->pt[(int)wsi->tsi];

	lws_context_lock(pt->context, __func__);
	lws_pt_lock(pt, __func__);
	lws_dll2_remove(&wsi->sul_timeout.list);
	lws_pt_unlock(pt);

	if (!secs)
		goto bail;

	if (secs == LWS_TO_KILL_SYNC) {
		lwsl_debug("%s: TO_KILL_SYNC %s\n", __func__, lws_wsi_tag(wsi));
		lws_context_unlock(pt->context);
		lws_close_free_wsi(wsi, LWS_CLOSE_STATUS_NOSTATUS,
				   "to sync kill");
		return;
	}

	if (secs == LWS_TO_KILL_ASYNC)
		secs = 0;

	// assert(!secs || !wsi->mux_stream_immortal);
	if (secs && wsi->mux_stream_immortal)
		lwsl_err("%s: on immortal stream %d %d\n", __func__, reason, secs);

	lws_pt_lock(pt, __func__);
	__lws_set_timeout(wsi, reason, secs);
	lws_pt_unlock(pt);

bail:
	lws_context_unlock(pt->context);
}


... if you came in with secs == 0, it's how you disable the timeout, you 
will never reach the __lws_set_timeout() that uses secs.

If you came in with LWS_TO_KILL_ASYNC, it wants to explicitly set a 
timeout for +0us, so it will close the wsi next time around the event 
loop.  So it's doing what it is supposed to be doing AFAICS.

-Andy


More information about the Libwebsockets mailing list