Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lws_context_destroy dead loop #3288

Open
yukuanlou opened this issue Dec 3, 2024 · 0 comments
Open

lws_context_destroy dead loop #3288

yukuanlou opened this issue Dec 3, 2024 · 0 comments

Comments

@yukuanlou
Copy link

yukuanlou commented Dec 3, 2024

In lws_context_destroy if the file descriptor is accidentally freed early, then in the loop while (pt->fds_count) wsi_from_fd won't be able to find wsi, and it won't be able to call lws_close_free_wsi, but this loop jumps out of the conditional fds_count--; in the lws_close_free_wsi, so it is stuck in a dead loop. It doesn't have a guaranteed mechanism for jumping out of the loop.

image

bugfix suggestions:

diff --git a/lib/core/context.c b/lib/core/context.c
--- a/lib/core/context.c	(revision c4b1e34a5051390438a13c50f0e57434445ce135)
+++ b/lib/core/context.c	(date 1733475213754)
@@ -1886,8 +1886,7 @@
 			/*
 			 * Close every handle in the fds
 			 */
-
-			while (pt->fds_count) {
+			for (unsigned int i = pt->fds_count; i > 0; i--) {
 				struct lws *wsi = wsi_from_fd(context,
 							      pt->fds[0].fd);
 
@@ -1904,6 +1903,9 @@
 
 					if (pt->pipe_wsi == wsi)
 						pt->pipe_wsi = NULL;
+				} else {
+					/* Prevents a dead loop when fd has already been released */
+					lwsl_err("%s: wsi is NULL! pt %d: fds_count %u \n", __func__, n, pt->fds_count);
 				}
 			}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant