commit 550b4ba

d_m  ·  2026-01-11 01:51:11 +0000 UTC
parent 0680456
Stop polling stdin once it is closed.

The previous code would handle an EOF event on every polling iteration,
even if it had already been handled. This caused programs that read
stdin to run at 100% CPU once it closed (the console vector would be
constantly firing).

This commit tracks whether stdin is still active. If not, we avoid
doing any polling or sending any console messages.
1 files changed,  +6, -4
+6, -4
 1@@ -1283,14 +1283,15 @@ emu_init(void)
 2 static void
 3 emu_run(void)
 4 {
 5-	int has_input, has_eof, has_sent = 1;
 6+	int has_input, has_eof, has_sent, stdin_active = 1;
 7 	char expirations[8], coninp[CONINBUFSIZE];
 8 	struct pollfd fds[3];
 9 	fds[0].fd = emu_x11, fds[0].events = POLLIN;
10 	fds[1].fd = emu_timer, fds[1].events = POLLIN;
11 	fds[2].fd = STDIN_FILENO, fds[2].events = POLLIN | POLLHUP;
12 	while(!dev[0x0f]) {
13-		if(poll(fds, 3, -1) <= 0)
14+		/* only poll stdin while it's still active */
15+		if(poll(fds, stdin_active ? 3 : 2, -1) <= 0)
16 			continue;
17 		while(XPending(emu_dpy))
18 			emu_event();
19@@ -1298,8 +1299,8 @@ emu_run(void)
20 			read(emu_timer, expirations, sizeof expirations);
21 			screen_update();
22 		}
23-		has_input = fds[2].revents & POLLIN;
24-		has_eof = fds[2].revents & POLLHUP;
25+		has_input = stdin_active && (fds[2].revents & POLLIN);
26+		has_eof = stdin_active && (fds[2].revents & POLLHUP);
27 		if(has_input || has_eof) {
28 			int i, n = read(STDIN_FILENO, coninp, CONINBUFSIZE - 1);
29 			if(n > 0) {
30@@ -1309,6 +1310,7 @@ emu_run(void)
31 			} else if(has_sent) {
32 				console_input(0, CONSOLE_END);
33 				has_sent = 0;
34+				stdin_active = 0;
35 			}
36 		}
37 	}