Skip to content

Add close() and Symbol.dispose to SqlStorageCursor - #8692

Merged
alsuren merged 1 commit into
mainfrom
dlaban/sql-cursor-close
Nov 23, 2026
Merged

alsuren merged 1 commit into
mainfrom
dlaban/sql-cursor-close

Conversation

@alsuren

@alsuren alsuren commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

A SQL cursor resets its SQLite statement only when it reaches its last row or when V8 collects it. An application that stops reading part way leaves the statement active on the Durable Object's connection, and while it is, SQLite rejects every DROP TABLE and DROP INDEX with SQLITE_LOCKED.

This adds cursor.close(), which ends the query without stepping it, and [Symbol.dispose]() (via JSG_DISPOSE(close)), so a cursor declared with using is closed however its block exits:

{
  using cursor = sql.exec("SELECT * FROM big");
  for (const row of cursor) {
    if (done(row)) break;
  }
}
sql.exec("DROP TABLE other"); // previously SQLITE_LOCKED
  • A closed cursor reads as one that has run to its end. columnNames, rowsRead and rowsWritten are kept.
  • close() is idempotent and does not step the statement, so it is safe after a step failed. Like the cursor's other methods, it throws when called from a different Durable Object; under using, that error arrives as a SuppressedError that still carries the original one.
  • Types: close(): void, and SqlStorageCursor extends Disposable, so TypeScript before 5.2 still compiles.
  • Opt in, so no compatibility flag.

@alsuren
alsuren force-pushed the dlaban/sql-cursor-close branch from 11d5a81 to 0ac4b60 Compare November 19, 2026 21:13
@alsuren
alsuren marked this pull request as ready for review November 19, 2026 22:20
@alsuren
alsuren requested review from a team as code owners November 19, 2026 22:20
@alsuren
alsuren requested a review from NuroDev November 19, 2026 22:20

@clanky-the-clanker clanky-the-clanker Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Superseded by the review of 12685c73.

This review, of 0ac4b60b

💬 Approved with comments — Correct, well-tested opt-in API addition. The only finding is an INFO: the wrong-IoContext fallback in close() loses the row counts and the tracing span tags.

Findings: 1 info (1 is an inline comment on the diff)

Not opened by the correctness review: 2 changed files it was meant to read
  • types/generated-snapshot/experimental/index.ts
  • types/generated-snapshot/index.d.ts

The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.


📋 Code Quality: 💬 1 info

Correct on the same-IoContext path; the wrong-IoContext fallback in close() behaves differently and has no test.

Recommendation: 💬 Optional Nits

  • [INFO] Wrong-IoContext fallback in close() skips endQuery(): row counts read as 0, the tracing span loses its tags, and the statement is freed later rather than right away; no test covers it. (src/workerd/api/sql.c++:321)
View full review

Cursor::close() (src/workerd/api/sql.c++:315-325) delegates to endQuery() when the cursor's state belongs to the current IoContext. That path keeps the row counts, runs the tracing callback and drops the statement, as the description says.

The else branch handles a cursor whose state belongs to another IoContext. It clears doneCallback and state without calling endQuery(), which has three effects:

  • rowsRead and rowsWritten keep their defaults, so after close() the getters report 0 instead of the real counts, which contradicts the description's "kept".
  • The tracing callback is dropped without running, so the cursor's span never gets its rows_read or rows_written tags.
  • Releasing the IoOwn<State> only queues the statement for deletion in the owning context, so it is not reset right away.

No test exercises this branch.


✅ All other checks passed

🔒 Security: ✅ LGTM

No security issues found.


📖 AGENTS.md: ✅ LGTM

No AGENTS.md issues found.


⚡ Performance: ✅ LGTM

No performance issues found.


📝 Documentation: ✅ LGTM

No documentation issues found.


Reviewed commit: 0ac4b60b

Comment thread src/workerd/api/sql.c++ Resolved
@alsuren
alsuren force-pushed the dlaban/sql-cursor-close branch from 0ac4b60 to 12685c7 Compare November 19, 2026 22:55

@clanky-the-clanker clanky-the-clanker Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Superseded by the review of 669c2023.

This review, of 12685c73

