unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
From: Eli Zaretskii <eliz@gnu.org>
To: Harald Sanftmann <haraldsa@web.de>
Cc: 63058@debbugs.gnu.org
Subject: bug#63058: 28.2; Emacs is terminated by Windows when logging off even if modified buffers are open
Date: Tue, 25 Apr 2023 09:21:05 +0300	[thread overview]
Message-ID: <83sfcowkha.fsf@gnu.org> (raw)
In-Reply-To: <fd005830-f4cb-185e-a0f0-2dd7e48e2c63@web.de> (message from Harald Sanftmann on Mon, 24 Apr 2023 17:48:59 +0200)

> Date: Mon, 24 Apr 2023 17:48:59 +0200
> From: Harald Sanftmann <haraldsa@web.de>
> 
>  From time to time it happens that I have not closed Emacs before
> logging out. Windows just terminates the process even if modified
> buffers are open.

That's the expected behavior in that case.  Emacs should auto-save
when the OS tells Emacs it is shutting down, so you have auto-save
files to restore the session later, when the system is restarted.

> I searched for a solution and learned how Windows is telling
> applications to prepare for the user logging off / Windows is shutting
> down:
> https://www.howtogeek.com/396277/what-exactly-happens-when-you-shut-down-or-sign-out-of-windows/
> 
> WM_QUERYENDSESSION and WM_ENDSESSION are the sent signals, if the
> application does not respond it will be terminated after 5 seconds.
> 
> Then I found some code which could be the Emacs implementation:
> 
> 
> https://github.com/emacs-mirror/emacs/blob/master/src/w32fns.c
> 
>        switch (msg)
>          {
>          case WM_ERASEBKGND:
>             ...
>          case WM_ENDSESSION:
>          my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
>          /* Allow time for Emacs to attempt an orderly shutdown.  If we
>             return, the process will be terminated immediately.  FIXME:
>             1000 seconds is too long to sleep if the shutdown attempt
>             fails (see bug#25875).  But if it fails, we want to find out
>             about it, so let's leave 1000 for now.  */
>          sleep (1000);
>          FALLTHROUGH;
> 
> Seems like Emacs does not respond but sleeps for 1000 s. This will
> probably not cut it.

This is not what happens; you saw only the first part of how Emacs on
Windows handles this situation, in the input thread.  What actually
happens is that before the input thread starts sleeping above, it
posts the WM_ENDSESSION message, via the my_post_msg call, to the main
thread, where we have this:

	case WM_ENDSESSION:
	  inev.kind = END_SESSION_EVENT;
	  break;

So this event gets inserted into the Emacs input event queue, and when
it is processed, we do:

    case END_SESSION_EVENT:
      /* Make an event (end-session).  */
      return list1 (Qend_session);

The above produces a Lisp event 'end-session', for which we have a
default binding:

  initial_define_lispy_key (Vspecial_event_map, "end-session",
			    "kill-emacs");

IOW, this event by default calls the command kill-emacs.  And that
command auto-saves any modified buffers that visit files, then shuts
down Emacs.

> Would be great to have a proper handling.

What is a proper handling in this case? delay the shutdown
indefinitely? that'd be even less proper, IMO.

See also bug#23483 (which led to the existing implementation) and
specifically these messages there:

  https://debbugs.gnu.org/cgi/bugreport.cgi?bug=23483#37
  https://debbugs.gnu.org/cgi/bugreport.cgi?bug=23483#40
  https://debbugs.gnu.org/cgi/bugreport.cgi?bug=23483#43
  https://debbugs.gnu.org/cgi/bugreport.cgi?bug=23483#46

You could, if you like, bind the end-session event to a different
command, perhaps that will suit your usage better.  But be very
careful when you do this, because invoking commands that require user
interaction will not work in this case: the Emacs' input thread, which
handles all kinds of inputs, is sleeping, so the user cannot interact
with Emacs.





  reply	other threads:[~2023-04-25  6:21 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-04-24 15:48 bug#63058: 28.2; Emacs is terminated by Windows when logging off even if modified buffers are open Harald Sanftmann
2023-04-25  6:21 ` Eli Zaretskii [this message]
2023-05-05  5:59   ` Eli Zaretskii

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=83sfcowkha.fsf@gnu.org \
    --to=eliz@gnu.org \
    --cc=63058@debbugs.gnu.org \
    --cc=haraldsa@web.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).