Skip to content

fix(telemetry): finalize async statement telemetry on close - #306

Merged
jay-xiao446 merged 1 commit into
mainfrom
fix/telemetry-async-close-finalize
Aug 28, 2026
Merged

jay-xiao446 merged 1 commit into
mainfrom
fix/telemetry-async-close-finalize

Conversation

@jay-xiao446

Copy link
Copy Markdown
Collaborator

Summary

Fix async partial-fetch telemetry finalization at the kernel owning-handle close boundary, mirroring the sync ExecutedStatement::close(&mut self) pattern from #299.

When await_result() / fetch_result() returns a ResultStream, success telemetry ownership moves to that stream so full result consumption can include result metrics. Node async partial-fetch flows can keep that result stream alive past op.close() and session.close(), so the terminal EXECUTE_STATEMENT_ASYNC row may miss the final telemetry flush.

This PR adds a concrete ExecutedAsyncStatement::close(&mut self) backstop that finalizes statement telemetry before closing the server statement. The immutable ExecutedStatementHandle::close(&self) remains server-close only.

RCA

Addresses RCA 29: Node async partial result fetching emitted CREATE_SESSION and DELETE_SESSION, but missed the EXECUTE_STATEMENT_ASYNC row because the returned async result stream was still live when the parent async statement/session closed.

Changes

  • Add concrete ExecutedAsyncStatement::close(&mut self) that finalizes statement telemetry and then closes the server statement.
  • Route NAPI AsyncStatement.close() through the concrete async close.
  • Route PyO3 ExecutedAsyncStatement.close() through the concrete async close for consistency.
  • Add regression coverage for partial async result fetch where the ResultStream remains live across async statement close and session close.

Notes

This guarantees the terminal async statement row at the owning async statement close boundary. If the partial ResultStream is still live, close cannot pull final reader chunk metrics from that stream; stream exhaustion/drop still records reader metrics when it wins the finalization race.

Testing

cargo fmt --all -- --check
cargo clippy --all-targets --locked -- -D warnings
cargo test --lib --locked
cargo test --lib --locked telemetry
cargo test --lib --locked session::is_open_tests::async_statement
cargo test --manifest-path napi/Cargo.toml --locked --features dyn-symbols async_statement
PYO3_PYTHON=python3.11 cargo test --manifest-path pyo3/Cargo.toml --locked --no-default-features

Signed-off-by: Jay Xiao <jay.xiao@databricks.com>

@oss-reviewer-bot oss-reviewer-bot 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.

Verdict: 1 Medium

Looks good — the async close backstop faithfully mirrors the sync ExecutedStatement::close pattern, finalize is idempotent (shared finalized flag prevents double-emit with the live ResultStream), and the napi/pyo3 bindings correctly force the inherent method via UFCS. One medium note: the finalizing close(&mut self) is shadowed by the trait's close(&self) under normal method syntax and unreachable via dyn ExecutedStatementHandle, so trait-in-scope Rust callers and the C ABI still bypass the telemetry finalize.

/// async statement is the binding-visible lifecycle boundary, though, so finalize
/// here as a deterministic backstop for partial-fetch callers that keep the stream
/// alive past session close.
pub async fn close(&mut self) -> Result<()> {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 Medium — The new finalizing close(&mut self) is only reached via UFCS or when the ExecutedStatementHandle trait is out of scope — otherwise the non-finalizing trait close(&self) silently wins, reintroducing the RCA-29 telemetry gap for those callers.

Rust method resolution tries the &self receiver candidate before &mut self. When ExecutedStatementHandle is in scope, exec.close() finds the trait's close(&self) at the &Self step and selects it (server-close only, no finalize) — the inherent &mut self method is never considered. The napi and pyo3 bindings dodge this correctly with UFCS (ExecutedAsyncStatement::close(&mut …)), and the new session.rs test works only because that test module keeps the trait out of scope and binds mut. But:

  • A direct Rust consumer that does use databricks_sql_kernel::ExecutedStatementHandle; (needed for cancel()) and then calls exec.close() gets the non-finalizing close — the exact partial-fetch telemetry loss this PR sets out to fix.
  • The C ABI closes through Box<dyn ExecutedStatementHandle> (per src/statement/handle.rs:32), which can only dispatch to close(&self), so async statements closed via the trait object never hit the new backstop.

In those paths telemetry still eventually flushes when the ResultStream drops, so this is a determinism regression rather than guaranteed loss, and it mirrors the pre-existing sync pattern. Worth a doc note on the inherent method ("trait-object / trait-in-scope callers get the non-finalizing close; use UFCS") so the guarantee isn't silently lost by a future caller.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

It's okay for now, all 3 drivers pass the telemetry emitted on statement closure test. How they work may not be the same but it's good for now for PuPr.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Agreed the tests passing across all three drivers is a reasonable bar for PuPr, and this was always flagged as a determinism regression rather than guaranteed loss — not a blocker. The reason I'm keeping it open is that "how they work may not be the same" is exactly the trap: the trait-in-scope and Box<dyn ExecutedStatementHandle> paths still resolve to the non-finalizing close(&self), and nothing in the current code records that. A one-line doc note on the inherent close(&mut self) ("trait-object / trait-in-scope callers get the non-finalizing close; use UFCS to finalize") would preserve the guarantee for the next caller at effectively zero cost. Happy to leave it as a post-PuPr follow-up rather than a merge gate.

@jay-xiao446
jay-xiao446 added this pull request to the merge queue Aug 28, 2026
Merged via the queue into main with commit e8fe052 Aug 28, 2026
30 checks passed

This branch was previously deployed

1 inactive deployment
azure-prod — de87dc97 Deployed Aug 28, 2026 by oss-reviewer-bot[bot] via followup #1242
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