From a2c8f2e71da2395b7e90b0db2c1a1b39a19c64f0 Mon Sep 17 00:00:00 2001
From: c vw <dl1ycf@darc.de>
Date: Wed, 26 Feb 2020 12:17:31 +0100
Subject: [PATCH] Terminate new protocol receiving thread gracefully if exiting
 piHPSDR.

---
 new_protocol.c | 11 +++++++++++
 1 file changed, 11 insertions(+)

diff --git a/new_protocol.c b/new_protocol.c
index c679567..a851f61 100644
--- a/new_protocol.c
+++ b/new_protocol.c
@@ -1377,6 +1377,17 @@ g_print("new_protocol_thread\n");
 
         buffer=malloc(NET_BUFFER_SIZE);
         bytesread=recvfrom(data_socket,buffer,NET_BUFFER_SIZE,0,(struct sockaddr*)&addr,&length);
+
+        if (!running) {
+          //
+          // When leaving piHPSDR, it may happen that the protocol has been stopped while
+	  // we were doing "rcvfrom". In this case, we do not want to "exit" but let the main
+	  // thread exit gracefully, including writing the props files.
+	  //
+	  free(buffer);
+	  break;
+	}
+
         if(bytesread<0) {
             g_print("recvfrom socket failed for new_protocol_thread");
             exit(-1);
-- 
2.45.2