Skip to content

bpo-19270: Fixed sched.scheduler.cancel to cancel correct event #22729

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 17 commits into from
Oct 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions Lib/sched.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,23 +26,19 @@
import time
import heapq
from collections import namedtuple
from itertools import count
import threading
from time import monotonic as _time

__all__ = ["scheduler"]

class Event(namedtuple('Event', 'time, priority, action, argument, kwargs')):
__slots__ = []
def __eq__(s, o): return (s.time, s.priority) == (o.time, o.priority)
def __lt__(s, o): return (s.time, s.priority) < (o.time, o.priority)
def __le__(s, o): return (s.time, s.priority) <= (o.time, o.priority)
def __gt__(s, o): return (s.time, s.priority) > (o.time, o.priority)
def __ge__(s, o): return (s.time, s.priority) >= (o.time, o.priority)

Event = namedtuple('Event', 'time, priority, sequence, action, argument, kwargs')
Event.time.__doc__ = ('''Numeric type compatible with the return value of the
timefunc function passed to the constructor.''')
Event.priority.__doc__ = ('''Events scheduled for the same time will be executed
in the order of their priority.''')
Event.sequence.__doc__ = ('''A continually increasing sequence number that
separates events if time and priority are equal.''')
Event.action.__doc__ = ('''Executing the event means executing
action(*argument, **kwargs)''')
Event.argument.__doc__ = ('''argument is a sequence holding the positional
Expand All @@ -61,6 +57,7 @@ def __init__(self, timefunc=_time, delayfunc=time.sleep):
self._lock = threading.RLock()
self.timefunc = timefunc
self.delayfunc = delayfunc
self._sequence_generator = count()

def enterabs(self, time, priority, action, argument=(), kwargs=_sentinel):
"""Enter a new event in the queue at an absolute time.
Expand All @@ -71,8 +68,10 @@ def enterabs(self, time, priority, action, argument=(), kwargs=_sentinel):
"""
if kwargs is _sentinel:
kwargs = {}
event = Event(time, priority, action, argument, kwargs)

with self._lock:
event = Event(time, priority, next(self._sequence_generator),
action, argument, kwargs)
heapq.heappush(self._queue, event)
return event # The ID

Expand Down Expand Up @@ -136,7 +135,8 @@ def run(self, blocking=True):
with lock:
if not q:
break
time, priority, action, argument, kwargs = q[0]
(time, priority, sequence, action,
argument, kwargs) = q[0]
now = timefunc()
if time > now:
delay = True
Expand Down
11 changes: 11 additions & 0 deletions Lib/test/test_sched.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,17 @@ def test_cancel_concurrent(self):
self.assertTrue(q.empty())
self.assertEqual(timer.time(), 4)

def test_cancel_correct_event(self):
# bpo-19270
events = []
scheduler = sched.scheduler()
scheduler.enterabs(1, 1, events.append, ("a",))
b = scheduler.enterabs(1, 1, events.append, ("b",))
scheduler.enterabs(1, 1, events.append, ("c",))
scheduler.cancel(b)
scheduler.run()
self.assertEqual(events, ["a", "c"])

def test_empty(self):
l = []
fun = lambda x: l.append(x)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
:meth:`sched.scheduler.cancel()` will now cancel the correct event, if two
events with same priority are scheduled for the same time. Patch by Bar Harel.