Function write-region resets idle time

classic Classic list List threaded Threaded
9 messages Options
Reply | Threaded
Open this post in threaded view
|

Function write-region resets idle time

Cecil Westerhof-4
I found a strange problem: write-region resets idle-time. But only
after the calling function has returned.

I defined the following functions:
    (defun write-region-problem ()
        (message (format "Before: %s %s" (current-time) (current-idle-time)))
        (write-region "Just a test\n" nil "~/problem.log" t 0)
        (message (format "After:  %s %s" (current-time) (current-idle-time)))
        )

    (defun write-region-problem-loop ()
      (dotimes (i 5)
        (sleep-for 15)
        (write-region-problem)))


When using:
    (run-with-idle-timer 30   nil   'write-region-problem-loop)

I get:
    Before: (23127 39556 376787 939000) (0 45 6316 747000)
    After:  (23127 39556 426254 575000) (0 45 55783 245000)
    Before: (23127 39571 427804 974000) (0 60 57334 626000)
    After:  (23127 39571 469158 56000) (0 60 98686 884000)
    Before: (23127 39586 472115 145000) (0 75 101644 158000)
    After:  (23127 39586 519021 983000) (0 75 148550 571000)
    Before: (23127 39601 522126 756000) (0 90 151656 293000)
    After:  (23127 39601 570429 201000) (0 90 199958 257000)
    Before: (23127 39616 573897 68000) (0 105 203426 556000)
    After:  (23127 39616 620647 371000) (0 105 250175 854000)

So everything seems to be OK.


But when using:
    (setq timer-object (run-with-timer 30 30 'write-region-problem))

I get:
    Before: (23127 39977 941713 503000) (0 29 994274 462000)
    After:  (23127 39977 982179 647000) (0 30 34741 483000)
    Before: (23127 40007 940217 421000) (0 29 946739 305000)
    After:  (23127 40007 981669 405000) (0 29 988192 219000)
    Before: (23127 40037 941670 105000) (0 29 944688 557000)
    After:  (23127 40037 991646 30000) (0 29 994665 603000)
    Before: (23127 40067 941685 17000) (0 29 938036 13000)
    After:  (23127 40067 992876 336000) (0 29 989228 329000)
    Before: (23127 40097 939595 311000) (0 29 936576 788000)
    After:  (23127 40097 977542 689000) (0 29 974525 333000)


So clearly write-region resets idle time.

In the past it was no problem at all. But at a certain point the
functionality changed. I do not know when, because I had for some time
problems with my gnus idle daemon, but I only now found out what the
problem is.


The version of Emacs I am using:
    "GNU Emacs 24.5.1 (x86_64-pc-linux-gnu, GTK+ Version 3.22.11)
     of 2017-09-12 on hullmann, modified by Debian"

--
Cecil Westerhof
Senior Software Engineer
LinkedIn: http://www.linkedin.com/in/cecilwesterhof
Reply | Threaded
Open this post in threaded view
|

Re: Function write-region resets idle time

Eli Zaretskii
> From: Cecil Westerhof <[hidden email]>
> Date: Thu, 11 Jan 2018 18:43:53 +0100
>
> I found a strange problem: write-region resets idle-time. But only
> after the calling function has returned.

??? Any Lisp you run resets idle-time.  Idleness starts when Emacs
gets to its command loop and finds that there's no pending input.  It
ends when there's some input event that causes the command loop to run
the Lisp interpreter.  In your case, the input event is supplied by
the timer you set up.

So why is what you discovered surprising, let alone a problem?  Am I
missing something?

Reply | Threaded
Open this post in threaded view
|

Re: Function write-region resets idle time

Cecil Westerhof-4
In reply to this post by Cecil Westerhof-4
Eli Zaretskii <[hidden email]> writes:

>> From: Cecil Westerhof <[hidden email]>
>> Date: Thu, 11 Jan 2018 18:43:53 +0100
>>
>> I found a strange problem: write-region resets idle-time. But only
>> after the calling function has returned.
>
> ??? Any Lisp you run resets idle-time.  Idleness starts when Emacs
> gets to its command loop and finds that there's no pending input.  It
> ends when there's some input event that causes the command loop to run
> the Lisp interpreter.  In your case, the input event is supplied by
> the timer you set up.
>
> So why is what you discovered surprising, let alone a problem?  Am I
> missing something?

The definition of idle:
    Emacs becomes idle when it starts waiting for user input, and it
    remains idle until the user provides some input. If a timer is set
    for five seconds of idleness, it runs approximately five seconds
    after Emacs first becomes idle.

And to make it even more clear:
    Even if repeat is non-nil, this timer will not run again as long
    as Emacs remains idle, because the duration of idleness will
    continue to increase and will not go down to five seconds again.

So the running of the function should NOT reset idle time.


The behaviour changed (for me) on the 19th of December.

And if what you say would be true: then there should be also no
difference if my function uses write-region or not. But there is.
Without write-region idle time is NOT reset. With write-region
idle-time IS reset.

I certainly find it surprising that write-region does something else
as all other functions with idle time.

--
Cecil Westerhof
Senior Software Engineer
LinkedIn: http://www.linkedin.com/in/cecilwesterhof
Reply | Threaded
Open this post in threaded view
|

Re: Function write-region resets idle time

Cecil Westerhof-4
Cecil Westerhof <[hidden email]> writes:

> Eli Zaretskii <[hidden email]> writes:
>
>>> From: Cecil Westerhof <[hidden email]>
>>> Date: Thu, 11 Jan 2018 18:43:53 +0100
>>>
>>> I found a strange problem: write-region resets idle-time. But only
>>> after the calling function has returned.
>>
>> ??? Any Lisp you run resets idle-time.  Idleness starts when Emacs
>> gets to its command loop and finds that there's no pending input.  It
>> ends when there's some input event that causes the command loop to run
>> the Lisp interpreter.  In your case, the input event is supplied by
>> the timer you set up.
>>
>> So why is what you discovered surprising, let alone a problem?  Am I
>> missing something?
>
> The definition of idle:
>     Emacs becomes idle when it starts waiting for user input, and it
>     remains idle until the user provides some input. If a timer is set
>     for five seconds of idleness, it runs approximately five seconds
>     after Emacs first becomes idle.
>
> And to make it even more clear:
>     Even if repeat is non-nil, this timer will not run again as long
>     as Emacs remains idle, because the duration of idleness will
>     continue to increase and will not go down to five seconds again.
>
> So the running of the function should NOT reset idle time.
>
>
> The behaviour changed (for me) on the 19th of December.
>
> And if what you say would be true: then there should be also no
> difference if my function uses write-region or not. But there is.
> Without write-region idle time is NOT reset. With write-region
> idle-time IS reset.
>
> I certainly find it surprising that write-region does something else
> as all other functions with idle time.

And to make it even more clear, I defined:
    (defun only-message ()
      (message (format "Only message: %s %s" (current-time) (current-idle-time)))
      )

And when I use:
    (setq timer-object (run-with-timer 30 30 'only-message))

I get:
    Only message: (23127 48054 400127 610000) (0 29 996735 28000)
    Only message: (23127 48084 396995 23000) (0 59 993602 312000)
    Only message: (23127 48114 400137 106000) (0 89 996744 686000)
    Only message: (23127 48144 400100 220000) (0 119 996707 561000)
    Only message: (23127 48174 400122 394000) (0 149 996729 864000)

So again, ONLY calling write-region does rest idle time.

--
Cecil Westerhof
Senior Software Engineer
LinkedIn: http://www.linkedin.com/in/cecilwesterhof
Reply | Threaded
Open this post in threaded view
|

Re: Function write-region resets idle time

Stefan Monnier
In reply to this post by Cecil Westerhof-4
> I certainly find it surprising that write-region does something else
> as all other functions with idle time.

Agreed.  But Emacs users can't do much about it.
Please M-x report-emacs-bug instead.


        Stefan


Reply | Threaded
Open this post in threaded view
|

Re: Function write-region resets idle time

Cecil Westerhof-4
In reply to this post by Cecil Westerhof-4
Stefan Monnier <[hidden email]> writes:

>> I certainly find it surprising that write-region does something else
>> as all other functions with idle time.
>
> Agreed.  But Emacs users can't do much about it.
> Please M-x report-emacs-bug instead.

OK, I will do that.

Just to show how strange it is, I share what I found in my further
examination.

When calling emacs with --no-init-file there is no problem with
write-region. After some prodding I found that write-region goes wrong
as soon as I use one or both of the following in my .emacs:
    (desktop-save-mode 1)
    (global-auto-revert-mode 1)

--
Cecil Westerhof
Senior Software Engineer
LinkedIn: http://www.linkedin.com/in/cecilwesterhof
Reply | Threaded
Open this post in threaded view
|

Re: Function write-region resets idle time

Eli Zaretskii
In reply to this post by Cecil Westerhof-4
> From: Cecil Westerhof <[hidden email]>
> Date: Thu, 11 Jan 2018 20:38:17 +0100
>
> And when I use:
>     (setq timer-object (run-with-timer 30 30 'only-message))
>
> I get:
>     Only message: (23127 48054 400127 610000) (0 29 996735 28000)
>     Only message: (23127 48084 396995 23000) (0 59 993602 312000)
>     Only message: (23127 48114 400137 106000) (0 89 996744 686000)
>     Only message: (23127 48144 400100 220000) (0 119 996707 561000)
>     Only message: (23127 48174 400122 394000) (0 149 996729 864000)
>
> So again, ONLY calling write-region does rest idle time.

With the original code you posted, I don't see the problem: the
idle-time advances by 30 sec every 30 sec.  I tried on 2 different
systems, one of them GNU/Linux, the other MS-Windows, and I see
consistent behavior on both.

So it's something specific to your system.  Somehow, write-region
causes Emacs on your system to receive some input event, and that
event resets the idleness time.  Is what you see in "emacs -Q"?  If
not, maybe you have something like global auto-revert-mode set, in
which case you get inotify event whenever you write to a file.  Or
maybe some D-bus service does something similar?

In general, idle time is very fragile, and gets reset on any input
event.  For example, I have focus-follows-mouse in my window manager,
and just moving the mouse into the Emacs frame resets the idle time on
that system, as expected.

> The behaviour changed (for me) on the 19th of December.

I don't understand this: you said you are using Emacs 24.5, so what
could have changed your Emacs on that date?

Reply | Threaded
Open this post in threaded view
|

Re: Function write-region resets idle time

Cecil Westerhof-4
In reply to this post by Cecil Westerhof-4
Eli Zaretskii <[hidden email]> writes:

>> From: Cecil Westerhof <[hidden email]>
>> Date: Thu, 11 Jan 2018 20:38:17 +0100
>>
>> And when I use:
>>     (setq timer-object (run-with-timer 30 30 'only-message))
>>
>> I get:
>>     Only message: (23127 48054 400127 610000) (0 29 996735 28000)
>>     Only message: (23127 48084 396995 23000) (0 59 993602 312000)
>>     Only message: (23127 48114 400137 106000) (0 89 996744 686000)
>>     Only message: (23127 48144 400100 220000) (0 119 996707 561000)
>>     Only message: (23127 48174 400122 394000) (0 149 996729 864000)
>>
>> So again, ONLY calling write-region does rest idle time.
>
> With the original code you posted, I don't see the problem: the
> idle-time advances by 30 sec every 30 sec.  I tried on 2 different
> systems, one of them GNU/Linux, the other MS-Windows, and I see
> consistent behavior on both.
>
> So it's something specific to your system.  Somehow, write-region
> causes Emacs on your system to receive some input event, and that
> event resets the idleness time.  Is what you see in "emacs -Q"?  If
> not, maybe you have something like global auto-revert-mode set, in
> which case you get inotify event whenever you write to a file.  Or
> maybe some D-bus service does something similar?

Yes, that is one of the two things that gives the problem. As soon as
I have one or both of the two following in my .emacs I get this
behaviour:
    (desktop-save-mode 1)
    (global-auto-revert-mode 1)


> In general, idle time is very fragile, and gets reset on any input
> event.  For example, I have focus-follows-mouse in my window manager,
> and just moving the mouse into the Emacs frame resets the idle time on
> that system, as expected.

Until now I did not have a problem with it. I used it with Gnus to do
things that potentially take a lot of time only when I have been idle
for a period of time.

Until recently I did not have problems with this.


>> The behaviour changed (for me) on the 19th of December.
>
> I don't understand this: you said you are using Emacs 24.5, so what
> could have changed your Emacs on that date?

I do not know. I do regularly my updates and I suppose that I did an
update on that day and that this changed the functionality of Emacs.

I have used this functionality for about five years without a problem.


I already reported it as a bug.

--
Cecil Westerhof
Senior Software Engineer
LinkedIn: http://www.linkedin.com/in/cecilwesterhof
Reply | Threaded
Open this post in threaded view
|

Re: Function write-region resets idle time

Cecil Westerhof-4
In reply to this post by Cecil Westerhof-4
Cecil Westerhof <[hidden email]> writes:

> I found a strange problem: write-region resets idle-time. But only
> after the calling function has returned.

After some searching I found the problem. At the moment I had a
    (desktop-save-mode 1)
or a
    (global-auto-revert-mode 1)
in my emacs.el I got this problem. (Since the 19th of December.)

I was using emacs 24.5, but after updating to 25.1.1 the problem
disappeared. I am a bit curious what happened here, but I will let it
rust.

--
Cecil Westerhof
Senior Software Engineer
LinkedIn: http://www.linkedin.com/in/cecilwesterhof