From ed28b0f2cac246e9dad669fd59631b8f9e5db2f5 Mon Sep 17 00:00:00 2001
From: Ben Gamari <ben@smart-cactus.org>
Date: Tue, 18 Jul 2023 15:45:12 -0400
Subject: [PATCH] rts/win32: Ensure reliability of IO manager shutdown

When the Win32 threaded IO manager shuts down, `ioManagerDie` sends an
`IO_MANAGER_DIE` event to the IO manager thread using the
`io_manager_event` event object. Finally, it will closes the event object,
and invalidate `io_manager_event`.

Previously, `readIOManagerEvent` would see that `io_manager_event` is
invalid and return `0`, suggesting that everything is right with the
world. This meant that if `ioManagerDie` invalidated the handle before
the event manager was blocked on the event we would end up in a
situation where the event manager would never realize it was asked to
shut down.

Fix this by ensuring that `readIOManagerEvent` instead returns
`IO_MANAGER_DIE` when we detect that the event object has been
invalidated by `ioManagerDie`.

Fixes #23691.
---
 rts/win32/ThrIOManager.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/rts/win32/ThrIOManager.c b/rts/win32/ThrIOManager.c
index 6bbf65a45f5..72806456687 100644
--- a/rts/win32/ThrIOManager.c
+++ b/rts/win32/ThrIOManager.c
@@ -79,7 +79,9 @@ readIOManagerEvent (void)
             }
         }
     } else {
-        res = 0;
+        // Making it here means that we have hit ioManagerDie, which
+        // closed our event object.
+        res = IO_MANAGER_DIE;
     }
 
     OS_RELEASE_LOCK(&event_buf_mutex);
-- 
GitLab