Skip to content

Fix countdown drift.#220

Merged
treyhunner merged 6 commits intotreyhunner:mainfrom
rodrigogiraoserrao:fix-drift
Mar 28, 2026
Merged

Fix countdown drift.#220
treyhunner merged 6 commits intotreyhunner:mainfrom
rodrigogiraoserrao:fix-drift

Conversation

@rodrigogiraoserrao
Copy link
Copy Markdown
Contributor

Fixes #219.

This fixes the drift I mentioned in #219 by using the real time that is elapsed during sleeps and in calling other functions. This change breaks some of the current tests because some of the tests relied on the exact looping structure. I did not fix the broken tests. I did test the app locally before and after the fix. A 60s timer takes ~66s on my computer before the fix and it takes ~60s after the fix.

I understand that, given I didn't touch the tests, you may close this PR. I just opened it either way to share a reference implementation for a fix.

@treyhunner
Copy link
Copy Markdown
Owner

@rodrigogiraoserrao I just pushed tests and a fix (to make sure 00:00 is shown just before the timer exits). Can you verify the timer still doesn't drift noticeably on your machine?

Thanks!

When subtracting time clamped n to 0, sleep_until was adjusted by the
full 30 seconds instead of the actual amount subtracted. Now
sleep_until is only adjusted by the actual change to n.

Also use None instead of 0 as the pause_start sentinel for clarity.
- Fix wrong reasoning in line count comment (test_main.py)
- Remove stale "20 chunks" and "chunked 1-second" references
- Fix "10x" drift comment to show actual values
- Fix "fewer sleep iterations" to "seconds are skipped entirely"
- Assert extreme drift test ends at 0
- Assert pause test displays 0
@rodrigogiraoserrao
Copy link
Copy Markdown
Contributor Author

@treyhunner looks good to me. I pulled your changes and ran a 1m timer together with a stopwatch on my phone. They ran in sync.

@treyhunner treyhunner merged commit aa5d715 into treyhunner:main Mar 28, 2026
14 checks passed
@treyhunner
Copy link
Copy Markdown
Owner

Great. Thanks for the PR @rodrigogiraoserrao! I'll put a new release on PyPI shortly.

@rodrigogiraoserrao
Copy link
Copy Markdown
Contributor Author

Great. Thanks for the PR @rodrigogiraoserrao! I'll put a new release on PyPI shortly.

Well, it was more or less what you'd call a “poisoned gift” in Portuguese. It was more work for you than the trouble it saved you 😅. But thanks for your work on the tests.

@treyhunner
Copy link
Copy Markdown
Owner

The tests needed refactoring anyway. 😉

And an LLM did most of the tedious parts anyway.

Thanks for reporting the bug and making a fix!

@rodrigogiraoserrao rodrigogiraoserrao deleted the fix-drift branch March 30, 2026 12:38
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.

Substantial drift for large timers

2 participants