Subject: Re: bug#25875: 26.0.50; Hang logging out of MS-Windows

From: Ken Brown <[email-masked]>
Date: Sun, 26 Feb 2017 13:04:18 -0500

On 2/26/2017 10:44 AM, Eli Zaretskii wrote:
>> From: Richard Copley <[email-masked]>
>> Yes indeed. Hence "when shutting down or logging out". In this
>> case shutting down the computer.
> 
> OK, supporting that is a relatively new feature, so it's little
> surprise it needs more work.  Ken, could you please take a look?
> 
> As I understand it, this happens because when the input thread gets
> the WM_ENDSESSION message, it posts it to the main thread and goes on
> to sleep for 1000 sec, to avoid ending the Emacs process before it
> finishes orderly shutdown.  But if the main thread happens to be
> inside redisplay, it could invoke one of the function that send
> messages to the input thread via SendMessage, which waits for the
> input thread to respond.  So we do have a kind of deadlock.

The problem might be that 1000 sec is too long for the input thread to sleep.  I chose that number arbitrarily, not realizing that the main thread could get stuck waiting for the input thread.  What about something like this?

--- a/src/w32fns.c
+++ b/src/w32fns.c
@@ -4801,8 +4801,10 @@ w32_wnd_proc (HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
 
     case WM_ENDSESSION:
       my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
-      /* If we return, the process will be terminated immediately.  */
-      sleep (1000);
+      /* Allow time for Emacs to attempt an orderly shutdown.  If we
+        return, the process will be terminated immediately.  */
+      sleep (5);
+      return 0;
 
     case WM_WINDOWPOSCHANGING:
       /* Don't restrict the sizing of any kind of frames.  If the window

With this change, I think Emacs will be killed in at most 5 seconds no matter what state it is in.  But 
I can't test this because I don't know how to reproduce Richard's problem.

Ken