[Spice-devel,vdagent-win,3/6] vdservice: clean agent proc handle in kill_agent()

Submitted by Arnon Gilboa on July 24, 2011, 9:38 p.m.

Details

Message ID 1311518301-1446-4-git-send-email-agilboa@redhat.com
State New, archived
Headers show

Not browsing as part of any series.

Commit Message

Arnon Gilboa July 24, 2011, 9:38 p.m.
fill_agent_events() will not add it to the events used by WaitFor(),
preventing usage of a dead process handle.
---
 vdservice/vdservice.cpp |   11 +++++++----
 1 files changed, 7 insertions(+), 4 deletions(-)

Patch hide | download patch | download mbox

diff --git a/vdservice/vdservice.cpp b/vdservice/vdservice.cpp
index 8c34f59..e09828a 100644
--- a/vdservice/vdservice.cpp
+++ b/vdservice/vdservice.cpp
@@ -909,21 +909,24 @@  bool VDService::kill_agent()
 {
     DWORD exit_code = 0;
     DWORD wait_ret;
+    HANDLE proc_handle;
     bool ret = true;
 
     if (!_agent_alive) {
         return true;
     }
     _agent_alive = false;
+    proc_handle = _agent_proc_info.hProcess;
+    _agent_proc_info.hProcess = 0;
     if (_pipe_connected) {
         _pipe_connected = false;
         DisconnectNamedPipe(_pipe_state.pipe);
     }
-    if (GetProcessId(_agent_proc_info.hProcess)) {
-        wait_ret = WaitForSingleObject(_agent_proc_info.hProcess, 3000);
+    if (GetProcessId(proc_handle)) {
+        wait_ret = WaitForSingleObject(proc_handle, 3000);
         switch (wait_ret) {
         case WAIT_OBJECT_0:
-            if (GetExitCodeProcess(_agent_proc_info.hProcess, &exit_code)) {
+            if (GetExitCodeProcess(proc_handle, &exit_code)) {
                 vd_printf("vdagent exit code %u", exit_code);
             } else if (exit_code == STILL_ACTIVE) {
                 vd_printf("Failed killing vdagent");
@@ -942,7 +945,7 @@  bool VDService::kill_agent()
             break;
         }
     }
-    CloseHandle(_agent_proc_info.hProcess);
+    CloseHandle(proc_handle);
     CloseHandle(_agent_proc_info.hThread);
     ZeroMemory(&_agent_proc_info, sizeof(_agent_proc_info));
     return ret;