Re: [Chicken-hackers] [PATCH] Avoid spin loop in socket_read() when debug client disconnects

2019-03-14 Thread felix . winkelmann
> Hi all,
> 
> Here's a tiny patch that makes the debug server end the program when a
> client disconnects unexpectedly, i.e. without first sending a terminate
> signal. Currently, the program will instead go into an infinite loop and
> burn 100% of a CPU until it's killed.
> 

Pushed. Thanks, Evan.


felix


___
Chicken-hackers mailing list
Chicken-hackers@nongnu.org
https://lists.nongnu.org/mailman/listinfo/chicken-hackers


[Chicken-hackers] [PATCH] Avoid spin loop in socket_read() when debug client disconnects

2019-03-14 Thread Evan Hanson
Hi all,

Here's a tiny patch that makes the debug server end the program when a
client disconnects unexpectedly, i.e. without first sending a terminate
signal. Currently, the program will instead go into an infinite loop and
burn 100% of a CPU until it's killed.

Cheers,

Evan
>From a93b4d4c6d213f2c04c34f8ff79ea61be6a0262a Mon Sep 17 00:00:00 2001
From: Evan Hanson 
Date: Thu, 14 Mar 2019 21:42:47 +1300
Subject: [PATCH] Avoid spin loop in socket_read() when debug client
 disconnects

---
 dbg-stub.c | 10 --
 1 file changed, 8 insertions(+), 2 deletions(-)

diff --git a/dbg-stub.c b/dbg-stub.c
index 348a77f6..218d8506 100644
--- a/dbg-stub.c
+++ b/dbg-stub.c
@@ -159,7 +159,7 @@ socket_read()
   if(*(input_buffer_top++) == '\n') {
 *ptr = '\0';
 --input_buffer_len;
-return 0;
+return 1;
   }
 
   if(++off >= RW_BUFFER_SIZE) return -1; /* read-buffer overflow */
@@ -171,6 +171,8 @@ socket_read()
 
 if(n == SOCKET_ERROR) return -1; /* read failed */
 
+if(n == 0) return 0; /* client disconnect */
+
 input_buffer_len = n;
 input_buffer_top = input_buffer;
   }
@@ -304,7 +306,11 @@ send_event(int event, C_char *loc, C_char *val, C_char *cloc)
 send_string_value(cloc);
 send_string(")\n");
 
-if(socket_read() < 0) terminate("read failed");
+n = socket_read();
+
+if(n < 0) terminate("read failed");
+
+if(n == 0) terminate("debugger disconnected");
 
 /* fprintf(stderr, "\n", rw_buffer); */
 n = sscanf(rw_buffer, "(%d ", );
-- 
2.11.0

___
Chicken-hackers mailing list
Chicken-hackers@nongnu.org
https://lists.nongnu.org/mailman/listinfo/chicken-hackers