Skip to content

fix(session): non-owning attached async handles + tighten http_error envelope - #147

Merged
vikrantpuppala merged 1 commit into
mainfrom
feat/attach-nonowning-followup
Jun 8, 2026
Merged

vikrantpuppala merged 1 commit into
mainfrom
feat/attach-nonowning-followup

Conversation

@vikrantpuppala

Copy link
Copy Markdown
Collaborator

What

Follow-up to #139 addressing @gopalldb's review. #139's attach_async_statement was documented as a read-only adopt, but two paths could still tear down the statement its owner controls, and the new http_error envelope parsing was looser than the statement-level path.

P1 — an attached handle must never tear down the owner's statement

  • Add an owning flag to ExecutedAsyncStatement — true for the submitting handle (Statement::submit), false for attached handles (new new_attached constructor used by attach_async_statement).
  • await_result() only arms the drop-cancel guard when owning. Before, a non-owning handle whose await future was dropped mid-poll (the canonical tokio::time::timeout pattern) would fire cancel_statement against the owner's query. Now a passive observer can safely timeout(attach(id).await_result()) without cancelling someone else's statement. The owning submit path keeps its DBU-safety drop-cancel behaviour unchanged.
  • cancel() errors on a non-owning handle (and so does the derived AsyncStatementCanceller::cancel() — owning is inherited so the footgun doesn't just move to the detached canceller). Returns InvalidArgument rather than firing a server-side cancel against a statement the caller doesn't own.

P1 — http_error SQLSTATE validation

  • Require the canonical 5-char SQLSTATE (len() == 5) before attaching it, matching sea.rs terminal_outcome. Was !is_empty(), which let junk ("X", "garbage") onto Error.sql_state.

P2

  • attach_async_statement stamps the statement id onto the NotFound error (with_query_id), matching terminal_outcome.
  • http_error parses the body once into serde_json::Value, then tries both envelope shapes off it (was two full string parses).
  • When the envelope carries an error_code but no message, synthesize "server returned error_code <ec>" instead of folding the raw JSON body into the human-readable message.

Testing

  • attached_handle_await_result_drop_does_not_cancel_statement — a dropped await on an attached handle fires zero cancels (the existing await_result_drop_fires_cancel_statement pins the owning contrast).
  • attached_handle_cancel_errors — cancel() and the derived canceller both error on an attached handle, with zero cancels on the wire.
  • Existing test_http_error_parses_sea_error_envelope / _falls_back_to_raw still pass with the tightened SQLSTATE + single-parse changes.
  • cargo +stable fmt --all, cargo clippy --all-targets -- -D warnings, full cargo test — all green.

Deferred (its own follow-up)

Review item: list_table_types() advertises "SYSTEM TABLE" (space) while SHOW TABLES emits "SYSTEM_TABLE" (underscore), so a round-trip of the canonical list into list_tables(table_types=...) for system tables may not match. Reconciling the advertised list needs a live system-table fixture to confirm the exact server casing (a dogfood probe over system / samples surfaced only TABLE / VIEW, no system-table row). Out of scope here — tracked as a separate investigation-first follow-up, as the reviewer suggested.

…ttp_error envelope

Addresses review feedback on #139 (attach_async_statement). The
attached handle was documented as a read-only adopt but still had
paths that could tear down the statement its owner controls.

P1 — attach must not tear down the owner's statement:
- Add an `owning` flag to ExecutedAsyncStatement (true for submit,
  false for attach via the new `new_attached` constructor).
- `await_result()` only arms the drop-cancel guard when `owning`. A
  non-owning handle whose await future is dropped mid-poll (e.g. a
  passive observer wrapping it in `tokio::time::timeout`) no longer
  fires `cancel_statement` against the owner's query. The owning
  submit path keeps the DBU-safety drop-cancel behaviour.
- `ExecutedAsyncStatement::cancel()` and the derived
  `AsyncStatementCanceller::cancel()` error (InvalidArgument) on a
  non-owning handle instead of cancelling a statement the caller
  doesn't own. `owning` is inherited by the canceller so the footgun
  doesn't just move.

P1 — http_error SQLSTATE validation:
- Require the canonical 5-char SQLSTATE shape (`len() == 5`) before
  attaching it, matching `terminal_outcome` — was `!is_empty()`, which
  let junk like "X" through onto Error.sql_state.

P2:
- attach_async_statement stamps the statement id onto the NotFound
  error (with_query_id), matching terminal_outcome.
- http_error parses the body once into serde_json::Value, then tries
  both envelope shapes off it (was two full string parses).
- When the envelope has an error_code but no message, synthesize
  "server returned error_code <ec>" instead of folding the raw JSON
  body into the human-readable message.

Tests:
- attached_handle_await_result_drop_does_not_cancel_statement: a
  dropped await on an attached handle fires zero cancels (the owning
  await_result_drop_fires_cancel_statement pins the contrast).
- attached_handle_cancel_errors: cancel() and the derived canceller
  both error on an attached handle, with zero cancels on the wire.

Deferred (its own follow-up — needs a live system-table fixture to
verify the exact server casing): list_table_types() advertises
"SYSTEM TABLE" (space) while SHOW TABLES emits "SYSTEM_TABLE"
(underscore); reconciling the canonical list is out of scope here.

Co-authored-by: Isaac
Signed-off-by: Vikrant Puppala <vikrant.puppala@databricks.com>
@vikrantpuppala
vikrantpuppala added this pull request to the merge queue Jun 8, 2026
Merged via the queue into main with commit 9e267bb Jun 8, 2026
15 checks passed

This branch was previously deployed

1 inactive deployment
azure-prod — d4dc86db Deployed Jun 8, 2026 by vikrantpuppala via v0 TLS (mitmproxy) #151
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.

2 participants