fix(csharp): auto-close server operation when result stream is exhausted#351
Closed
msrathore-db wants to merge 1 commit intomainfrom
Closed
fix(csharp): auto-close server operation when result stream is exhausted#351msrathore-db wants to merge 1 commit intomainfrom
msrathore-db wants to merge 1 commit intomainfrom
Conversation
When ReadNextRecordBatchAsync returns null (all results consumed), send TCloseOperationReq to the server immediately instead of waiting for Dispose(). Power BI's M engine has no deterministic disposal — it relies on GC to clean up IArrowArrayStream objects. Without a finalizer or auto-close, server commands stay open for ~22 min (CommandInactivityTimeout), blocking warehouse autostop even when no queries are running. Changes: - Send CloseOperation via CloseOperationBestEffort() when results are exhausted (covers Table.Buffer, schema inference, metadata calls) - Add finalizer as safety net for abandoned readers never read - Add _operationClosed flag to prevent duplicate TCloseOperationReq - Make Dispose() idempotent with exhaustion-triggered close Tested with A/B repro against warehouse with 5-min auto-stop: - Without fix: warehouse stayed running 10+ min past last query - With fix: warehouse auto-stopped within timeout Co-authored-by: Isaac
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes abandoned PowerBI result fetches that prevent SQL warehouse auto-stop. Affects ~5,000 warehouses daily, wasting ~30,000 compute-hours/day fleet-wide.
Root cause:
DatabricksCompositeReaderonly sendsTCloseOperationReqinsideDispose(). Power BI's M engine (Power Query) has nousingblocks or deterministic disposal — it relies on GC to clean upIArrowArrayStreamobjects. Since the reader has no finalizer, GC collects it silently without closing the server command. Commands stay open for ~22 min (CommandInactivityTimeout), and the autostop scheduler seesHasOnClusterOrSchedulingCommand=true, blocking warehouse shutdown.Fix (single file:
DatabricksCompositeReader.cs):ReadNextRecordBatchAsyncreturns null, sendTCloseOperationReqimmediately viaCloseOperationBestEffort(). This covers the dominant PowerBI pattern (Table.Buffer()reads all results).~DatabricksCompositeReader()for readers abandoned without any reads (e.g., cancelled previews)._operationClosedflag prevents duplicateTCloseOperationReqwhen both auto-close andDispose()fire.Test plan
Verified with A/B repro against warehouse
00adc7b6c00429b8(5-min auto-stop timeout):Repro tool at
csharp/tools/AbandonedResultRepro/— executes queries, reads all results without callingDispose(), holds process alive 10 min.This pull request was AI-assisted by Isaac.