Repository navigation
fix(telemetry): finalize statement telemetry on sync close - #299
Conversation
There was a problem hiding this comment.
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.
| /// 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 |
There was a problem hiding this comment.
🔵 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.
RCA: sync statement close missed statement telemetrySymptomPython kernel sync execute with partial result consumption and early
The expected Repro flowsequenceDiagram
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
Root causeThe sync For early close after partial fetch, the stream never reaches natural exhaustion, so FixThis 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
The mutable concrete close is used by PyO3 and NAPI sync executed-statement close paths because those wrappers own the concrete Regression coverageAdded a Rust regression test for:
Local validation
|
Signed-off-by: Jay Xiao <jay.xiao@databricks.com>
48e7108 to
3853a67
Compare
There was a problem hiding this comment.
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.
| } | ||
| .instrument(self.session.log_span().clone()) | ||
| .await | ||
| self.close_server_statement().await |
There was a problem hiding this comment.
🔵 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.)
Summary
ExecutedStatementresult-stream telemetry during explicit close so partial fetch + close emits the terminalEXECUTE_STATEMENTevent.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
feat(telemetry): add circuit breaker)mainbut is based on Phase 8 for stacked validation.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: CloseStatementTests
cargo +stable fmt --all -- --checkcargo test statement_close_after_partial_fetch_exports_terminal_statement_event --lib --lockedcargo test telemetry --lib --lockedcd pyo3 && PYO3_PYTHON=/usr/bin/python3.10 cargo check --lockedcd napi && cargo check --locked