Changes between and of Initial VersionVersion 40Ticket #2424


Ignore:
Timestamp:
07/22/15 23:33:56 (6 years ago)
Author:
Glyph
Comment:

Legend:

Unmodified
Added
Removed
Modified
  • Ticket #2424

    • Property Cc spiv ivank zooko@… added
    • Property Owner Glyph deleted
    • Property Summary changed from task.LoopingCall: a problem if change the system time to ☂ Allow Twisted programs to behave correctly with respect to changes in the system clock
  • Ticket #2424 – Description

    initial v40  
    1 I wrote a simple test trying to find why my app freeze
    2 when change the system time:
     1Twisted currently assumes that the wall clock and system clock times are perfectly synchronized, and provides no facility for compensating when that stops being true.
    32
    4 {{{
     3This is a problem because the mechanism for "when to wake up` for `callLater` is decided by passing a `timeout` parameter to the operating system multiplexing API - `select()` (or similar).  This API generally considers "time" to be "the monotonic clock".
    54
    6 from twisted.internet import reactor, task
    7 import time
     5This can manifest in a few ways:
    86
     7  - There's no way to schedule a repeated call - using `LoopingCall` or otherwise - which reliably runs in the face of the system clock being moved backward.  If you are trying to schedule a call every 10 milliseconds, and the system clock is moved backward by a day, the timestamp inside the `callLater` will indicate that it should not run again until 1 day plus 10 milliseconds have passed.
     8  - If you set a 60 minute timeout with `callLater`, wait 30 minutes, and then put your system to sleep for 120 minutes, when it wakes up it will wait an additional 30 minutes to trigger the call, ''unless'' there's some other activity which wakes up the reactor, in which case it will fix itself and get called immediately.
     9  - If NTP introduces a large slew, the frequency of LoopingCall invocations will slow down or speed up accordingly.
    910
    10 def callback():
    11     print time.ctime(time.time())
     11To fix this, we will need to:
    1212
     13 1. introduce 2 new APIs: `callAfter` and `callAt`, which use the monotonic and wall clocks respectively
     14 2. try to monitor for notifications about "wake from sleep" and "system clock changed" events, on Windows, Mac, Linux, and FreeBSD, to the extent that that's possible.
     15 3. expose a new API, `callWhenTimeChanged` or something like that, to detect deltas between monotonic and wall clocks and allow applications to make specific corrections for them.
    1316
    14 
    15 
    16 t = task.LoopingCall(callback)
    17 t.start(2.0)
    18 reactor.run()
    19 
    20 }}}
    21 
    22 If run this and change the system time, let's say a
    23 day forward, everything work fine, but if change the
    24 system time backward, the program freeze until you
    25 change forward again. This is specially problematic
    26 for a computer using ntp, updating its clock time to
    27 time.
    28 
    29 It seems that there is a problem in LoopingCall._reschedule that don't play well when "fromNow = self.starttime - self._seconds()" is
    30 negative, and seems to be platform independent:
    31 
    32 W2k Pro
    33 python 2.4.4
    34 twisted 2.4.0
    35 
    36 Wxp Pro
    37 python 2.4.4
    38 twisted 2.4.0
    39 
    40 Ubuntu Linux 5.1
    41 python 2.4.2
    42 twisted 2.0.1-4
     17A ticket should be filed for each of these pieces of work.