Script exceeded CPU time limit error with crypto.subtle and compatibility_date
0 reputation · 22 Oct 2023, 00:25 UTC
0 reputation · 22 Oct 2023, 00:25 UTC
Cloudflare Workers enforce a per-request CPU time limit on the free tier, and the execution duration of the crypto.subtle API varies with the worker's compatibility_date setting. With a compatibility_date prior to 2023-09-01 the runtime uses an older isolate where certain cryptographic operations can exceed the limit and produce the Script exceeded CPU time limit error. With compatibility_date set to 2023-09-01 or later the runtime enables a newer V8 isolate with an optimized Web Crypto implementation that reduces the likelihood of limit breaches for equivalent workloads.
The transition is not uniform. Libraries that rely on older Node.js crypto polyfills may still experience intermittent limit breaches even with the newer compatibility date, indicating an unresolved tension between backward compatibility and predictable performance. Local development with wrangler dev does not always mirror production CPU limit behavior, leaving the practical impact of compatibility_date choices uncertain for reproducible builds.
Which compatibility_date values produce consistent CPU limit compliance for crypto.subtle-based workloads in production? How does the choice interact with third-party crypto polyfills under the newer isolate? What observable behavior differences remain between pre-2023-09-01 and 2023-09-01+ runtimes for the same crypto.subtle operations?
33275 reputation · 22 Oct 2023, 08:53 UTC
For most crypto.subtle workloads that run on the free tier, setting compatibility_date to 2023‑09‑01 or later provides consistent compliance with the per‑request CPU limit. Workloads that rely on the older isolate (any date before 2023‑09‑01) frequently exceed the limit because the Web Crypto implementation is less efficient.
Cloudflare Workers use a V8 isolate that changes with the compatibility date. The isolate introduced on 2023‑09‑01 includes an optimized native Web Crypto backend that reduces the CPU cycles required for operations such as RSA key generation, ECDH, and AES‑GCM. The pre‑2023‑09‑01 isolate implements these algorithms in software or via a less‑efficient bridge, which can consume several hundred milliseconds of CPU time for a single operation.
Libraries that polyfill Node.js’ crypto module (for example crypto‑js or node‑crypto‑polyfill) do not use the native crypto.subtle API even when the newer isolate is available. They re‑implement the algorithms in JavaScript, which still incurs the same high CPU cost as the old isolate. Consequently, a worker that uses such a polyfill can hit the limit regardless of the compatibility_date setting.
wrangler.toml, set compatibility_date = "2023‑09‑01" (or a later date).
crypto.subtle calls where possible.
Because the new isolate is not a silver bullet for all workloads, the most reliable way to avoid the error is to keep the total CPU per request well under the free‑tier threshold and to avoid heavy JavaScript polyfills for cryptographic work.
Could you specify which third‑party library (if any) you are using for cryptographic operations? Knowing that would help refine the recommendation.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.