Skip to content

fix(telemetry): finalize statement telemetry on sync close - #299

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

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

Conversation

@jay-xiao446

@jay-xiao446 jay-xiao446 commented Aug 26, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

  • Finalize sync ExecutedStatement result-stream telemetry during explicit close so partial fetch + close emits the terminal EXECUTE_STATEMENT event.
  • Route PyO3 and NAPI sync executed-statement close through the concrete mutable close path.
  • Add a regression test for execute -> fetch one batch -> close -> session close telemetry export.

RCA

Covers RCA 50: Python kernel sync execute with partial fetch and early cursor close emitted session lifecycle telemetry but missed statement telemetry. The explicit sync close contract now finalizes result telemetry instead of relying on drop-time finalization.

Stack

Flow

sequenceDiagram
    participant Py as Python Cursor.close()
    participant Pyo3 as PyO3 ExecutedStatement.close()
    participant Rust as Rust ExecutedStatement::close(&mut self)
    participant Stream as ResultStreamTelemetry
    participant Server as Statement Execution API
    participant Exporter as Telemetry exporter

    Py->>Pyo3: close active KernelResultSet
    Pyo3->>Rust: concrete mutable close
    Rust->>Stream: finish(reader.metrics(), None)
    Stream->>Exporter: enqueue EXECUTE_STATEMENT once
    Rust->>Server: CloseStatement
Loading

Tests

  • cargo +stable fmt --all -- --check
  • cargo test statement_close_after_partial_fetch_exports_terminal_statement_event --lib --locked
  • cargo test telemetry --lib --locked
  • cd pyo3 && PYO3_PYTHON=/usr/bin/python3.10 cargo check --locked
  • cd napi && cargo check --locked

@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 Low

Looks good — correct, well-tested fix. Telemetry finalization is idempotent (result_stream finalized flag + observer finalized.swap), so the new explicit-close finalization and the drop-time backstop can't double-emit, and the closed flag prevents redundant CloseStatement RPCs. One low-severity maintainability note about the two same-named close methods diverging in behavior.

Scope: reviewed as a stacked increment (122e56f..head); the parent's changes are reviewed on the parent PR.

Comment thread src/statement/executed.rs
/// Explicit close for concrete sync statements.
///
/// This finalizes result-stream telemetry before the server-side
/// `CloseStatement` RPC so partial-result closes emit the same terminal

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🔵 Low — ExecutedStatement now has two identically-named close methods with divergent behavior: the inherent close(&mut self) (this line) finalizes result-stream telemetry before CloseStatement, while the trait impl ExecutedStatementHandle::close(&self) (executed.rs:339) only calls close_server_statement() and relies on drop-time finalization. Method resolution silently picks one based on the receiver's mutability/import context — which is exactly why the pyo3/napi call sites had to be rewritten to fully-qualified Type::close(&mut ...) form to hit the finalizing path.

This is a maintenance footgun: a future caller that acquires the handle behind the trait object (or an &self binding) will get the non-finalizing path and lose the "terminal statement event emitted before close" guarantee this PR establishes — the event would still fire at Drop, but the ordering/timing contract in the inherent method's doc comment would be quietly broken. Consider a doc note on the trait impl pointing at the inherent method, or (better) unifying so both paths finalize telemetry. Not a correctness bug today since Drop backstops finalization and the two shipped call sites are correct.

@jay-xiao446

Copy link
Copy Markdown
Collaborator Author

RCA: sync statement close missed statement telemetry

Symptom

Python kernel sync execute with partial result consumption and early Cursor.close() completed successfully, but telemetry only emitted session lifecycle events:

  • CREATE_SESSION
  • DELETE_SESSION

The expected EXECUTE_STATEMENT row for the server statement id was missing. The equivalent Python Thrift flow emitted the statement row, so this was a kernel telemetry parity gap rather than a test expectation issue.

Repro flow

sequenceDiagram
    participant App as Python app
    participant Cursor as Cursor
    participant Kernel as PyO3 kernel handle
    participant Exec as Rust ExecutedStatement
    participant Stream as ResultStreamTelemetry

    App->>Cursor: execute SELECT
    Cursor->>Kernel: sync execute
    Kernel->>Exec: create ExecutedStatement + ResultStream
    App->>Cursor: fetch first batch only
    Cursor->>Stream: records partial result metrics
    App->>Cursor: close()
    Cursor->>Kernel: ExecutedStatement.close()
    Kernel->>Exec: close server statement
    Note over Exec,Stream: Before this PR, telemetry finalization relied on drop
    Note over App,Stream: Result: statement event could be missing
