← Debugging Guides

阅读中文版本

Cloudflare Workers — CPU time limit exceeded

A Cloudflare Worker throws "exceeded CPU limit" because the request consumed more CPU time than the plan allows, from a tight loop or heavy computation.

intermediatedebuggingcloudflareperformance

Symptoms

  • error: Exceeded CPU limit
  • The Worker returns a 1042 error code to the client
  • Requests with large payloads fail but small ones succeed

Possible causes

  • A loop iterates over a large array without chunking or yielding
  • A JSON.parse or regex runs on a very large string
  • A crypto operation like bcrypt is too expensive for the CPU limit

Diagnosis steps

Check the Worker CPU limit for your plan

npx wrangler tail --format json

Interpretation

The logs show CPU time per request; compare against the plan limit (10ms free, 30ms paid).

Profile the slow request

npx wrangler dev --local

Interpretation

Run the failing request locally and use console.time to find the slow segment.

Root cause

The Workers runtime enforces a per-request CPU time limit; synchronous computation exceeding the limit is terminated and the client receives an error.

Fix

Move heavy computation to a Durable Object, a separate service, or chunk the work across multiple requests. Replace CPU-heavy libs with edge-compatible alternatives.

Verify the fix

Deploy and send the large payload; confirm the Worker returns a successful response within the CPU limit.

Symptom walkthrough

A Cloudflare Worker throws “exceeded CPU limit” because the request consumed more CPU time than the plan allows, from a tight loop or heavy computation. The error appears in production but may not surface in local development because the conditions that trigger it depend on the deployment environment or runtime configuration. Understanding the full symptom set helps distinguish this issue from similar errors.

Diagnosis in practice

Follow the diagnosis steps in the frontmatter to isolate the cause. Each step includes a command and an interpretation of the output. The goal is to narrow down whether the issue is in the configuration, the runtime, or the application code. Reproduce the error in a minimal setup before applying a fix.

Root cause

The Workers runtime enforces a per-request CPU time limit; synchronous computation exceeding the limit is terminated and the client receives an error.

Fix

Move heavy computation to a Durable Object, a separate service, or chunk the work across multiple requests. Replace CPU-heavy libs with edge-compatible alternatives. After applying the fix, monitor the system to confirm the error does not recur under load or in different environments.

Verification

Deploy and send the large payload; confirm the Worker returns a successful response within the CPU limit. If the verification passes, the fix is confirmed; if not, re-examine the diagnosis steps for missed contributing factors.

Diagnostic commands

Run these commands to reproduce and diagnose the issue:

npx wrangler tail --format json
npx wrangler dev --local