Skip to content

fix: enforce Cloudflare binding timeout for non-streaming calls - #132

Merged
stackbilt-admin merged 4 commits into
Stackbilt-dev:mainfrom
mikemikimike:fix/provider-timeout-nonstream
Nov 9, 2026
Merged

stackbilt-admin merged 4 commits into
Stackbilt-dev:mainfrom
mikemikimike:fix/provider-timeout-nonstream

Conversation

@mikemikimike

Copy link
Copy Markdown
Contributor

Fixes #131

Issue: #131

Cloudflare Workers AI binding calls made through non-streaming generateResponse now honor the provider timeout setting. Slow calls reject with the library's TimeoutError, while successful calls clear their timer; streaming keeps its existing open-timeout handling.

Tests:

  • npm test
  • npm run typecheck
  • npm run build
  • npm run test:worker-import
  • git diff --check

@stackbilt-admin stackbilt-admin left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. The binding path ignoring timeout was a real gap, and the approach looks right:

  • the timer is cleared in .finally on both success and error;
  • the losing side of the race is handled;
  • a timeout raises the same TimeoutError / TIMEOUT the HTTP providers use;
  • streaming opts out with enforceTimeout: false, so its open timer doesn't run twice;
  • zero and negative timeout values disable this deadline; an omitted setting uses the existing 30-second default.

The first test (a binding call that never resolves) is a real regression test.

Changes requested:

  1. Make the cleanup test prove cleanup. "clears the non-streaming timeout after a binding response" resolves the request and then advances time, but it would pass without clearTimeout, and it also passes on main. Please record vi.getTimerCount() before the call and check it returns to that baseline after the response resolves. Please also add an error-path case: when ai.run rejects, the timer is cleared and the original error is surfaced, not a TimeoutError.

  2. Restore real timers in afterEach. vi.useRealTimers() currently runs at the end of each test body. If an assertion fails or the test hangs, fake timers leak into later tests. The pattern in stream-lifecycle.test.ts / resilience-context.test.ts is what we use elsewhere.

  3. Document the behavior change in the CHANGELOG and with a short README note:

    • BaseProvider defaults timeout to 30000 ms, so non-streaming Workers AI calls that had no deadline are now limited to 30s by default.
    • The timeout applies per attempt, not to the whole operation. TimeoutError is retryable and the default allows 3 retries, so one logical request can take four attempts plus backoff. The factory can also fall back to another provider on TIMEOUT.
    • The Workers AI binding takes no abort signal, so Promise.race stops waiting but does not cancel the call. A timed-out call may keep running alongside retries and may still incur charges.
    • For long generations, raise timeout, lower maxRetries, or use streaming.

    A short code comment at the race site explaining the abandon-not-abort behavior would also help.

Optional, not blocking:

  • Treat non-finite or > 2^31−1 timeouts as "no timeout". setTimeout clamps those values and fires almost immediately, so timeout: Infinity would now fail at once. makeRequest has the same latent issue, so this could also be a follow-up.
  • healthCheck() still calls the binding with no deadline. That's fine as a follow-up.

We plan to ship this as a patch release, since it makes an existing setting work. CI is waiting on maintainer approval for fork PRs. We'll require normal CI to pass on the final commit before merging. The failing supply-chain check is a known repo-level problem unrelated to this PR, tracked in #137.

@mikemikimike

Copy link
Copy Markdown
Contributor Author

Addressed the review feedback on the timeout change:\n\n- Added timer-count assertions for successful binding responses and a rejection-path regression test that preserves the original error.\n- Moved fake-timer cleanup into afterEach.\n- Added the requested CHANGELOG and README documentation, plus a comment explaining that Workers AI timeout abandons the wait without aborting the binding call.\n\nValidation: npm test (792 tests), npm run typecheck, npm run build, npm run test:worker-import, and git diff --check.

@stackbilt-admin

Copy link
Copy Markdown
Member

Thanks, the updates cover everything from the review. Before we can review it again, it needs a rebase: #139 (released as 1.24.0) changed src/providers/cloudflare.ts, and this PR now conflicts with main. Please rebase onto current main (a2bc706) and push. Once it's rebased, we'll approve CI and do a final review.

@stackbilt-admin

Copy link
Copy Markdown
Member

Thanks for the rebase and the updates. The cleanup tests now catch a missing clearTimeout, vi.useRealTimers() is in afterEach, and the README section covers what we asked for. We reviewed the PR as it would land on 1.25.0. It typechecks, the suite passes, and the timeout handling holds up: the timer is cleared on success and rejection, there are no unhandled rejections, a timeout raises a retryable TimeoutError the factory can fall back on, streaming gets no second timer, and the Llama vision path is bounded too. Two small items block merging.