Loading

Root cause

The sync ExecutedStatement owns the ResultStream, and the ResultStream owns statement telemetry finalization. However, explicit sync close only closed the server-side statement. It did not make result-stream telemetry finalization part of the explicit close contract.

For early close after partial fetch, the stream never reaches natural exhaustion, so ResultStream::next_batch() does not finalize telemetry. The previous behavior relied on drop-time finalization, which is too indirect for an explicit Cursor.close() / statement close CUJ.

Fix

This PR makes concrete sync statement close explicitly finish result-stream telemetry before closing the server statement:

sequenceDiagram
    participant Py as Python Cursor.close()
    participant Pyo3 as PyO3 ExecutedStatement.close()
    participant Rust as Rust ExecutedStatement::close(&mut self)
    participant Stream as ResultStreamTelemetry
    participant Server as Statement Execution API
    participant Exporter as Telemetry exporter

    Py->>Pyo3: close active KernelResultSet
    Pyo3->>Rust: concrete mutable close
    Rust->>Stream: finish(reader.metrics(), None)
    Stream->>Exporter: enqueue EXECUTE_STATEMENT once
    Rust->>Server: CloseStatement
Loading

The mutable concrete close is used by PyO3 and NAPI sync executed-statement close paths because those wrappers own the concrete ExecutedStatement. The existing shared trait close remains server-close-only to preserve the current ExecutedStatementHandle shared-reference contract.

Regression coverage

Added a Rust regression test for:

  1. Enable telemetry with a capturing exporter.
  2. Execute a statement returning inline results.
  3. Fetch one batch.
  4. Explicitly close the sync executed statement.
  5. Close the session.
  6. Assert exactly one EXECUTE_STATEMENT telemetry event exists with the expected statement id and result latency.

Local validation

  • cargo +stable fmt --all -- --check
  • cargo test statement_close_after_partial_fetch_exports_terminal_statement_event --lib --locked
  • cargo test telemetry --lib --locked
  • cd pyo3 && PYO3_PYTHON=/usr/bin/python3.10 cargo check --locked
  • cd napi && cargo check --locked

Signed-off-by: Jay Xiao <jay.xiao@databricks.com>
@jay-xiao446
jay-xiao446 added this pull request to the merge queue Aug 28, 2026

@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 Low

Looks good — the fix is correct and well-tested. finish_telemetry uses consumed=false consistent with the drop path, the finalized guard makes the close-then-drop double-finalize a no-op (test asserts exactly one event), the test genuinely exercises the partial-fetch path (one batch fetched, end-of-stream never observed), and PyO3/NAPI correctly route through the new inherent &mut close. One low-severity note: the trait ExecutedStatementHandle::close(&self) cannot finalize telemetry, so it diverges from the concrete close for dyn-based consumers.

Comment thread src/statement/executed.rs
}
.instrument(self.session.log_span().clone())
.await
self.close_server_statement().await

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🔵 Low — The new inherent ExecutedStatement::close(&mut self) finalizes result-stream telemetry (finish_telemetry()) before the server close, but the trait method ExecutedStatementHandle::close(&self) only calls close_server_statement() — it structurally cannot finalize telemetry (it takes &self, while ResultStream::finish_telemetry needs &mut self).

The two in-tree explicit-close call sites (PyO3, NAPI) now route through the concrete &mut path, so they're fixed. But ExecutedStatementHandle is re-exported publicly (lib.rs:146), so a Rust consumer closing via Arc<dyn ExecutedStatementHandle> still gets the pre-fix behavior: the terminal EXECUTE_STATEMENT event is deferred to drop-time finalization rather than emitted on explicit close — exactly the reliability gap this PR closes for the binding paths.

Not a regression (the trait method's behavior is unchanged) and drop-time finalization remains a backstop, so severity is low. Worth a doc note on the trait close pointing callers who want the explicit-close telemetry guarantee at the concrete &mut close, so the divergence isn't a silent footgun.

(Anchored to the nearest changed line — see the description for the exact location.)

Merged via the queue into main with commit a1c3cc7 Aug 28, 2026
27 checks passed

This branch was previously deployed

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