unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
* bug#300: strange interaction of periodic timers and suspending Emacs 22.1
@ 2008-05-22 19:52 ` Joe Wells
  2008-05-23 21:17   ` Stefan Monnier
                     ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Joe Wells @ 2008-05-22 19:52 UTC (permalink / raw)
  To: bug-gnu-emacs

Dear GNU Emacs gurus,

When Emacs 22.1 resumes from being suspended, it will execute a
periodic timer once for each time it would have executed it if Emacs
had never been suspended.  The same thing happens when the entire
computer is suspended (i.e., hibernation (suspend to disk) or sleep
(suspend to RAM)).

While it is conceivable that some uses of timers might prefer the
current behavior, in the vast majority of cases a timer does a
recurrent task and there is no benefit from repeating the timer
zillions of times in a row.  In fact, this can cause Emacs to freeze
for a long time when resuming due to processing periodic timers if the
timers do significant work.

It would be better if it was possible to get a periodic timer to
execute at most once for a period of time during which Emacs (or the
entire computer) was suspended.

Depending on your point of view, this is either a bug report (my
viewpoint) or a feature request.

To reproduce the behavior, start Emacs with this command:

  emacs -nw -Q

(You must be using a shell where the command "fg %1" will resume this
Emacs if it is suspended.  If not, adjust the reproduction code below
to use the correct command to resume Emacs.)

