Skip to content

Measure D1 usage again after Cloudflare made its binding fields private - #266

Merged
tnkuehne merged 1 commit into
mainfrom
fix/d1-observation-sessions
Nov 21, 2026
Merged

tnkuehne merged 1 commit into
mainfrom
fix/d1-observation-sessions

Conversation

@tnkuehne

@tnkuehne tnkuehne commented Nov 21, 2026 •

Copy link
Copy Markdown
Contributor

Problem

The runtime measured D1 queries through the binding's primary-only session, alwaysPrimarySession. On 2026-11-04 Cloudflare made that field private (workerd 7d790e45). Since then the setup check fails on every query. The runtime then falls back to the unobserved binding and only logs D1 observation unavailable: incompatible native binding. No D1 usage is measured at all.

Fix

  • Sessions: the runtime observes the fresh session that each query already gets from withSession(). It changes _send on that session in place. A copy cannot reach the private transport fields.
  • exec: native exec runs only on the private primary-only session. The runtime now runs the same statements as one batch on the query's session:
    • Like native exec, it treats each line as one statement.
    • In local workerd this matched native exec for multi-statement lines, blank lines, PRAGMA and failures. On a failure, both roll back every statement.
    • The error message keeps the native D1_EXEC_ERROR: Error in line 1: … format.
  • Loud failure: a query that completes without passing through the measured _send is reported with the same error. That would happen if D1 queries took another path, such as the experimental d1_binding_jsrpc binding.

Tests

  • Unit tests: a session mirroring workerd's private fields.
  • workerd comparison: tests/cloudflare/d1-observer.test.ts compares every scenario natively and through the runtime, including exec and exec-error.
  • Passing locally: pnpm check, the app runtime unit tests, and the Cloudflare and dynamic runtime suites.
  • Known local failure: the noindex control-plane test, which also fails locally on main.

RetriggerView in GreptileConfidence Score: 5/5

The PR appears safe to merge; no actionable issues were found.

Summary

Restores D1 usage measurement by observing each query’s fresh session instead of the binding’s now-private primary session.

  • D1 queries now measure usage on their own fresh session.
  • D1 exec runs each SQL line together on the query session.

Reviews (1) · Last reviewed commit: "Measure D1 usage again after Cloudflare ..." · Reviewed by Greptile

Cloudflare moved the D1 binding's primary-only session into a private
field. The runtime needed it to observe queries, so it fell back to the
unobserved binding and measured no usage at all.

Observe the fresh session of each query in place instead, and run exec
as one batch on that session, as native exec treats each line as one
statement. A query that completes without a measurement is reported.
@cloudflare-workers-and-pages

Copy link
Copy Markdown

Deploying with  Cloudflare Workers  Cloudflare Workers

The latest updates on your project. Learn more about integrating Git with Workers.

Status Name Latest Commit Updated (UTC)
✅ Deployment successful!
View logs
widefleet-docs 0f6783d Nov 21 2026, 04:47 PM

@tnkuehne
tnkuehne merged commit 83280eb into main Nov 21, 2026
17 checks passed
@tnkuehne
tnkuehne deleted the fix/d1-observation-sessions branch November 21, 2026 17:07
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