Skip to content
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

Fix WheelTimer implementation that can expired timeout early #17850

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from

Conversation

udovichenkoAlexander
Copy link

When entries insert in the end of timer queue, then unnecessary entry inserted (with duplicated key).
This can lead to some timeouts expired early and consume memory.

This can be checked with following test (to this file)

def test_multi_insert_then_past(self) -> None:
    wheel: WheelTimer[object] = WheelTimer(bucket_size=5)

    obj1 = object()
    obj2 = object()
    obj3 = object()
    wheel.insert(100, obj1, 150)
    wheel.insert(100, obj2, 160)
    wheel.insert(100, obj3, 155)
    
    self.assertListEqual(wheel.fetch(110), [])
    self.assertListEqual(wheel.fetch(158), [obj1])

Look at the entries after obj2 insert:

_Entry(end_key=21, elements=set())
_Entry(end_key=22, elements=set())
_Entry(end_key=23, elements=set())
_Entry(end_key=24, elements=set())
_Entry(end_key=25, elements=set())
_Entry(end_key=26, elements=set())
_Entry(end_key=27, elements=set())
_Entry(end_key=28, elements=set())
_Entry(end_key=29, elements=set())
_Entry(end_key=30, elements=set())
_Entry(end_key=31, elements={<object object at 0x7a129fb7ab80>})
_Entry(end_key=31, elements=set()) <-- this entry has duplicate key
_Entry(end_key=32, elements=set())
_Entry(end_key=33, elements={<object object at 0x7a129fb7ab70>})

gaps and duplicates cause errors when search Entry to insert in the middle of queue:
self.entries[max(min_key, then_key) - min_key].elements.add(obj)

This Pull request fix this with some minor refactoring (remove current_tick unused variable) and add this test case to unit tests.

@udovichenkoAlexander udovichenkoAlexander requested a review from a team as a code owner October 18, 2024 14:33
@CLAassistant
Copy link

CLAassistant commented Oct 18, 2024

CLA assistant check
All committers have signed the CLA.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants