Skip to content

fix: avoid division by zero when logging watch load rate - #4289

Open
snowyukitty wants to merge 1 commit into
dgtlmoon:masterfrom
snowyukitty:fix/zero-elapsed-watch-load-rate
Open

fix: avoid division by zero when logging watch load rate#4289
snowyukitty wants to merge 1 commit into
dgtlmoon:masterfrom
snowyukitty:fix/zero-elapsed-watch-load-rate

Conversation

@snowyukitty

Copy link
Copy Markdown

Summary

  • Measure watch-loading durations with time.perf_counter().
  • Avoid dividing by zero when the clock reports a non-positive elapsed duration.
  • Add a deterministic regression test for an empty datastore completing within one clock tick.

Root cause

load_all_watches() used time.time() and unconditionally formatted loaded / elapsed in both completion log paths. A fast load can receive identical clock readings, making elapsed == 0.0; an empty datastore then aborts startup with ZeroDivisionError while trying to log 0 / 0.0.

This was reproduced on Windows, where the wall clock has lower resolution, but the guard is platform-independent. The regression test replaces only this module's clock reference with constant readings, fails on master, and passes with this fix.

Testing

  • Regression test before the fix: fails with ZeroDivisionError at file_saving_datastore.py:330.
  • pytest -q changedetectionio/tests/unit/test_file_saving_datastore.py — 1 passed.
  • pytest -q changedetectionio/tests/unit/ — 210 passed, 30 subtests passed.
  • ruff check . --select E9,F63,F7,F82,INT.
  • ruff check changedetectionio/tests/unit/test_file_saving_datastore.py.
  • git diff --check.

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.

1 participant