✅ Approved — This is a correct, well-tested, opt-in API addition. The earlier wrong-IoContext note is fixed (close() now throws, like the cursor's other methods), and no new issues were found.

Not opened by the correctness review: 2 changed files it was meant to read
  • types/generated-snapshot/experimental/index.ts
  • types/generated-snapshot/index.d.ts

The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.


✅ All checks passed

🔒 Security: ✅ LGTM

No security issues found.


📖 AGENTS.md: ✅ LGTM

No AGENTS.md issues found.


📋 Code Quality: ✅ LGTM

Cursor::close() now delegates to endQuery() whenever state is set. Calling it from another Durable Object throws, just as the cursor's other methods do, and the new closeOtherCursor test covers that case. The row counts and the tracing callback are kept, and calling close() twice is safe. The new tests cover closing after a partial read, using with break, throw and generators, a failed step, and statement-cache reuse.


⚡ Performance: ✅ LGTM

close() resets the statement sooner and adds no per-row work.


📝 Documentation: ✅ LGTM

No documentation issues found.


Reviewed commit: 12685c73

@alsuren
alsuren force-pushed the dlaban/sql-cursor-close branch from 12685c7 to 669c202 Compare November 20, 2026 09:28

@clanky-the-clanker clanky-the-clanker Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Superseded by the review of 8d5c9824.

This review, of 669c2023

✅ Approved — The opt-in API addition is correct and well tested, and the generated types match the source annotations. No issues were found.

Not opened by the correctness review: 2 changed files it was meant to read
  • types/generated-snapshot/experimental/index.ts
  • types/generated-snapshot/index.d.ts

The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.


✅ All checks passed

🔒 Security: ✅ LGTM

No security issues found.


📖 AGENTS.md: ✅ LGTM

No AGENTS.md issues found.


📋 Code Quality: ✅ LGTM

Cursor::close() calls endQuery() whenever state is set. It is idempotent and safe after a failed step, and it throws when called from another Durable Object, as the cursor's other methods do. JSG_METHOD(close) and JSG_DISPOSE(close) are wired up correctly. The tests cover the partial-read, using, failed-step, statement-cache and cross-Durable-Object cases.


⚡ Performance: ✅ LGTM

close() resets the statement sooner and adds no per-row work.


📝 Documentation: ✅ LGTM

The updated comment in iteratorImpl describes the current behaviour. The generated type snapshots match the source annotations.


Reviewed commit: 669c2023

@alsuren
alsuren requested a review from jclee November 20, 2026 11:53
Comment thread src/workerd/api/sql.c++ Resolved
Comment thread src/workerd/api/sql.h Resolved
Comment thread src/workerd/api/sql.c++ Resolved
@alsuren
alsuren force-pushed the dlaban/sql-cursor-close branch from 669c202 to 8d5c982 Compare November 20, 2026 21:35

@clanky-the-clanker clanky-the-clanker Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Superseded by the review of ca43afb9.

This review, of 8d5c9824

✅ Approved — Since the last review, close() throws from the wrong Durable Object and asserts that the statement state is cleared. Both are correct and tested, and no issues were found.

Not opened by the correctness review: 2 changed files it was meant to read
  • types/generated-snapshot/experimental/index.ts
  • types/generated-snapshot/index.d.ts

The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.


✅ All checks passed

🔒 Security: ✅ LGTM

No security issues found.


📖 AGENTS.md: ✅ LGTM

No AGENTS.md issues found.


📋 Code Quality: ✅ LGTM

Cursor::close() now calls endQuery(*s) and then KJ_DASSERT(state == kj::none). Dereferencing the IoOwn throws when the call comes from another Durable Object, and closeOtherCursor tests that case. endQuery clears state, so the assertion holds. Leaving columnNames for garbage collection was already agreed in the discussion.


⚡ Performance: ✅ LGTM

close() resets the statement sooner and adds no per-row work.


📝 Documentation: ✅ LGTM

The updated iteratorImpl comment describes the current behaviour, and the generated type snapshots match the source annotations.


Reviewed commit: 8d5c9824

@alsuren
alsuren force-pushed the dlaban/sql-cursor-close branch from 8d5c982 to ca43afb Compare November 21, 2026 09:43

@clanky-the-clanker clanky-the-clanker Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Superseded by the review of ba1cf2d2.

This review, of ca43afb9

✅ Approved — No issues found: close() correctly ends the query, is idempotent, throws from the wrong Durable Object, and is fully tested; types match the source annotations.

Not opened by the correctness review: 2 changed files it was meant to read
  • types/generated-snapshot/experimental/index.ts
  • types/generated-snapshot/index.d.ts

The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.


✅ All checks passed

🔒 Security: ✅ LGTM

Calling from another Durable Object throws when the IoOwn is dereferenced. The change adds no injection, authorization or data-exposure risk.


📖 AGENTS.md: ✅ LGTM

No AGENTS.md issues found.


📋 Code Quality: ✅ LGTM

close() ends an active query through endQuery, does nothing on a cursor with no active query, and never steps the statement. The tests cover partial iteration, using with break, throw and generators, calling close more than once, row counts and column names kept after close, statement reuse, wrong-Durable-Object calls and SuppressedError. The tail span counts match the new calls.


⚡ Performance: ✅ LGTM

close() releases the statement sooner and adds no per-row work.


📝 Documentation: ✅ LGTM

The updated iteratorImpl comment describes the current behaviour. All four generated type snapshots match the source annotations.


Reviewed commit: ca43afb9

@alsuren
alsuren force-pushed the dlaban/sql-cursor-close branch from ca43afb to ba1cf2d Compare November 21, 2026 09:48

@clanky-the-clanker clanky-the-clanker Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Superseded by the review of 6bf27f03.

This review, of ba1cf2d2

✅ Approved — No issues found: close() correctly ends the query, is idempotent, throws from the wrong Durable Object, and is fully tested; types match the source annotations.

Not opened by the correctness review: 2 changed files it was meant to read
  • types/generated-snapshot/experimental/index.ts
  • types/generated-snapshot/index.d.ts

The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.


✅ All checks passed

🔒 Security: ✅ LGTM

Calling from another Durable Object throws when the IoOwn is dereferenced. The change adds no injection, authorization or data-exposure risk.


📖 AGENTS.md: ✅ LGTM

No AGENTS.md issues found.


📋 Code Quality: ✅ LGTM

close() ends an active query through endQuery, does nothing on a cursor with no active query, and never steps the statement. The tests cover partial iteration, using with break, throw and generators, calling close more than once, row counts and column names kept after close, statement reuse, wrong-Durable-Object calls and SuppressedError. The tail span counts match the new calls.


⚡ Performance: ✅ LGTM

close() releases the statement sooner and adds no per-row work.


📝 Documentation: ✅ LGTM

The updated iteratorImpl comment describes the current behaviour. All four generated type snapshots match the source annotations.


Reviewed commit: ba1cf2d2 — same changes as ca43afb9, so this is that commit's review (no new review was run; line numbers are as of ca43afb9)

@github-actions

github-actions Bot commented Nov 21, 2026 •

Copy link
Copy Markdown

The generated output of @cloudflare/workers-types matches the snapshot in types/generated-snapshot 🎉

A cursor resets its SQLite statement only when it reaches its last
row or when V8 collects it. An application that stops reading part
way leaves the statement active on the Durable Object's connection,
and while it is, SQLite rejects every DROP TABLE and DROP INDEX with
SQLITE_LOCKED. The only way out was to call one() and ignore its
error, which steps the statement once more: after a step has failed,
that runs the statement again from the start.

close() ends the query without stepping it: it resets the statement,
returns it to the statement cache and keeps rowsRead, rowsWritten and
columnNames. A closed cursor reads as one that has run to its end.
close() is idempotent. Like the cursor's other methods, it throws when
called from a Durable Object other than the one that created the
cursor; under `using`, that error arrives as a SuppressedError that
still carries the original one.

JSG_DISPOSE(close) makes the cursor work with `using`, so that
leaving the block by break, return, throw or a generator's return()
closes it. Both are opt in, so existing code is unaffected.
@alsuren
alsuren force-pushed the dlaban/sql-cursor-close branch from ba1cf2d to 6bf27f0 Compare November 23, 2026 08:28

@clanky-the-clanker clanky-the-clanker Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

✅ Approved — No issues found: close() correctly ends the query, is idempotent, throws from the wrong Durable Object, and is fully tested; types match the source annotations.

Not opened by the correctness review: 2 changed files it was meant to read
  • types/generated-snapshot/experimental/index.ts
  • types/generated-snapshot/index.d.ts

The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.


✅ All checks passed

🔒 Security: ✅ LGTM

Calling from another Durable Object throws when the IoOwn is dereferenced. The change adds no injection, authorization or data-exposure risk.


📖 AGENTS.md: ✅ LGTM

No AGENTS.md issues found.


📋 Code Quality: ✅ LGTM

close() ends an active query through endQuery, does nothing on a cursor with no active query, and never steps the statement. The tests cover partial iteration, using with break, throw and generators, calling close more than once, row counts and column names kept after close, statement reuse, wrong-Durable-Object calls and SuppressedError. The tail span counts match the new calls.


⚡ Performance: ✅ LGTM

close() releases the statement sooner and adds no per-row work.


📝 Documentation: ✅ LGTM

The updated iteratorImpl comment describes the current behaviour. All four generated type snapshots match the source annotations.


Reviewed commit: 6bf27f03 — same changes as ca43afb9, so this is that commit's review (no new review was run; line numbers are as of ca43afb9)

@codecov-commenter

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 18.18182% with 9 lines in your changes missing coverage. Please review.
✅ Project coverage is 67.73%. Comparing base (1adff5d) to head (6bf27f0).

Files with missing lines Patch % Lines
src/workerd/api/sql.c++ 0.00% 6 Missing ⚠️
src/workerd/api/sql.h 40.00% 0 Missing and 3 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #8692      +/-   ##
==========================================
- Coverage   68.01%   67.73%   -0.28%     
==========================================
  Files         846      846              
  Lines      219198   215006    -4192     
  Branches    32587    32591       +4     
==========================================
- Hits       149081   145635    -3446     
+ Misses      50876    50128     -748     
- Partials    19241    19243       +2     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@petebacondarwin
petebacondarwin removed the request for review from NuroDev November 23, 2026 10:04
@alsuren
alsuren merged commit 479edd6 into main Nov 23, 2026
36 of 37 checks passed
@alsuren
alsuren deleted the dlaban/sql-cursor-close branch November 23, 2026 10:04
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.

5 participants