Blocking

  1. Non-finite or oversized timeouts fail at once. The guard in runModel only skips !timeout || timeout <= 0. setTimeout clamps Infinity and anything above 2³¹−1 ms to 1 ms, so timeout: Infinity (which some callers use to mean "no deadline") now gives TimeoutError: Request timeout after Infinityms straight away. With default retries that is 4 abandoned ai.run calls per request, each possibly billed, and the failures count against the circuit breaker. Before this PR those calls worked. Please treat only finite values in (0, 2_147_483_647] as a deadline (for example Number.isFinite(t) && t > 0 && t <= 2_147_483_647) and add a test for Infinity and 2 ** 31.
  2. CHANGELOG. The entry reads as a pure fix. Please describe the behaviour change for users: with no timeout set, Workers AI binding calls are now cut off at 30 s per attempt and retried; a timed-out call is abandoned, not cancelled, so it may keep running and be billed; and repeated timeouts count toward the circuit breaker. "Clear their deadline timer" can go. It also needs a rebase onto main (1.25.0); only CHANGELOG.md conflicts, and new entries go under ## [Unreleased].

Please fix in the same round

  • Abort signal wording (README and the comment at the race in cloudflare.ts). "Workers AI bindings do not accept an abort signal" says more than the sources show. Cloudflare's bindings docs and AiOptions in @cloudflare/workers-types document no signal, but Cloudflare's own workers-ai-provider passes signal to binding.run (cloudflare/ai#457), and nothing documents whether the runtime honours it. Something like "the binding API documents no abort signal, so a timed-out call is not cancelled" is accurate. Passing a signal the same way is optional and can be a follow-up.
  • timeout: 0. On Workers AI, 0 or a negative value now means no deadline; on the HTTP providers 0 means the 30 s default (timeoutMs || config.timeout || 30000). Please say what 0 does in the ProviderConfig.timeout JSDoc and the README, or make the behaviours match.
  • README. Name the release instead of "now", add a sentence that timeouts count as circuit-breaker failures (a slow model pinned with allowFallback: false gives TimeoutError three times, then CircuitBreakerOpenError), and note in Reasoning Control that high reasoning effort on Workers AI can run past the 30 s default.

Follow-up, not needed here: healthCheck() still calls the binding without a deadline.

@mikemikimike
mikemikimike force-pushed the fix/provider-timeout-nonstream branch from e207228 to 28a9100 Compare November 7, 2026 04:13
@stackbilt-admin

Copy link
Copy Markdown
Member

Thanks for the update. This covers everything from the last review: only finite timeouts in (0, 2³¹−1] set a deadline, with tests for Infinity and 2 ** 31; the abort-signal wording is accurate in both the README and the code comment; timeout: 0 is documented; and the README covers circuit-breaker failures and high reasoning effort. We reviewed it as it would land on 1.25.0. It typechecks, the suite passes (899 tests), and the build and worker import check pass.

Two small documentation fixes before we merge:

  1. Release name. The CHANGELOG entry and the README section say "In 1.25.0", but this ships as 1.25.1. Please name 1.25.1, or describe the behaviour without a version in the CHANGELOG entry, since its release heading will name the version.
  2. CHANGELOG spacing. The diff deletes the blank line before ## [1.24.1]. Please put it back.

Optional: the first new test still calls vi.useRealTimers() at the end of its body. That's redundant now that afterEach does it.

Resolve the CHANGELOG conflict with 1.26.0 and Stackbilt-dev#147, describe the timeout
behaviour without naming a release (it ships in the next one), and restore
the blank line before ## [1.24.1].

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@stackbilt-admin

Copy link
Copy Markdown
Member

Thanks again for this. To get it into the next release, we finished the last two docs items ourselves in one merge commit (559c36b) on your branch:

  • Merged main. 1.26.0 and feat: route around models whose context window can't hold the input #147 had changed the same part of the CHANGELOG, so there was a conflict. The timeout entry now sits under ### Fixed in [Unreleased], after Added and Changed, with a link to this PR.
  • Version wording. Our last review asked for "1.25.1", but 1.26.0 has since shipped, so that was out of date. Instead, the CHANGELOG entry and the README section now describe the behaviour without naming a version: "Calls with no timeout set are now cut off…" and "BaseProvider defaults timeout to 30,000 ms…". The release heading will give the version.
  • CHANGELOG spacing. The blank line before ## [1.24.1] is back.

We didn't change any code or tests; the optional vi.useRealTimers() cleanup is left as it was. On the merged branch, typecheck, all 951 tests, the build, and the package and worker import checks pass.

@stackbilt-admin
stackbilt-admin merged commit c5f00af into Stackbilt-dev:main Nov 9, 2026
2 checks passed
stackbilt-admin added a commit that referenced this pull request Nov 9, 2026
* chore: release 1.27.0

- Opt-in context-window routing and a better input estimate (#147)
- Workers AI non-streaming binding timeout (#132)
- Checkpoint and README version references updated

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* docs: 1.27.0 upgrade note for the Workers AI timeout and review fixes

Co-Authored-By: Claude Opus 5.5 <[email protected]>

---------

Co-authored-by: Kurt Overmier <[email protected]>
Co-authored-by: Claude Opus 5.5 <[email protected]>
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.

Provider timeout is silently ignored on non-streaming Workers AI calls

3 participants