Skip to content

fix(cmdutil): cap poll sleep at the deadline so --timeout stops on time - #47

Merged
kshahbw merged 2 commits into
Bandwidth:mainfrom
kush2shah:fix/poll-timeout
Aug 25, 2026
Merged

fix(cmdutil): cap poll sleep at the deadline so --timeout stops on time#47
kshahbw merged 2 commits into
Bandwidth:mainfrom
kush2shah:fix/poll-timeout

Conversation

@kush2shah

Copy link
Copy Markdown
Contributor

Fixes #35.

What was wrong

Poll in internal/cmdutil/poll.go always slept the full --interval between checks and only tested the deadline after honoring done. So --timeout could overshoot by a full interval plus the in-flight request: with a 5s interval and --timeout 1, the command resolved at t≈5s — as exit 0 if the second poll succeeded, or exit 5 if it didn't — never at ~1s. This affected every --wait command.

The fix

The sleep is now min(interval, time-until-deadline), so the final wait lands on the deadline: one last check runs there, and no new check starts past it. Overshoot is now bounded by one in-flight request instead of one interval.

On the open question from the issue — success on a check that started on time but lands at/after the deadline — this keeps exit 0 with the result, as the issue argues: the operation genuinely completed, and discarding that is worse than being slightly late. --timeout bounds when polling stops, not total wall-clock duration; the doc comment on Poll and the AGENTS.md section now say exactly that (the old AGENTS.md text described the loose behavior and is replaced with the tightened bound).

Tests

Two regression tests, written failing-first against the old loop (both failed at ~250ms, now pass at ~20ms):

  • TestPollTimeoutDoesNotOvershootByFullInterval — 250ms interval, 20ms timeout must report ErrPollTimeout (exit 5) at ~20ms, not ~250ms.
  • TestPollFinalCheckAtDeadlineCanSucceed — a check that succeeds at the deadline returns the result at ~deadline, not a full interval later.

Verified: go build, go vet, full suite, gofmt clean; poll tests race-clean over -count=5.

Poll always slept the full interval between checks and only tested the
deadline after honoring done, so --timeout could overshoot by a full
interval (plus the in-flight request): a 5s interval with --timeout 1
resolved at t≈5s, as success or as exit 5, never at ~1s.

The sleep is now min(interval, time-until-deadline), so the final check
lands on the deadline and no new check starts past it. A check that
starts on time and succeeds still returns its result — the operation
genuinely completed; --timeout bounds when polling stops, not total
wall-clock duration. Overshoot is now bounded by one in-flight request
instead of one interval.

AGENTS.md's interim description of the loose behavior is updated to
match. Fixes Bandwidth#35.
@kush2shah
kush2shah requested review from a team as code owners August 25, 2026 15:22
@bwappsec

bwappsec commented Aug 25, 2026

Copy link
Copy Markdown

Snyk checks have passed. No issues have been found so far.

Status Scan Engine Critical High Medium Low Total (0)
Open Source Security 0 0 0 0 0 issues
Licenses 0 0 0 0 0 issues
Code Security 0 0 0 0 0 issues

💻 Catch issues earlier using the plugins for VS Code, JetBrains IDEs, Visual Studio, and Eclipse.

@kshahbw
kshahbw merged commit 09965d6 into Bandwidth:main Aug 25, 2026
8 checks passed
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.

--timeout is checked between polls, so it overshoots and can succeed after expiring

4 participants