[Spice-devel] [vdagent-win PATCH v5 6/6] vdagent: Stop correctly helper thread

Frediano Ziglio fziglio at redhat.com
Mon Jul 9 08:51:44 UTC 2018


The thread launched to detect desktop switches events is not stopped
correctly causing potentially dandling pointers.
Queue a APC to make the loop exit and wait for thread termination
from the main thread.

Signed-off-by: Frediano Ziglio <fziglio at redhat.com>
---
 vdagent/vdagent.cpp | 14 ++++++++++++--
 1 file changed, 12 insertions(+), 2 deletions(-)

diff --git a/vdagent/vdagent.cpp b/vdagent/vdagent.cpp
index e0923be..0321d85 100644
--- a/vdagent/vdagent.cpp
+++ b/vdagent/vdagent.cpp
@@ -240,11 +240,13 @@ DWORD WINAPI VDAgent::event_thread_proc(LPVOID param)
         return 1;
     }
     while (agent->_running) {
-        DWORD wait_ret = WaitForSingleObject(desktop_event, INFINITE);
+        DWORD wait_ret = WaitForSingleObjectEx(desktop_event, INFINITE, TRUE);
         switch (wait_ret) {
         case WAIT_OBJECT_0:
             agent->set_control_event(CONTROL_DESKTOP_SWITCH);
             break;
+        case WAIT_IO_COMPLETION:
+            break;
         case WAIT_TIMEOUT:
         default:
             vd_printf("WaitForSingleObject(): %lu", wait_ret);
@@ -254,6 +256,10 @@ DWORD WINAPI VDAgent::event_thread_proc(LPVOID param)
     return 0;
 }
 
+static VOID CALLBACK event_thread_stop_proc(ULONG_PTR)
+{
+}
+
 bool VDAgent::run()
 {
     DWORD session_id;
@@ -335,8 +341,12 @@ bool VDAgent::run()
             set_clipboard_owner(owner_none);
         }
     }
-    vd_printf("Agent stopped");
+    if (!QueueUserAPC(event_thread_stop_proc, event_thread, 0)) {
+        TerminateThread(event_thread, 0);
+    }
+    WaitForSingleObject(event_thread, INFINITE);
     CloseHandle(event_thread);
+    vd_printf("Agent stopped");
     return true;
 }
 
-- 
2.17.1



More information about the Spice-devel mailing list