Then evaluate these 3 sexps:

  (defun foo-timer (x)
    (or (eq x 'xyzzy) (error "impossible"))
    (setq foo-counter (1+ foo-counter))
    ;;(message "foo-counter: %d" foo-counter)
    ;;(setq bar-counter foo-counter)
    (save-excursion
      (set-buffer foo-buffer)
      (goto-char (point-max))
      (insert (format " %d" foo-counter))))

  (defun foo-test ()
    (interactive)
    (and (boundp 'foo-timer)
         (timerp foo-timer)
         (cancel-timer foo-timer))
    (setq foo-counter 0)
    (setq foo-buffer-name "*foo-buffer*")
    (kill-buffer (get-buffer-create foo-buffer-name))
    (setq foo-buffer (get-buffer-create foo-buffer-name))
    ;; Once per second, starting now.
    (setq foo-timer (run-at-time t 1 #'foo-timer 'xyzzy))
    (switch-to-buffer foo-buffer)
    (sit-for 5)
    (goto-char (point-max))
    (insert " suspending")
    ;; extra BUG: In violation of its documentation, suspend-emacs
    ;; will add a newline or return character to the string it is
    ;; asked to stuff as terminal input.
    (suspend-emacs "sleep 10; fg %1")
    (goto-char (point-max))
    (insert " resuming")
    ;; possible extra BUG: It is strange that the (sit-for 0) needs to
    ;; be repeated to get all the events.  The first (sit-for 0)
    ;; generally only gets 3 events.
    (dotimes (x 20) (sit-for 0))
    (goto-char (point-max))
    (insert " canceling")
    (cancel-timer foo-timer)
    (buffer-string))

  (foo-test)

After this finishes, the contents of the *foo-buffer* buffer will be
(roughly) this string:

  " 1 2 3 4 5 suspending resuming 6 7 8 9 10 11 12 13 14 15 canceling"

The count can sometimes reach 6 before suspending and 16 before
canceling.

Instead of the current Emacs behavior, it would be very desirable to
be able to have periodic timers whose behavior would leave the
contents of the *foo-buffer* buffer as this string:

  " 1 2 3 4 5 suspending resuming 6 canceling"

It is very undesirable to have a periodic timer repeated zillions of
times upon resuming Emacs.  Note that the same problem happens when
the entire computer is suspended and resumed.

In addition to wanting to see this problem fixed, I would also be
interested to hear of a reliable way to work around it.

I hope this report is helpful.

-- 
Joe Wells

======================================================================
In GNU Emacs 22.1.1 (i686-pc-linux-gnu, GTK+ Version 2.8.20)
 of 2007-06-27 on artemis
configured using `configure  '--prefix=/home/jbw/local2' '--enable-debug' '--disable-nls' '--with-x-toolkit=gtk' 'CFLAGS=-O0 -g3 -ggdb''

Important settings:
  value of $LC_ALL: nil
  value of $LC_COLLATE: nil
  value of $LC_CTYPE: en_US.UTF-8
  value of $LC_MESSAGES: nil
  value of $LC_MONETARY: nil
  value of $LC_NUMERIC: nil
  value of $LC_TIME: jbw
  value of $LANG: nil
  locale-coding-system: utf-8
  default-enable-multibyte-characters: t

Major mode: Fundamental

Minor modes in effect:
  encoded-kbd-mode: t
  menu-bar-mode: t
  file-name-shadow-mode: t
  global-font-lock-mode: t
  unify-8859-on-encoding-mode: t
  utf-translate-cjk-mode: t
  auto-compression-mode: t
  line-number-mode: t


-- 
Heriot-Watt University is a Scottish charity
registered under charity number SC000278.








^ permalink raw reply	[flat|nested] 4+ messages in thread

* bug#300: strange interaction of periodic timers and suspending Emacs 22.1
  2008-05-22 19:52 ` bug#300: strange interaction of periodic timers and suspending Emacs 22.1 Joe Wells
@ 2008-05-23 21:17   ` Stefan Monnier
       [not found]   ` <mailman.12118.1211578329.18990.bug-gnu-emacs@gnu.org>
  2008-05-30  3:10   ` bug#300: marked as done (strange interaction of periodic timers and suspending Emacs 22.1) Emacs bug Tracking System
  2 siblings, 0 replies; 4+ messages in thread
From: Stefan Monnier @ 2008-05-23 21:17 UTC (permalink / raw)
  To: Joe Wells; +Cc: bug-gnu-emacs, 300

> When Emacs 22.1 resumes from being suspended, it will execute a
> periodic timer once for each time it would have executed it if Emacs
> had never been suspended.  The same thing happens when the entire
> computer is suspended (i.e., hibernation (suspend to disk) or sleep
> (suspend to RAM)).

I seem to remember a discussion about this a couple years ago.
Does anybody remember something about it?

> While it is conceivable that some uses of timers might prefer the
> current behavior, in the vast majority of cases a timer does a
> recurrent task and there is no benefit from repeating the timer
> zillions of times in a row.  In fact, this can cause Emacs to freeze
> for a long time when resuming due to processing periodic timers if the
> timers do significant work.

Yes, that sounds like a problem.  Do you have an actual case where this
currently happens?  We have enough real-life problems to fix, so if this
problem is only theoretical it's less likely to get fixed soon.

> In addition to wanting to see this problem fixed, I would also be
> interested to hear of a reliable way to work around it.

A reliable way is to make your timer non-repeated and instead to re-arm
it manually at its end.  This also helps when your timer takes more time
to process than its expected frequency.


        Stefan






^ permalink raw reply	[flat|nested] 4+ messages in thread

* bug#300: strange interaction of periodic timers and suspending Emacs 22.1
       [not found]   ` <mailman.12118.1211578329.18990.bug-gnu-emacs@gnu.org>
@ 2008-05-29 11:50     ` Joe Wells
  0 siblings, 0 replies; 4+ messages in thread
From: Joe Wells @ 2008-05-29 11:50 UTC (permalink / raw)
  To: Stefan Monnier; +Cc: 300

[ I am not cc-ing this to bug-gnu-emacs@gnu.org on the theory that
  sending this to 300@emacsbugs.donarmstrong.com will also get it sent
  to bug-gnu-emacs@gnu.org.  I am guessing this is the cause of all
  the duplicates showing up in the gnu.emacs.bug newsgroup. ]

Stefan Monnier <monnier@iro.umontreal.ca> writes:

>> When Emacs 22.1 resumes from being suspended, it will execute a
>> periodic timer once for each time it would have executed it if Emacs
>> had never been suspended.  The same thing happens when the entire
>> computer is suspended (i.e., hibernation (suspend to disk) or sleep
>> (suspend to RAM)).
>
[...]
>
>> While it is conceivable that some uses of timers might prefer the
>> current behavior, in the vast majority of cases a timer does a
>> recurrent task and there is no benefit from repeating the timer
>> zillions of times in a row.  In fact, this can cause Emacs to freeze
>> for a long time when resuming due to processing periodic timers if the
>> timers do significant work.
>
> Yes, that sounds like a problem.  Do you have an actual case where this
> currently happens?  We have enough real-life problems to fix, so if this
> problem is only theoretical it's less likely to get fixed soon.

The case that bugs me enough to care is that I have a timer that
invokes desktop-save roughly every 5 minutes.  (My scheme is a bit
more clever than that and tries to save during idle times, but if it
can't save during an idle time it will still ensure the desktop file
is saved at least every 5 minutes.)  My desktop file is quite big
because I include in desktop-globals-to-save some variables that have
very large values, so it takes 2 to 3 seconds to save this file.  If I
suspend my computer for 20 hours, when I wake it up Emacs wants to do
desktop-save 240 times, which can easily take several minutes.

I hope this information is helpful.

-- 
Joe


-- 
Heriot-Watt University is a Scottish charity
registered under charity number SC000278.







^ permalink raw reply	[flat|nested] 4+ messages in thread

* bug#300: marked as done (strange interaction of periodic timers  and suspending Emacs 22.1)
  2008-05-22 19:52 ` bug#300: strange interaction of periodic timers and suspending Emacs 22.1 Joe Wells
  2008-05-23 21:17   ` Stefan Monnier
       [not found]   ` <mailman.12118.1211578329.18990.bug-gnu-emacs@gnu.org>
@ 2008-05-30  3:10   ` Emacs bug Tracking System
  2 siblings, 0 replies; 4+ messages in thread
From: Emacs bug Tracking System @ 2008-05-30  3:10 UTC (permalink / raw)
  To: Stefan Monnier

[-- Attachment #1: Type: text/plain, Size: 917 bytes --]


Your message dated Thu, 29 May 2008 23:05:14 -0400
with message-id <jwvr6bk7arz.fsf-monnier+emacsbugreports@gnu.org>
and subject line Re: bug#300: strange interaction of periodic timers and suspending Emacs 22.1
has caused the Emacs bug report #300,
regarding strange interaction of periodic timers and suspending Emacs 22.1
to be marked as done.

This means that you claim that the problem has been dealt with.
If this is not the case it is now your responsibility to reopen the
bug report if necessary, and/or fix the problem forthwith.

(NB: If you are a system administrator and have no idea what this
message is talking about, this may indicate a serious mail system
misconfiguration somewhere. Please contact don@donarmstrong.com
immediately.)


-- 
300: http://emacsbugs.donarmstrong.com/cgi-bin/bugreport.cgi?bug=300
Emacs Bug Tracking System
Contact don@donarmstrong.com with problems

[-- Attachment #2: Type: message/rfc822, Size: 6894 bytes --]

From: Joe Wells <jbw@localhost.localdomain>
To: bug-gnu-emacs@gnu.org
Subject: strange interaction of periodic timers and suspending Emacs 22.1
Date: Thu, 22 May 2008 20:52:13 +0100
Message-ID: <86tzgq3ymq.fsf@macs.hw.ac.uk>

Dear GNU Emacs gurus,

When Emacs 22.1 resumes from being suspended, it will execute a
periodic timer once for each time it would have executed it if Emacs
had never been suspended.  The same thing happens when the entire
computer is suspended (i.e., hibernation (suspend to disk) or sleep
(suspend to RAM)).

While it is conceivable that some uses of timers might prefer the
current behavior, in the vast majority of cases a timer does a
recurrent task and there is no benefit from repeating the timer
zillions of times in a row.  In fact, this can cause Emacs to freeze
for a long time when resuming due to processing periodic timers if the
timers do significant work.

It would be better if it was possible to get a periodic timer to
execute at most once for a period of time during which Emacs (or the
entire computer) was suspended.

Depending on your point of view, this is either a bug report (my
viewpoint) or a feature request.

To reproduce the behavior, start Emacs with this command:

  emacs -nw -Q

(You must be using a shell where the command "fg %1" will resume this
Emacs if it is suspended.  If not, adjust the reproduction code below
to use the correct command to resume Emacs.)

Then evaluate these 3 sexps:

  (defun foo-timer (x)
    (or (eq x 'xyzzy) (error "impossible"))
    (setq foo-counter (1+ foo-counter))
    ;;(message "foo-counter: %d" foo-counter)
    ;;(setq bar-counter foo-counter)
    (save-excursion
      (set-buffer foo-buffer)
      (goto-char (point-max))
      (insert (format " %d" foo-counter))))

  (defun foo-test ()
    (interactive)
    (and (boundp 'foo-timer)
         (timerp foo-timer)
         (cancel-timer foo-timer))
    (setq foo-counter 0)
    (setq foo-buffer-name "*foo-buffer*")
    (kill-buffer (get-buffer-create foo-buffer-name))
    (setq foo-buffer (get-buffer-create foo-buffer-name))
    ;; Once per second, starting now.
    (setq foo-timer (run-at-time t 1 #'foo-timer 'xyzzy))
    (switch-to-buffer foo-buffer)
    (sit-for 5)
    (goto-char (point-max))
    (insert " suspending")
    ;; extra BUG: In violation of its documentation, suspend-emacs
    ;; will add a newline or return character to the string it is
    ;; asked to stuff as terminal input.
    (suspend-emacs "sleep 10; fg %1")
    (goto-char (point-max))
    (insert " resuming")
    ;; possible extra BUG: It is strange that the (sit-for 0) needs to
    ;; be repeated to get all the events.  The first (sit-for 0)
    ;; generally only gets 3 events.
    (dotimes (x 20) (sit-for 0))
    (goto-char (point-max))
    (insert " canceling")
    (cancel-timer foo-timer)
    (buffer-string))

  (foo-test)

After this finishes, the contents of the *foo-buffer* buffer will be
(roughly) this string:

  " 1 2 3 4 5 suspending resuming 6 7 8 9 10 11 12 13 14 15 canceling"

The count can sometimes reach 6 before suspending and 16 before
canceling.

Instead of the current Emacs behavior, it would be very desirable to
be able to have periodic timers whose behavior would leave the
contents of the *foo-buffer* buffer as this string:

  " 1 2 3 4 5 suspending resuming 6 canceling"

It is very undesirable to have a periodic timer repeated zillions of
times upon resuming Emacs.  Note that the same problem happens when
the entire computer is suspended and resumed.

In addition to wanting to see this problem fixed, I would also be
interested to hear of a reliable way to work around it.

I hope this report is helpful.

-- 
Joe Wells

======================================================================
In GNU Emacs 22.1.1 (i686-pc-linux-gnu, GTK+ Version 2.8.20)
 of 2007-06-27 on artemis
configured using `configure  '--prefix=/home/jbw/local2' '--enable-debug' '--disable-nls' '--with-x-toolkit=gtk' 'CFLAGS=-O0 -g3 -ggdb''

Important settings:
  value of $LC_ALL: nil
  value of $LC_COLLATE: nil
  value of $LC_CTYPE: en_US.UTF-8
  value of $LC_MESSAGES: nil
  value of $LC_MONETARY: nil
  value of $LC_NUMERIC: nil
  value of $LC_TIME: jbw
  value of $LANG: nil
  locale-coding-system: utf-8
  default-enable-multibyte-characters: t

Major mode: Fundamental

Minor modes in effect:
  encoded-kbd-mode: t
  menu-bar-mode: t
  file-name-shadow-mode: t
  global-font-lock-mode: t
  unify-8859-on-encoding-mode: t
  utf-translate-cjk-mode: t
  auto-compression-mode: t
  line-number-mode: t


-- 
Heriot-Watt University is a Scottish charity
registered under charity number SC000278.





[-- Attachment #3: Type: message/rfc822, Size: 1935 bytes --]

From: Stefan Monnier <monnier@iro.umontreal.ca>
To: Joe Wells <jbw@macs.hw.ac.uk>
Cc: 300-done@emacsbugs.donarmstrong.com
Subject: Re: bug#300: strange interaction of periodic timers and suspending Emacs 22.1
Date: Thu, 29 May 2008 23:05:14 -0400
Message-ID: <jwvr6bk7arz.fsf-monnier+emacsbugreports@gnu.org>

> The case that bugs me enough to care is that I have a timer that
> invokes desktop-save roughly every 5 minutes.  (My scheme is a bit
> more clever than that and tries to save during idle times, but if it
> can't save during an idle time it will still ensure the desktop file
> is saved at least every 5 minutes.)  My desktop file is quite big

Of course, beside fixing your code to not use a repeating timer, but
a one-shot timer which you manually re-arm at the end, it turns out we
already have a variable to control this problem:

    timer-max-repeats


-- Stefan


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2008-05-30  3:10 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
     [not found] <jwvr6bk7arz.fsf-monnier+emacsbugreports@gnu.org>
2008-05-22 19:52 ` bug#300: strange interaction of periodic timers and suspending Emacs 22.1 Joe Wells
2008-05-23 21:17   ` Stefan Monnier
     [not found]   ` <mailman.12118.1211578329.18990.bug-gnu-emacs@gnu.org>
2008-05-29 11:50     ` Joe Wells
2008-05-30  3:10   ` bug#300: marked as done (strange interaction of periodic timers and suspending Emacs 22.1) Emacs bug Tracking System

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).