From 836f1b6b0560bd178efb8d52900b4b136f87ae30 Mon Sep 17 00:00:00 2001
From: Zebediah Figura <zfigura@codeweavers.com>
Date: Tue, 23 Jul 2019 17:22:20 -0500
Subject: [PATCH] ntdll: Check the APC fd first.

---
 dlls/ntdll/esync.c | 13 ++++++++-----
 1 file changed, 8 insertions(+), 5 deletions(-)

diff --git a/dlls/ntdll/esync.c b/dlls/ntdll/esync.c
index fc621ccfb..0adb4ad77 100644
--- a/dlls/ntdll/esync.c
+++ b/dlls/ntdll/esync.c
@@ -1046,6 +1046,14 @@ static NTSTATUS __esync_wait_objects( DWORD count, const HANDLE *handles,
             ret = do_poll( fds, pollcount, timeout ? &end : NULL );
             if (ret > 0)
             {
+                /* We must check this first! The server may set an event that
+                 * we're waiting on, but we need to return STATUS_USER_APC. */
+                if (alertable)
+                {
+                    if (fds[pollcount - 1].revents & POLLIN)
+                        goto userapc;
+                }
+
                 /* Find out which object triggered the wait. */
                 for (i = 0; i < count; i++)
                 {
@@ -1089,11 +1097,6 @@ static NTSTATUS __esync_wait_objects( DWORD count, const HANDLE *handles,
                         return count - 1;
                     }
                 }
-                if (alertable)
-                {
-                    if (fds[i++].revents & POLLIN)
-                        goto userapc;
-                }
 
                 /* If we got here, someone else stole (or reset, etc.) whatever
                  * we were waiting for. So keep waiting. */
-- 
2.23.0

