From d1623cfcdff0c6a2cc800522c2a48d86965770fb Mon Sep 17 00:00:00 2001 From: Vu Anh Phung Date: Wed, 11 Nov 2026 22:59:20 +0000 Subject: [PATCH 1/5] feat: support Symbol.asyncDispose on client, session, and operation DBSQLClient, DBSQLSession, and DBSQLOperation (and the IDBSQLClient, IDBSQLSession, and IOperation interfaces) gain [Symbol.asyncDispose](), which delegates to close(), so `await using` works on both backends. Disposing an already-closed object is a no-op. The published typings declare SymbolConstructor.asyncDispose themselves, so consumers don't need TypeScript's esnext.disposable lib or a recent @types/node. Fixes #331 Signed-off-by: Vu Anh Phung Co-authored-by: Isaac --- CHANGELOG.md | 1 + README.md | 17 +++++++++++ lib/DBSQLClient.ts | 7 +++++ lib/DBSQLOperation.ts | 8 +++++ lib/DBSQLSession.ts | 8 +++++ lib/contracts/IDBSQLClient.ts | 6 ++++ lib/contracts/IDBSQLSession.ts | 6 ++++ lib/contracts/IOperation.ts | 6 ++++ lib/contracts/asyncDispose.ts | 9 ++++++ tests/unit/.stubs/OperationStub.ts | 4 +++ tests/unit/DBSQLClient.test.ts | 43 +++++++++++++++++++++++++++ tests/unit/DBSQLOperation.test.ts | 43 +++++++++++++++++++++++++++ tests/unit/DBSQLSession.test.ts | 45 +++++++++++++++++++++++++++++ tests/unit/kernel/execution.test.ts | 38 ++++++++++++++++++++++++ 14 files changed, 241 insertions(+) create mode 100644 lib/contracts/asyncDispose.ts diff --git a/CHANGELOG.md b/CHANGELOG.md index 77d44b38..df14e77f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ## Unreleased +- `DBSQLClient`, sessions, and operations now implement `Symbol.asyncDispose`, so `await using` closes them automatically; disposing an already-closed object is a no-op. Custom implementations of the `IDBSQLClient`, `IDBSQLSession`, or `IOperation` interfaces must add this method. (#331) - CloudFetch retry-exhaustion errors now identify the download and the last failure: the `RetryError` message reads `CloudFetch result download failed: (last error: )`. `` is `HTTP : ` (or `HTTP ` when the status text is empty) for HTTP failures, and `network error `, `network error ()` (e.g. `aborted`, when there is no `code`) or a bare `network error` for network failures (the raw network-error message, which can contain the presigned URL, is not copied). If no cause can be determined, the ` (last error: …)` suffix is omitted. The `RetryError` instance, `errorCode` and `payload` (including the node-fetch `Request` / `Response`) are unchanged. (#471) - Report untyped NULL columns as STRING in kernel result metadata, matching Thrift. (#470) - Reject non-scalar query parameter values at bind time with a `ParameterError` on both the Thrift and kernel backends, instead of stringifying them (which could leave a Thrift operation stuck before FINISHED). Newly rejected on the Thrift backend: arrays, plain objects, boxed primitives (e.g. `new String('x')`, `new Number(1)`), functions, symbols, and a `DBSQLParameter` that wraps another `DBSQLParameter`. Primitives, `Date`, `Int64`, and `DBSQLParameter` are still accepted. diff --git a/README.md b/README.md index 638cbf78..0b99c326 100644 --- a/README.md +++ b/README.md @@ -51,6 +51,23 @@ client }); ``` +### Automatic cleanup with `await using` + +The client, sessions, and operations implement `Symbol.asyncDispose`, so an `await using` declaration closes them when +it goes out of scope, even if an error is thrown (disposing an already-closed object is a no-op). Node.js 24+ supports +`await using` natively; on Node.js 22, compile it with TypeScript 5.2+. + +```javascript +async function main() { + await using client = new DBSQLClient(); + await client.connect({ host, path, token }); + + await using session = await client.openSession(); + await using operation = await session.executeStatement('SELECT "Hello, World!"'); + console.table(await operation.fetchAll()); +} // closes `operation`, `session`, then `client` +``` + ## Configuration See [CONNECTION_PARAMETERS.md](CONNECTION_PARAMETERS.md) for every connection, diff --git a/lib/DBSQLClient.ts b/lib/DBSQLClient.ts index ea381bf3..7ce9e128 100644 --- a/lib/DBSQLClient.ts +++ b/lib/DBSQLClient.ts @@ -950,6 +950,13 @@ export default class DBSQLClient extends EventEmitter implements IDBSQLClient, I this.authProvider = undefined; } + /** + * Closes the client (see {@link close}); called by `await using client = ...` + */ + public async [Symbol.asyncDispose](): Promise { + await this.close(); + } + public getConfig(): ClientConfig { return this.config; } diff --git a/lib/DBSQLOperation.ts b/lib/DBSQLOperation.ts index 1ec1922c..27cc68d0 100644 --- a/lib/DBSQLOperation.ts +++ b/lib/DBSQLOperation.ts @@ -233,6 +233,14 @@ export default class DBSQLOperation implements IOperation { return result; } + /** + * Closes operation (see {@link close}); called by `await using operation = ...` + * @throws {StatusError} + */ + public async [Symbol.asyncDispose](): Promise { + await this.close(); + } + public async finished(options?: FinishedOptions): Promise { return this.withErrorTelemetry(async () => { await this.failIfClosed(); diff --git a/lib/DBSQLSession.ts b/lib/DBSQLSession.ts index 18588ac3..f35358f0 100644 --- a/lib/DBSQLSession.ts +++ b/lib/DBSQLSession.ts @@ -365,6 +365,14 @@ export default class DBSQLSession implements IDBSQLSession { } } + /** + * Closes the session (see {@link close}); called by `await using session = ...` + * @public + */ + public async [Symbol.asyncDispose](): Promise { + await this.close(); + } + private wrapOperation(backend: IOperationBackend): DBSQLOperation { const operation = new DBSQLOperation({ backend, diff --git a/lib/contracts/IDBSQLClient.ts b/lib/contracts/IDBSQLClient.ts index 0a6f416b..009d319d 100644 --- a/lib/contracts/IDBSQLClient.ts +++ b/lib/contracts/IDBSQLClient.ts @@ -5,6 +5,7 @@ import { ProxyOptions } from '../connection/contracts/IConnectionOptions'; import OAuthPersistence from '../connection/auth/DatabricksOAuth/OAuthPersistence'; import ITokenProvider from '../connection/auth/tokenProvider/ITokenProvider'; import { TokenCallback } from '../connection/auth/tokenProvider/ExternalTokenProvider'; +import './asyncDispose'; export interface ClientOptions { logger?: IDBSQLLogger; @@ -289,4 +290,9 @@ export default interface IDBSQLClient { openSession(request?: OpenSessionRequest): Promise; close(): Promise; + + /** + * Closes the client, enabling `await using client = ...`. No-op if already closed + */ + [Symbol.asyncDispose](): Promise; } diff --git a/lib/contracts/IDBSQLSession.ts b/lib/contracts/IDBSQLSession.ts index e7eb31d8..17b373d9 100644 --- a/lib/contracts/IDBSQLSession.ts +++ b/lib/contracts/IDBSQLSession.ts @@ -3,6 +3,7 @@ import IOperation from './IOperation'; import Status from '../dto/Status'; import InfoValue from '../dto/InfoValue'; import { DBSQLParameter, DBSQLParameterValue } from '../DBSQLParameter'; +import './asyncDispose'; export type ExecuteStatementOptions = { /** @@ -233,4 +234,9 @@ export default interface IDBSQLSession { * closes the session */ close(): Promise; + + /** + * Closes the session, enabling `await using session = ...`. No-op if already closed + */ + [Symbol.asyncDispose](): Promise; } diff --git a/lib/contracts/IOperation.ts b/lib/contracts/IOperation.ts index bbeed622..3a1ce65a 100644 --- a/lib/contracts/IOperation.ts +++ b/lib/contracts/IOperation.ts @@ -2,6 +2,7 @@ import { Readable, ReadableOptions } from 'node:stream'; import { TGetOperationStatusResp, TTableSchema } from '../../thrift/TCLIService_types'; import Status from '../dto/Status'; import { ResultMetadata } from './ResultMetadata'; +import './asyncDispose'; export type OperationStatusCallback = (progress: TGetOperationStatusResp) => unknown; @@ -105,4 +106,9 @@ export default interface IOperation { iterateRows(options?: IteratorOptions): IOperationRowsIterator; toNodeStream(options?: NodeStreamOptions): Readable; + + /** + * Closes the operation, enabling `await using operation = ...`. No-op if already closed + */ + [Symbol.asyncDispose](): Promise; } diff --git a/lib/contracts/asyncDispose.ts b/lib/contracts/asyncDispose.ts new file mode 100644 index 00000000..43c82458 --- /dev/null +++ b/lib/contracts/asyncDispose.ts @@ -0,0 +1,9 @@ +// Declares the built-in `Symbol.asyncDispose` (identical to the declaration in TypeScript's +// `esnext.disposable` lib and in `@types/node`) so the published typings work without either. +declare global { + interface SymbolConstructor { + readonly asyncDispose: unique symbol; + } +} + +export {}; diff --git a/tests/unit/.stubs/OperationStub.ts b/tests/unit/.stubs/OperationStub.ts index 1dcac5ca..a56abbaa 100644 --- a/tests/unit/.stubs/OperationStub.ts +++ b/tests/unit/.stubs/OperationStub.ts @@ -69,4 +69,8 @@ export default class OperationStub implements IOperation { public toNodeStream(options?: NodeStreamOptions): Readable { throw new Error('Not implemented'); } + + public async [Symbol.asyncDispose]() { + await this.close(); + } } diff --git a/tests/unit/DBSQLClient.test.ts b/tests/unit/DBSQLClient.test.ts index 090d7ede..da4435cc 100644 --- a/tests/unit/DBSQLClient.test.ts +++ b/tests/unit/DBSQLClient.test.ts @@ -579,6 +579,49 @@ describe('DBSQLClient.close', () => { }); }); +describe('DBSQLClient[Symbol.asyncDispose]', () => { + it('should close the client and its sessions', async () => { + const client = new DBSQLClient(); + const thriftClient = sinon.spy(new ThriftClientStub()); + + client['client'] = thriftClient; + client['connectionProvider'] = new ConnectionProviderStub(); + client['authProvider'] = new AuthProviderStub(); + client['backend'] = new ThriftBackend({ context: client, onConnectionEvent: () => {} }); + + const session = await client.openSession(); + if (!(session instanceof DBSQLSession)) { + throw new Error('Assertion error: expected session to be DBSQLSession'); + } + const closeSpy = sinon.spy(client, 'close'); + + const result = await client[Symbol.asyncDispose](); + expect(result).to.be.undefined; + expect(closeSpy.calledOnce).to.be.true; + expect(session['isOpen']).to.be.false; + expect(thriftClient.CloseSession.calledOnce).to.be.true; + expect(client['backend']).to.be.undefined; + expect(client['client']).to.be.undefined; + }); + + it('should be a no-op for a closed or never-connected client', async () => { + const closedClient = new DBSQLClient(); + const thriftClient = sinon.spy(new ThriftClientStub()); + closedClient['client'] = thriftClient; + closedClient['connectionProvider'] = new ConnectionProviderStub(); + closedClient['authProvider'] = new AuthProviderStub(); + closedClient['backend'] = new ThriftBackend({ context: closedClient, onConnectionEvent: () => {} }); + await closedClient.openSession(); + + await closedClient.close(); + await closedClient[Symbol.asyncDispose](); + await closedClient[Symbol.asyncDispose](); + expect(thriftClient.CloseSession.calledOnce).to.be.true; + + await new DBSQLClient()[Symbol.asyncDispose](); + }); +}); + describe('DBSQLClient.createAuthProvider', () => { it('should use access token auth method', () => { const client = new DBSQLClient(); diff --git a/tests/unit/DBSQLOperation.test.ts b/tests/unit/DBSQLOperation.test.ts index f9195105..9caa7074 100644 --- a/tests/unit/DBSQLOperation.test.ts +++ b/tests/unit/DBSQLOperation.test.ts @@ -418,6 +418,49 @@ describe('DBSQLOperation', () => { }); }); + describe('Symbol.asyncDispose', () => { + it('should close the operation', async () => { + const context = new ClientContextStub(); + const driver = sinon.spy(context.driver); + const operation = createOperationForTest({ handle: operationHandleStub({ hasResultSet: true }), context }); + + const result = await operation[Symbol.asyncDispose](); + expect(result).to.be.undefined; + expect(operation['closed']).to.be.true; + expect(driver.closeOperation.callCount).to.be.equal(1); + }); + + it('should be a no-op for an already closed or cancelled operation', async () => { + const context = new ClientContextStub(); + const driver = sinon.spy(context.driver); + const closed = createOperationForTest({ handle: operationHandleStub({ hasResultSet: true }), context }); + const cancelled = createOperationForTest({ handle: operationHandleStub({ hasResultSet: true }), context }); + + await closed.close(); + await cancelled.cancel(); + await closed[Symbol.asyncDispose](); + await closed[Symbol.asyncDispose](); + await cancelled[Symbol.asyncDispose](); + expect(driver.closeOperation.callCount).to.be.equal(1); + }); + + it('should propagate close errors', async () => { + const context = new ClientContextStub(); + context.driver.closeOperationResp.status.statusCode = TStatusCode.ERROR_STATUS; + const operation = createOperationForTest({ handle: operationHandleStub({ hasResultSet: true }), context }); + + try { + await operation[Symbol.asyncDispose](); + expect.fail('It should throw a StatusError'); + } catch (e) { + if (e instanceof AssertionError) { + throw e; + } + expect(e).to.be.instanceOf(StatusError); + } + }); + }); + describe('finished', () => { [TOperationState.INITIALIZED_STATE, TOperationState.RUNNING_STATE, TOperationState.PENDING_STATE].forEach( (operationState) => { diff --git a/tests/unit/DBSQLSession.test.ts b/tests/unit/DBSQLSession.test.ts index ce782207..36ab26e9 100644 --- a/tests/unit/DBSQLSession.test.ts +++ b/tests/unit/DBSQLSession.test.ts @@ -760,4 +760,49 @@ describe('DBSQLSession', () => { ); }); }); + + describe('Symbol.asyncDispose', () => { + it('should close the session and its operations', async () => { + const context = new ClientContextStub(); + const driver = sinon.spy(context.driver); + const session = createSessionForTest({ handle: sessionHandleStub, context }); + const operation = await session.executeStatement('SELECT * FROM table'); + if (!(operation instanceof DBSQLOperation)) { + expect.fail('Assertion error: operation is not a DBSQLOperation'); + } + + const result = await session[Symbol.asyncDispose](); + expect(result).to.be.undefined; + expect(session['isOpen']).to.be.false; + expect(operation['closed']).to.be.true; + expect(driver.closeSession.callCount).to.eq(1); + }); + + it('should be a no-op for an already closed session', async () => { + const context = new ClientContextStub(); + const driver = sinon.spy(context.driver); + const session = createSessionForTest({ handle: sessionHandleStub, context }); + + await session.close(); + await session[Symbol.asyncDispose](); + await session[Symbol.asyncDispose](); + expect(driver.closeSession.callCount).to.eq(1); + }); + + it('should propagate close errors', async () => { + const context = new ClientContextStub(); + const session = createSessionForTest({ handle: sessionHandleStub, context }); + const error = new Error('close failed'); + sinon.stub(context.driver, 'closeSession').rejects(error); + + try { + await session[Symbol.asyncDispose](); + expect.fail('It should throw an error'); + } catch (e) { + expect(e).to.equal(error); + } + // A failed close still marks the session closed, so disposing again is a no-op. + await session[Symbol.asyncDispose](); + }); + }); }); diff --git a/tests/unit/kernel/execution.test.ts b/tests/unit/kernel/execution.test.ts index 73e37822..26581bc4 100644 --- a/tests/unit/kernel/execution.test.ts +++ b/tests/unit/kernel/execution.test.ts @@ -19,6 +19,8 @@ import expectNativeConnectionOptions from './_helpers/nativeOptions'; import KernelBackend from '../../../lib/kernel/KernelBackend'; import KernelSessionBackend from '../../../lib/kernel/KernelSessionBackend'; import KernelOperationBackend from '../../../lib/kernel/KernelOperationBackend'; +import DBSQLSession from '../../../lib/DBSQLSession'; +import DBSQLOperation from '../../../lib/DBSQLOperation'; import { KernelNativeBinding, KernelConnection, KernelStatement } from '../../../lib/kernel/KernelNativeLoader'; import IClientContext, { ClientConfig } from '../../../lib/contracts/IClientContext'; import { PrimaryKeysRequest } from '../../../lib/contracts/IDBSQLSession'; @@ -1643,3 +1645,39 @@ describe('KernelOperationBackend — sync (executeStatementCancellable) path', ( expect(status.errorDetailsJson).to.equal(null); }); }); + +describe('DBSQLSession / DBSQLOperation over kernel backends — Symbol.asyncDispose', () => { + function makeFacadeSession(connection: FakeNativeConnection) { + const context = makeContext(); + return new DBSQLSession({ backend: new KernelSessionBackend({ connection, context }), context }); + } + + it('disposing an operation closes the native statement; re-disposing is a no-op', async () => { + const connection = new FakeNativeConnection(); + const session = makeFacadeSession(connection); + const operation = await session.executeStatement('SELECT 1'); + if (!(operation instanceof DBSQLOperation)) { + expect.fail('Assertion error: operation is not a DBSQLOperation'); + } + const closeSpy = sinon.spy(connection.statementToReturn, 'close'); + + expect(await operation[Symbol.asyncDispose]()).to.equal(undefined); + expect(connection.statementToReturn.closed).to.equal(true); + await operation[Symbol.asyncDispose](); + expect(closeSpy.callCount).to.equal(1); + }); + + it('disposing a session closes its operations and the native connection; re-disposing is a no-op', async () => { + const connection = new FakeNativeConnection(); + const session = makeFacadeSession(connection); + const operation = await session.executeStatement('SELECT 1'); + const closeSpy = sinon.spy(connection, 'close'); + + expect(await session[Symbol.asyncDispose]()).to.equal(undefined); + expect(connection.closed).to.equal(true); + expect(connection.statementToReturn.closed).to.equal(true); + await session[Symbol.asyncDispose](); + await operation[Symbol.asyncDispose](); + expect(closeSpy.callCount).to.equal(1); + }); +}); From 02233d7de5255f6638a1030c2d69dab2160e22c3 Mon Sep 17 00:00:00 2001 From: "peco-engineer-bot[bot]" Date: Thu, 12 Nov 2026 08:40:53 +0000 Subject: [PATCH 2/5] engineer-bot: address @mention on PR #586 Signed-off-by: peco-engineer-bot[bot] Signed-off-by: Vu Anh Phung --- README.md | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 0b99c326..bf2bfd91 100644 --- a/README.md +++ b/README.md @@ -55,7 +55,10 @@ client The client, sessions, and operations implement `Symbol.asyncDispose`, so an `await using` declaration closes them when it goes out of scope, even if an error is thrown (disposing an already-closed object is a no-op). Node.js 24+ supports -`await using` natively; on Node.js 22, compile it with TypeScript 5.2+. +`await using` natively; on Node.js 22, compile it with TypeScript 5.2+. TypeScript also needs the disposable +declarations (such as the global `AsyncDisposable` type) to compile `await using`: if your typings don't already +provide them, add `"esnext.disposable"` (or `"esnext"`) to `compilerOptions.lib` in your `tsconfig.json`, otherwise the +example below can fail to compile even with TypeScript 5.2+. ```javascript async function main() { From ca5fa374d328d34a8b160af3850a8ac1aa013507 Mon Sep 17 00:00:00 2001 From: "peco-engineer-bot[bot]" Date: Thu, 12 Nov 2026 18:24:20 +0000 Subject: [PATCH 3/5] engineer-bot: address @mention on PR #586 Signed-off-by: peco-engineer-bot[bot] --- CHANGELOG.md | 2 + README.md | 6 +- lib/DBSQLClient.ts | 13 ++- lib/DBSQLOperation.ts | 6 +- lib/DBSQLSession.ts | 26 +++--- lib/utils/CloseableCollection.ts | 38 +++++++- tests/unit/DBSQLClient.test.ts | 88 +++++++++++++++++++ tests/unit/DBSQLOperation.test.ts | 26 ++++-- tests/unit/DBSQLSession.test.ts | 91 ++++++++++++++++++++ tests/unit/kernel/execution.test.ts | 17 ++++ tests/unit/utils/CloseableCollection.test.ts | 32 ++++++- 11 files changed, 320 insertions(+), 25 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 89917950..1994e779 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,8 @@ ## Unreleased - `DBSQLClient`, sessions, and operations now implement `Symbol.asyncDispose`, so `await using` closes them automatically; disposing an already-closed object is a no-op. Custom implementations of the `IDBSQLClient`, `IDBSQLSession`, or `IOperation` interfaces must add this method. (#331) +- `DBSQLSession.close()` and `DBSQLClient.close()` now attempt every cleanup step even if one fails: an operation that fails to close no longer prevents closing its sibling operations, the session's backend session, or the client's backend, feature-flag, and telemetry resources. A single failure is rethrown unchanged once cleanup completes; several failures are reported as an `AggregateError`. A client whose backend fails to close is now also reset, like a successfully closed one. +- Closing a cancelled operation now releases its backend handle (Thrift `CloseOperation` / kernel statement close) instead of being a no-op, and a cancelled operation stays tracked by its session until it is closed, so `session.close()` and `await using` release it too. - CloudFetch retry-exhaustion errors now identify the download and the last failure: the `RetryError` message reads `CloudFetch result download failed: (last error: )`. `` is `HTTP : ` (or `HTTP ` when the status text is empty) for HTTP failures, and `network error `, `network error ()` (e.g. `aborted`, when there is no `code`) or a bare `network error` for network failures (the raw network-error message, which can contain the presigned URL, is not copied). If no cause can be determined, the ` (last error: …)` suffix is omitted. The `RetryError` instance, `errorCode` and `payload` (including the node-fetch `Request` / `Response`) are unchanged. (#471) - Report untyped NULL columns as STRING in kernel result metadata, matching Thrift. (#470) - Reject non-scalar query parameter values at bind time with a `ParameterError` on both the Thrift and kernel backends, instead of stringifying them (which could leave a Thrift operation stuck before FINISHED). Newly rejected on the Thrift backend: arrays, plain objects, boxed primitives (e.g. `new String('x')`, `new Number(1)`), functions, symbols, and a `DBSQLParameter` that wraps another `DBSQLParameter`. Primitives, `Date`, `Int64`, and `DBSQLParameter` are still accepted. diff --git a/README.md b/README.md index bf2bfd91..202be0f7 100644 --- a/README.md +++ b/README.md @@ -54,7 +54,11 @@ client ### Automatic cleanup with `await using` The client, sessions, and operations implement `Symbol.asyncDispose`, so an `await using` declaration closes them when -it goes out of scope, even if an error is thrown (disposing an already-closed object is a no-op). Node.js 24+ supports +it goes out of scope, even if an error is thrown (disposing an already-closed object is a no-op). Closing is +best-effort: if an operation fails to close, its session and the client are still closed, and the failure is rethrown +once the remaining cleanup has been attempted (several failures from one `close()` are reported as an +`AggregateError`). If both the block and a disposal throw, `await using` throws a `SuppressedError` whose `error` is the +disposal failure and whose `suppressed` is the block's error. Node.js 24+ supports `await using` natively; on Node.js 22, compile it with TypeScript 5.2+. TypeScript also needs the disposable declarations (such as the global `AsyncDisposable` type) to compile `await using`: if your typings don't already provide them, add `"esnext.disposable"` (or `"esnext"`) to `compilerOptions.lib` in your `tsconfig.json`, otherwise the diff --git a/lib/DBSQLClient.ts b/lib/DBSQLClient.ts index f07ff7a6..7a373f74 100644 --- a/lib/DBSQLClient.ts +++ b/lib/DBSQLClient.ts @@ -33,7 +33,7 @@ import { } from './connection/auth/tokenProvider'; import IDBSQLLogger, { LogLevel } from './contracts/IDBSQLLogger'; import DBSQLLogger from './DBSQLLogger'; -import CloseableCollection from './utils/CloseableCollection'; +import CloseableCollection, { throwCollectedErrors } from './utils/CloseableCollection'; import IConnectionProvider from './connection/contracts/IConnectionProvider'; import TelemetryClient from './telemetry/TelemetryClient'; import TelemetryClientProvider from './telemetry/TelemetryClientProvider'; @@ -921,9 +921,18 @@ export default class DBSQLClient extends EventEmitter implements IDBSQLClient, I * method on shutdown so the aggregator drains its remaining metrics. */ public async close(): Promise { + // A failing session must not prevent releasing the backend and telemetry: attempt every + // cleanup step and report the collected failures at the end. + const errors: Array = []; try { await this.sessions.closeAll(); + } catch (error) { + errors.push(error); + } + try { await this.backend?.close(); + } catch (error) { + errors.push(error); } finally { this.releaseFeatureFlagCache(); } @@ -949,6 +958,8 @@ export default class DBSQLClient extends EventEmitter implements IDBSQLClient, I this.client = undefined; this.connectionProvider = undefined; this.authProvider = undefined; + + throwCollectedErrors(errors, 'Failed to close client'); } /** diff --git a/lib/DBSQLOperation.ts b/lib/DBSQLOperation.ts index 27cc68d0..bbd1d178 100644 --- a/lib/DBSQLOperation.ts +++ b/lib/DBSQLOperation.ts @@ -207,7 +207,8 @@ export default class DBSQLOperation implements IOperation { } const result = await this.backend.cancel(); this.cancelled = true; - this.onClose?.(); + // Cancelling doesn't release the backend handle, so the operation stays registered with its + // session until it's closed (explicitly, by `await using`, or when the session is closed). return result; } @@ -220,7 +221,8 @@ export default class DBSQLOperation implements IOperation { } private async closeInternal(): Promise { - if (this.closed || this.cancelled) { + // A cancelled operation still holds a backend handle, so only an already closed one is a no-op. + if (this.closed) { return Status.success(); } const result = await this.backend.close(); diff --git a/lib/DBSQLSession.ts b/lib/DBSQLSession.ts index f35358f0..8d3efa4d 100644 --- a/lib/DBSQLSession.ts +++ b/lib/DBSQLSession.ts @@ -19,7 +19,7 @@ import IOperation from './contracts/IOperation'; import DBSQLOperation from './DBSQLOperation'; import Status from './dto/Status'; import InfoValue from './dto/InfoValue'; -import CloseableCollection from './utils/CloseableCollection'; +import CloseableCollection, { throwCollectedErrors } from './utils/CloseableCollection'; import { LogLevel } from './contracts/IDBSQLLogger'; import HiveDriverError from './errors/HiveDriverError'; import ParameterError from './errors/ParameterError'; @@ -328,7 +328,14 @@ export default class DBSQLSession implements IDBSQLSession { return Status.success(); } - await this.operations.closeAll(); + // A failing operation must not prevent closing the session itself: attempt every + // cleanup step and report the collected failures at the end. + const errors: Array = []; + try { + await this.operations.closeAll(); + } catch (err) { + errors.push(err); + } // `latencyMs` on CONNECTION_CLOSE is the closeSession RPC duration, not the // session lifetime. It's the symmetric counterpart of CREATE_SESSION's @@ -347,22 +354,21 @@ export default class DBSQLSession implements IDBSQLSession { }); }; + let status: Status | undefined; try { closeStart = Date.now(); - const status = await this.backend.close(); - - this.onClose?.(); - this.isOpen = false; - emitClose(); - + status = await this.backend.close(); this.context.getLogger().log(LogLevel.debug, `Session closed with id: ${this.id}`); - return status; } catch (err) { + errors.push(err); + } finally { this.onClose?.(); this.isOpen = false; emitClose(); - throw err; } + + throwCollectedErrors(errors, `Failed to close session with id: ${this.id}`); + return status ?? Status.success(); } /** diff --git a/lib/utils/CloseableCollection.ts b/lib/utils/CloseableCollection.ts index 91b8bfcf..4d89bc70 100644 --- a/lib/utils/CloseableCollection.ts +++ b/lib/utils/CloseableCollection.ts @@ -21,12 +21,44 @@ export default class CloseableCollection { this.items.delete(item); } + /** + * Attempts to close every item, even if some of them fail. Successfully closed items are removed from the + * collection; failed ones are kept. Failures are reported once all items have been attempted + * (see {@link throwCollectedErrors}). + */ public async closeAll() { + const errors: Array = []; const items = [...this.items]; for (const item of items) { - await item.close(); // eslint-disable-line no-await-in-loop - item.onClose = undefined; - this.items.delete(item); + try { + await item.close(); // eslint-disable-line no-await-in-loop + item.onClose = undefined; + this.items.delete(item); + } catch (error) { + errors.push(error); + } } + throwCollectedErrors(errors, 'Failed to close some resources'); + } +} + +// `AggregateError` is available in every supported Node.js version, but isn't part of the ES2018 lib typings +// this project compiles against, so reference it with a local type instead of a global declaration. +const AggregateErrorConstructor = ( + globalThis as unknown as { + AggregateError: new (errors: Iterable, message?: string) => Error & { errors: Array }; + } +).AggregateError; + +/** + * Rethrows the collected cleanup errors: nothing if there are none, the error itself if there is exactly one + * (so callers see the same error as before), or an `AggregateError` holding all of them otherwise. + */ +export function throwCollectedErrors(errors: Array, message: string): void { + if (errors.length === 1) { + throw errors[0]; + } + if (errors.length > 1) { + throw new AggregateErrorConstructor(errors, message); } } diff --git a/tests/unit/DBSQLClient.test.ts b/tests/unit/DBSQLClient.test.ts index f955e57d..de59c06b 100644 --- a/tests/unit/DBSQLClient.test.ts +++ b/tests/unit/DBSQLClient.test.ts @@ -620,6 +620,94 @@ describe('DBSQLClient[Symbol.asyncDispose]', () => { await new DBSQLClient()[Symbol.asyncDispose](); }); + + it('should release every resource when an operation always fails to close', async () => { + const client = new DBSQLClient(); + const thriftClient = sinon.spy(new ThriftClientStub()); + client['client'] = thriftClient; + client['connectionProvider'] = new ConnectionProviderStub(); + client['authProvider'] = new AuthProviderStub(); + const backend = new ThriftBackend({ context: client, onConnectionEvent: () => {} }); + client['backend'] = backend; + const backendClose = sinon.spy(backend, 'close'); + const releaseFeatureFlagCache = sinon.spy(client as any, 'releaseFeatureFlagCache'); + // Simulate an initialized telemetry client so `close()` has to release it + client['host'] = 'test.cloud.databricks.com'; + client['telemetryClient'] = {} as any; + const releaseClient = sinon.stub(TelemetryClientProvider.getInstance(), 'releaseClient').resolves(); + + try { + const session = await client.openSession(); + const operation = await session.executeStatement('SELECT 1'); + const error = new Error('operation close failed'); + const operationClose = sinon.stub((operation as any).backend, 'close').rejects(error); + + // Mirrors the README's nested `await using`: operation, then session, then client are disposed + const disposalErrors: Array = []; + for (const resource of [operation, session, client]) { + try { + await resource[Symbol.asyncDispose](); // eslint-disable-line no-await-in-loop + } catch (e) { + disposalErrors.push(e); + } + } + + // The operation failure is reported by its own disposal and by the session's cleanup... + expect(disposalErrors).to.deep.equal([error, error]); + // ...but it's attempted only by those two, and never again by the client + expect(operationClose.callCount).to.eq(2); + // The session, backend, feature flags and telemetry are all still released + expect(thriftClient.CloseSession.calledOnce).to.be.true; + expect(client['sessions']['items'].size).to.eq(0); + expect(backendClose.calledOnce).to.be.true; + expect(releaseFeatureFlagCache.calledOnce).to.be.true; + expect(releaseClient.calledOnceWith(client, 'test.cloud.databricks.com')).to.be.true; + expect(client['backend']).to.be.undefined; + expect(client['client']).to.be.undefined; + expect(client['telemetryClient']).to.be.undefined; + } finally { + releaseClient.restore(); + } + }); + + it('should release the backend and telemetry and report failures when a session fails to close', async () => { + const client = new DBSQLClient(); + const thriftClient = sinon.spy(new ThriftClientStub()); + client['client'] = thriftClient; + client['connectionProvider'] = new ConnectionProviderStub(); + client['authProvider'] = new AuthProviderStub(); + const backend = new ThriftBackend({ context: client, onConnectionEvent: () => {} }); + client['backend'] = backend; + const sessionError = new Error('session close failed'); + const backendError = new Error('backend close failed'); + const backendClose = sinon.stub(backend, 'close').rejects(backendError); + client['host'] = 'test.cloud.databricks.com'; + client['telemetryClient'] = {} as any; + const releaseClient = sinon.stub(TelemetryClientProvider.getInstance(), 'releaseClient').resolves(); + + try { + const session = await client.openSession(); + sinon.stub(session, 'close').rejects(sessionError); + + try { + await client[Symbol.asyncDispose](); + expect.fail('It should throw an error'); + } catch (e) { + if (e instanceof AssertionError) { + throw e; + } + // `AggregateError` isn't in the project's ES2018 lib typings + expect(e).to.be.instanceOf((globalThis as any).AggregateError); + expect((e as any).errors).to.deep.equal([sessionError, backendError]); + } + expect(backendClose.calledOnce).to.be.true; + expect(releaseClient.calledOnce).to.be.true; + expect(client['backend']).to.be.undefined; + expect(client['telemetryClient']).to.be.undefined; + } finally { + releaseClient.restore(); + } + }); }); describe('DBSQLClient.createAuthProvider', () => { diff --git a/tests/unit/DBSQLOperation.test.ts b/tests/unit/DBSQLOperation.test.ts index 9caa7074..b1c0567d 100644 --- a/tests/unit/DBSQLOperation.test.ts +++ b/tests/unit/DBSQLOperation.test.ts @@ -338,7 +338,7 @@ describe('DBSQLOperation', () => { expect(operation['closed']).to.be.true; }); - it('should return immediately if already cancelled', async () => { + it('should release the backend handle of a cancelled operation', async () => { const context = new ClientContextStub(); const driver = sinon.spy(context.driver); const operation = createOperationForTest({ handle: operationHandleStub({ hasResultSet: true }), context }); @@ -352,9 +352,12 @@ describe('DBSQLOperation', () => { expect(operation['closed']).to.be.false; await operation.close(); - expect(driver.closeOperation.callCount).to.be.equal(0); + expect(driver.closeOperation.callCount).to.be.equal(1); expect(operation['cancelled']).to.be.true; - expect(operation['closed']).to.be.false; + expect(operation['closed']).to.be.true; + + await operation.close(); + expect(driver.closeOperation.callCount).to.be.equal(1); }); it('should initialize from directResults', async () => { @@ -430,16 +433,27 @@ describe('DBSQLOperation', () => { expect(driver.closeOperation.callCount).to.be.equal(1); }); - it('should be a no-op for an already closed or cancelled operation', async () => { + it('should be a no-op for an already closed operation', async () => { const context = new ClientContextStub(); const driver = sinon.spy(context.driver); const closed = createOperationForTest({ handle: operationHandleStub({ hasResultSet: true }), context }); - const cancelled = createOperationForTest({ handle: operationHandleStub({ hasResultSet: true }), context }); await closed.close(); - await cancelled.cancel(); await closed[Symbol.asyncDispose](); await closed[Symbol.asyncDispose](); + expect(driver.closeOperation.callCount).to.be.equal(1); + }); + + it('should release the backend handle of a cancelled operation exactly once', async () => { + const context = new ClientContextStub(); + const driver = sinon.spy(context.driver); + const cancelled = createOperationForTest({ handle: operationHandleStub({ hasResultSet: true }), context }); + + await cancelled.cancel(); + expect(driver.closeOperation.callCount).to.be.equal(0); + await cancelled[Symbol.asyncDispose](); + expect(driver.closeOperation.callCount).to.be.equal(1); + expect(cancelled['closed']).to.be.true; await cancelled[Symbol.asyncDispose](); expect(driver.closeOperation.callCount).to.be.equal(1); }); diff --git a/tests/unit/DBSQLSession.test.ts b/tests/unit/DBSQLSession.test.ts index 36ab26e9..eb636d29 100644 --- a/tests/unit/DBSQLSession.test.ts +++ b/tests/unit/DBSQLSession.test.ts @@ -804,5 +804,96 @@ describe('DBSQLSession', () => { // A failed close still marks the session closed, so disposing again is a no-op. await session[Symbol.asyncDispose](); }); + + it('should close the session even if closing an operation fails', async () => { + const context = new ClientContextStub(); + const closeSession = sinon.spy(context.driver, 'closeSession'); + const error = new Error('operation close failed'); + const originalCloseOperation = context.driver.closeOperation.bind(context.driver); + // Calls 1 and 2 close the failing operation (its own disposal, then the session's cleanup); + // call 3 closes the other operation, which succeeds. + let closeOperationCalls = 0; + const closeOperation = sinon.stub(context.driver, 'closeOperation').callsFake(async (request) => { + closeOperationCalls += 1; + if (closeOperationCalls <= 2) { + throw error; + } + return originalCloseOperation(request); + }); + const session = createSessionForTest({ handle: sessionHandleStub, context }); + const failing = await session.executeStatement('SELECT 1'); + const other = await session.executeStatement('SELECT 2'); + if (!(other instanceof DBSQLOperation)) { + expect.fail('Assertion error: operation is not a DBSQLOperation'); + } + + // Mirrors `await using session = ...; await using operation = ...`: the operation is disposed first + await expectFailure(() => failing[Symbol.asyncDispose]()); + try { + await session[Symbol.asyncDispose](); + expect.fail('It should throw an error'); + } catch (e) { + if (e instanceof AssertionError) { + throw e; + } + expect(e).to.equal(error); + } + // The other operation and the session itself were still closed + expect(other['closed']).to.be.true; + expect(session['isOpen']).to.be.false; + expect(closeSession.callCount).to.eq(1); + expect(closeOperation.callCount).to.eq(3); + + // Disposing the closed session again is a no-op + await session[Symbol.asyncDispose](); + expect(closeSession.callCount).to.eq(1); + expect(closeOperation.callCount).to.eq(3); + }); + + it('should report both operation and session close failures', async () => { + const context = new ClientContextStub(); + const operationError = new Error('operation close failed'); + const sessionError = new Error('session close failed'); + sinon.stub(context.driver, 'closeOperation').rejects(operationError); + sinon.stub(context.driver, 'closeSession').rejects(sessionError); + const session = createSessionForTest({ handle: sessionHandleStub, context }); + await session.executeStatement('SELECT 1'); + + try { + await session[Symbol.asyncDispose](); + expect.fail('It should throw an error'); + } catch (e) { + if (e instanceof AssertionError) { + throw e; + } + // `AggregateError` isn't in the project's ES2018 lib typings + expect(e).to.be.instanceOf((globalThis as any).AggregateError); + expect((e as any).errors).to.deep.equal([operationError, sessionError]); + } + expect(session['isOpen']).to.be.false; + }); + + it('should release cancelled operations exactly once', async () => { + const context = new ClientContextStub(); + const driver = sinon.spy(context.driver); + const session = createSessionForTest({ handle: sessionHandleStub, context }); + const operation = await session.executeStatement('SELECT * FROM table'); + if (!(operation instanceof DBSQLOperation)) { + expect.fail('Assertion error: operation is not a DBSQLOperation'); + } + + await operation.cancel(); + // A cancelled operation still holds a backend handle, so the session keeps tracking it + expect(session['operations']['items'].has(operation)).to.be.true; + expect(driver.closeOperation.callCount).to.eq(0); + + await session[Symbol.asyncDispose](); + expect(driver.closeOperation.callCount).to.eq(1); + expect(operation['closed']).to.be.true; + expect(session['operations']['items'].size).to.eq(0); + + await operation[Symbol.asyncDispose](); + expect(driver.closeOperation.callCount).to.eq(1); + }); }); }); diff --git a/tests/unit/kernel/execution.test.ts b/tests/unit/kernel/execution.test.ts index d59193d8..2658768e 100644 --- a/tests/unit/kernel/execution.test.ts +++ b/tests/unit/kernel/execution.test.ts @@ -1710,4 +1710,21 @@ describe('DBSQLSession / DBSQLOperation over kernel backends — Symbol.asyncDis await operation[Symbol.asyncDispose](); expect(closeSpy.callCount).to.equal(1); }); + + it('disposing a cancelled operation closes the native statement exactly once', async () => { + const connection = new FakeNativeConnection(); + const session = makeFacadeSession(connection); + const operation = await session.executeStatement('SELECT 1'); + const closeSpy = sinon.spy(connection.statementToReturn, 'close'); + + await operation.cancel(); + expect(connection.statementToReturn.cancelled).to.equal(true); + expect(closeSpy.callCount).to.equal(0); + + await operation[Symbol.asyncDispose](); + expect(connection.statementToReturn.closed).to.equal(true); + await operation[Symbol.asyncDispose](); + await session[Symbol.asyncDispose](); + expect(closeSpy.callCount).to.equal(1); + }); }); diff --git a/tests/unit/utils/CloseableCollection.test.ts b/tests/unit/utils/CloseableCollection.test.ts index da7ab147..9a9c86d4 100644 --- a/tests/unit/utils/CloseableCollection.test.ts +++ b/tests/unit/utils/CloseableCollection.test.ts @@ -121,7 +121,7 @@ describe('CloseableCollection', () => { expect(collection['items'].size).to.be.eq(0); }); - it('should close all and delete only first successfully closed items', async () => { + it('should attempt to close all items and keep only failed ones', async () => { const collection = new CloseableCollection(); expect(collection['items'].size).to.be.eq(0); @@ -141,8 +141,10 @@ describe('CloseableCollection', () => { }, }; + let item3Closed = false; const item3: ICloseable = { close() { + item3Closed = true; this.onClose?.(); return Promise.resolve(); }, @@ -163,10 +165,36 @@ describe('CloseableCollection', () => { if (error instanceof AssertionError || !(error instanceof Error)) { throw error; } + // A single failure is rethrown as is expect(error.message).to.eq(errorMessage); expect(item1.onClose).to.be.undefined; expect(item2.onClose).to.be.not.undefined; - expect(item3.onClose).to.be.not.undefined; + // Items after the failed one are still closed + expect(item3Closed).to.be.true; + expect(item3.onClose).to.be.undefined; + expect(collection['items'].size).to.be.eq(1); + expect(collection['items'].has(item2)).to.be.true; + } + }); + + it('should report multiple failures as an AggregateError', async () => { + const collection = new CloseableCollection(); + const error1 = new Error('Error from item 1'); + const error2 = new Error('Error from item 2'); + + collection.add({ close: () => Promise.reject(error1) }); + collection.add({ close: () => Promise.reject(error2) }); + + try { + await collection.closeAll(); + expect.fail('It should throw an error'); + } catch (error) { + if (error instanceof AssertionError) { + throw error; + } + // `AggregateError` isn't in the project's ES2018 lib typings + expect(error).to.be.instanceOf((globalThis as any).AggregateError); + expect((error as any).errors).to.deep.equal([error1, error2]); expect(collection['items'].size).to.be.eq(2); } }); From 2b827699a4a7e0a86841f255d9432a379f35cce5 Mon Sep 17 00:00:00 2001 From: "peco-engineer-bot[bot]" Date: Thu, 12 Nov 2026 19:06:58 +0000 Subject: [PATCH 4/5] engineer-bot: address @mention on PR #586 Signed-off-by: peco-engineer-bot[bot] --- CHANGELOG.md | 2 +- README.md | 13 +- lib/DBSQLClient.ts | 2 + lib/DBSQLOperation.ts | 2 + lib/DBSQLSession.ts | 2 + lib/asyncDisposable.ts | 40 ++++ lib/contracts/IDBSQLClient.ts | 6 - lib/contracts/IDBSQLSession.ts | 6 - lib/contracts/IOperation.ts | 6 - lib/index.ts | 3 + tests/unit/.stubs/OperationStub.ts | 4 - tests/unit/DBSQLClient.test.ts | 5 +- tests/unit/DBSQLSession.test.ts | 3 +- tests/unit/asyncDisposable.test.ts | 69 ++++++ tests/unit/asyncDispose.types.test.ts | 289 ++++++++++++++++++++++++++ tests/unit/kernel/execution.test.ts | 5 +- 16 files changed, 426 insertions(+), 31 deletions(-) create mode 100644 lib/asyncDisposable.ts create mode 100644 tests/unit/asyncDisposable.test.ts create mode 100644 tests/unit/asyncDispose.types.test.ts diff --git a/CHANGELOG.md b/CHANGELOG.md index 1994e779..97a56270 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,7 @@ ## Unreleased -- `DBSQLClient`, sessions, and operations now implement `Symbol.asyncDispose`, so `await using` closes them automatically; disposing an already-closed object is a no-op. Custom implementations of the `IDBSQLClient`, `IDBSQLSession`, or `IOperation` interfaces must add this method. (#331) +- `DBSQLClient`, sessions, and operations now implement `Symbol.asyncDispose`, so `await using` closes them automatically; disposing an already-closed object is a no-op. The `IDBSQLClient`, `IDBSQLSession`, and `IOperation` interfaces and the return types of `connect()`, `openSession()`, and the session's operation methods are unchanged, so existing custom implementations, mocks, and subclasses keep compiling. Because those methods return the interface types, wrap their results with the new `asyncDisposable()` helper to bind them with `await using` in TypeScript (`await using session = asyncDisposable(await client.openSession())`); it returns the driver's own objects unchanged and gives a custom implementation without the method a `[Symbol.asyncDispose]()` that calls `close()`. The typings declare `Symbol.asyncDispose` themselves, so calling it explicitly doesn't need the `esnext.disposable` lib. (#331) - `DBSQLSession.close()` and `DBSQLClient.close()` now attempt every cleanup step even if one fails: an operation that fails to close no longer prevents closing its sibling operations, the session's backend session, or the client's backend, feature-flag, and telemetry resources. A single failure is rethrown unchanged once cleanup completes; several failures are reported as an `AggregateError`. A client whose backend fails to close is now also reset, like a successfully closed one. - Closing a cancelled operation now releases its backend handle (Thrift `CloseOperation` / kernel statement close) instead of being a no-op, and a cancelled operation stays tracked by its session until it is closed, so `session.close()` and `await using` release it too. - CloudFetch retry-exhaustion errors now identify the download and the last failure: the `RetryError` message reads `CloudFetch result download failed: (last error: )`. `` is `HTTP : ` (or `HTTP ` when the status text is empty) for HTTP failures, and `network error `, `network error ()` (e.g. `aborted`, when there is no `code`) or a bare `network error` for network failures (the raw network-error message, which can contain the presigned URL, is not copied). If no cause can be determined, the ` (last error: …)` suffix is omitted. The `RetryError` instance, `errorCode` and `payload` (including the node-fetch `Request` / `Response`) are unchanged. (#471) diff --git a/README.md b/README.md index 202be0f7..1f08c55d 100644 --- a/README.md +++ b/README.md @@ -64,13 +64,20 @@ declarations (such as the global `AsyncDisposable` type) to compile `await using provide them, add `"esnext.disposable"` (or `"esnext"`) to `compilerOptions.lib` in your `tsconfig.json`, otherwise the example below can fail to compile even with TypeScript 5.2+. -```javascript +`openSession()` and the session's operation methods are typed as returning the `IDBSQLSession` and `IOperation` +interfaces, which don't require `Symbol.asyncDispose` (so existing custom implementations keep compiling). In +TypeScript, wrap their results with `asyncDisposable()`: it returns the driver's own sessions and operations unchanged, +typed as disposable. + +```typescript +import { DBSQLClient, asyncDisposable } from '@databricks/sql'; + async function main() { await using client = new DBSQLClient(); await client.connect({ host, path, token }); - await using session = await client.openSession(); - await using operation = await session.executeStatement('SELECT "Hello, World!"'); + await using session = asyncDisposable(await client.openSession()); + await using operation = asyncDisposable(await session.executeStatement('SELECT "Hello, World!"')); console.table(await operation.fetchAll()); } // closes `operation`, `session`, then `client` ``` diff --git a/lib/DBSQLClient.ts b/lib/DBSQLClient.ts index 7a373f74..04bd0065 100644 --- a/lib/DBSQLClient.ts +++ b/lib/DBSQLClient.ts @@ -43,6 +43,8 @@ import MetricsAggregator from './telemetry/MetricsAggregator'; import { DriverConfiguration, DRIVER_NAME, TelemetryEventType, DEFAULT_TELEMETRY_CONFIG } from './telemetry/types'; import { safeEmit, isTelemetryDisabledByEnv } from './telemetry/telemetryUtils'; import driverVersion from './version'; +// Declares `Symbol.asyncDispose` for the emitted typings of `[Symbol.asyncDispose]()` below +import './contracts/asyncDispose'; function prependSlash(str: string): string { if (str.length > 0 && str.charAt(0) !== '/') { diff --git a/lib/DBSQLOperation.ts b/lib/DBSQLOperation.ts index bbd1d178..d3497744 100644 --- a/lib/DBSQLOperation.ts +++ b/lib/DBSQLOperation.ts @@ -22,6 +22,8 @@ import { synthesizeThriftStatus, synthesizeThriftResultSetMetadata } from './thr import { mapOperationTypeToTelemetryType } from './telemetry/telemetryTypeMappers'; import ExceptionClassifier from './telemetry/ExceptionClassifier'; import { safeEmit } from './telemetry/telemetryUtils'; +// Declares `Symbol.asyncDispose` for the emitted typings of `[Symbol.asyncDispose]()` below +import './contracts/asyncDispose'; function mapNeutralResultFormatToTelemetryType(resultFormat?: ResultFormat): string | undefined { switch (resultFormat) { diff --git a/lib/DBSQLSession.ts b/lib/DBSQLSession.ts index 8d3efa4d..be0f5106 100644 --- a/lib/DBSQLSession.ts +++ b/lib/DBSQLSession.ts @@ -29,6 +29,8 @@ import ISessionBackend from './contracts/ISessionBackend'; import IOperationBackend from './contracts/IOperationBackend'; import { numberToInt64 as numberToInt64Impl } from './thrift-backend/ThriftSessionBackend'; import { safeEmit } from './telemetry/telemetryUtils'; +// Declares `Symbol.asyncDispose` for the emitted typings of `[Symbol.asyncDispose]()` below +import './contracts/asyncDispose'; // Explicitly promisify a callback-style `pipeline` because `node:stream/promises` is not available in Node 14 const pipeline = util.promisify(stream.pipeline); diff --git a/lib/asyncDisposable.ts b/lib/asyncDisposable.ts new file mode 100644 index 00000000..2697a240 --- /dev/null +++ b/lib/asyncDisposable.ts @@ -0,0 +1,40 @@ +// Declares `Symbol.asyncDispose` for the emitted typings below +import './contracts/asyncDispose'; + +/** + * A closeable resource that can also be bound with `await using`. + */ +export type AsyncDisposableResource = T & { + [Symbol.asyncDispose](): Promise; +}; + +/** + * Returns `resource` typed as async-disposable, so that it can be bound with `await using`. + * + * `connect()`, `openSession()` and the session's operation methods return the `IDBSQLClient` / + * `IDBSQLSession` / `IOperation` interfaces, which don't require `Symbol.asyncDispose` (so custom + * implementations and subclasses written before it was added keep compiling). The driver's own clients, + * sessions, and operations implement it, so they are returned unchanged. A custom implementation without it gets a non-enumerable + * `[Symbol.asyncDispose]()` that calls its `close()` (this throws a `TypeError` if the object isn't + * extensible, e.g. a frozen object). + * + * @example + * await using session = asyncDisposable(await client.openSession()); + * await using operation = asyncDisposable(await session.executeStatement('SELECT 1')); + */ +export default function asyncDisposable }>( + resource: T, +): AsyncDisposableResource { + const candidate = resource as T & { [Symbol.asyncDispose]?: unknown }; + if (typeof candidate[Symbol.asyncDispose] !== 'function') { + Object.defineProperty(resource, Symbol.asyncDispose, { + configurable: true, + writable: true, + enumerable: false, + value: async function asyncDispose(this: T): Promise { + await this.close(); + }, + }); + } + return resource as AsyncDisposableResource; +} diff --git a/lib/contracts/IDBSQLClient.ts b/lib/contracts/IDBSQLClient.ts index e08c9b5d..c6eab70d 100644 --- a/lib/contracts/IDBSQLClient.ts +++ b/lib/contracts/IDBSQLClient.ts @@ -5,7 +5,6 @@ import { ProxyOptions } from '../connection/contracts/IConnectionOptions'; import OAuthPersistence from '../connection/auth/DatabricksOAuth/OAuthPersistence'; import ITokenProvider from '../connection/auth/tokenProvider/ITokenProvider'; import { TokenCallback } from '../connection/auth/tokenProvider/ExternalTokenProvider'; -import './asyncDispose'; export interface ClientOptions { logger?: IDBSQLLogger; @@ -296,9 +295,4 @@ export default interface IDBSQLClient { openSession(request?: OpenSessionRequest): Promise; close(): Promise; - - /** - * Closes the client, enabling `await using client = ...`. No-op if already closed - */ - [Symbol.asyncDispose](): Promise; } diff --git a/lib/contracts/IDBSQLSession.ts b/lib/contracts/IDBSQLSession.ts index 17b373d9..e7eb31d8 100644 --- a/lib/contracts/IDBSQLSession.ts +++ b/lib/contracts/IDBSQLSession.ts @@ -3,7 +3,6 @@ import IOperation from './IOperation'; import Status from '../dto/Status'; import InfoValue from '../dto/InfoValue'; import { DBSQLParameter, DBSQLParameterValue } from '../DBSQLParameter'; -import './asyncDispose'; export type ExecuteStatementOptions = { /** @@ -234,9 +233,4 @@ export default interface IDBSQLSession { * closes the session */ close(): Promise; - - /** - * Closes the session, enabling `await using session = ...`. No-op if already closed - */ - [Symbol.asyncDispose](): Promise; } diff --git a/lib/contracts/IOperation.ts b/lib/contracts/IOperation.ts index 3a1ce65a..bbeed622 100644 --- a/lib/contracts/IOperation.ts +++ b/lib/contracts/IOperation.ts @@ -2,7 +2,6 @@ import { Readable, ReadableOptions } from 'node:stream'; import { TGetOperationStatusResp, TTableSchema } from '../../thrift/TCLIService_types'; import Status from '../dto/Status'; import { ResultMetadata } from './ResultMetadata'; -import './asyncDispose'; export type OperationStatusCallback = (progress: TGetOperationStatusResp) => unknown; @@ -106,9 +105,4 @@ export default interface IOperation { iterateRows(options?: IteratorOptions): IOperationRowsIterator; toNodeStream(options?: NodeStreamOptions): Readable; - - /** - * Closes the operation, enabling `await using operation = ...`. No-op if already closed - */ - [Symbol.asyncDispose](): Promise; } diff --git a/lib/index.ts b/lib/index.ts index a2b90c9c..20d28b4f 100644 --- a/lib/index.ts +++ b/lib/index.ts @@ -23,6 +23,9 @@ import { LogLevel } from './contracts/IDBSQLLogger'; // Re-export types for TypeScript users export type { default as ITokenProvider } from './connection/auth/tokenProvider/ITokenProvider'; +export { default as asyncDisposable } from './asyncDisposable'; +export type { AsyncDisposableResource } from './asyncDisposable'; + export { LOG_FILE_UNAVAILABLE_WARNING_CODE } from './DBSQLLogger'; export { CircuitBreakerOpenError, CIRCUIT_BREAKER_OPEN_CODE } from './telemetry/CircuitBreaker'; export { TelemetryTerminalError } from './telemetry/DatabricksTelemetryExporter'; diff --git a/tests/unit/.stubs/OperationStub.ts b/tests/unit/.stubs/OperationStub.ts index a56abbaa..1dcac5ca 100644 --- a/tests/unit/.stubs/OperationStub.ts +++ b/tests/unit/.stubs/OperationStub.ts @@ -69,8 +69,4 @@ export default class OperationStub implements IOperation { public toNodeStream(options?: NodeStreamOptions): Readable { throw new Error('Not implemented'); } - - public async [Symbol.asyncDispose]() { - await this.close(); - } } diff --git a/tests/unit/DBSQLClient.test.ts b/tests/unit/DBSQLClient.test.ts index de59c06b..f20495da 100644 --- a/tests/unit/DBSQLClient.test.ts +++ b/tests/unit/DBSQLClient.test.ts @@ -4,6 +4,7 @@ import fs from 'fs'; import { Response } from 'node-fetch'; import DBSQLClient, { ThriftLibrary } from '../../lib/DBSQLClient'; import DBSQLSession from '../../lib/DBSQLSession'; +import asyncDisposable from '../../lib/asyncDisposable'; import ThriftBackend from '../../lib/thrift-backend/ThriftBackend'; import KernelBackend from '../../lib/kernel/KernelBackend'; import * as KernelNativeLoader from '../../lib/kernel/KernelNativeLoader'; @@ -637,8 +638,8 @@ describe('DBSQLClient[Symbol.asyncDispose]', () => { const releaseClient = sinon.stub(TelemetryClientProvider.getInstance(), 'releaseClient').resolves(); try { - const session = await client.openSession(); - const operation = await session.executeStatement('SELECT 1'); + const session = asyncDisposable(await client.openSession()); + const operation = asyncDisposable(await session.executeStatement('SELECT 1')); const error = new Error('operation close failed'); const operationClose = sinon.stub((operation as any).backend, 'close').rejects(error); diff --git a/tests/unit/DBSQLSession.test.ts b/tests/unit/DBSQLSession.test.ts index eb636d29..bd13c004 100644 --- a/tests/unit/DBSQLSession.test.ts +++ b/tests/unit/DBSQLSession.test.ts @@ -5,6 +5,7 @@ import DBSQLSession, { numberToInt64 } from '../../lib/DBSQLSession'; import InfoValue from '../../lib/dto/InfoValue'; import Status from '../../lib/dto/Status'; import DBSQLOperation from '../../lib/DBSQLOperation'; +import asyncDisposable from '../../lib/asyncDisposable'; import { PrimaryKeysRequest } from '../../lib/contracts/IDBSQLSession'; import ISessionBackend from '../../lib/contracts/ISessionBackend'; import ParameterError from '../../lib/errors/ParameterError'; @@ -821,7 +822,7 @@ describe('DBSQLSession', () => { return originalCloseOperation(request); }); const session = createSessionForTest({ handle: sessionHandleStub, context }); - const failing = await session.executeStatement('SELECT 1'); + const failing = asyncDisposable(await session.executeStatement('SELECT 1')); const other = await session.executeStatement('SELECT 2'); if (!(other instanceof DBSQLOperation)) { expect.fail('Assertion error: operation is not a DBSQLOperation'); diff --git a/tests/unit/asyncDisposable.test.ts b/tests/unit/asyncDisposable.test.ts new file mode 100644 index 00000000..6a65bcc9 --- /dev/null +++ b/tests/unit/asyncDisposable.test.ts @@ -0,0 +1,69 @@ +import { expect } from 'chai'; +import sinon from 'sinon'; +import asyncDisposable from '../../lib/asyncDisposable'; +import * as driver from '../../lib'; +import DBSQLClient from '../../lib/DBSQLClient'; +import Status from '../../lib/dto/Status'; +import OperationStub from './.stubs/OperationStub'; +import ClientContextStub from './.stubs/ClientContextStub'; +import { createSessionForTest } from './.stubs/createSessionForTest'; + +describe('asyncDisposable', () => { + it('is exported from the package entry point', () => { + expect(driver.asyncDisposable).to.equal(asyncDisposable); + }); + + it("returns the driver's own resources unchanged", async () => { + const client = new DBSQLClient(); + const session = createSessionForTest({ + handle: { sessionId: { guid: Buffer.alloc(16), secret: Buffer.alloc(16) } }, + context: new ClientContextStub(), + }); + const operation = await session.executeStatement('SELECT 1'); + + for (const resource of [client, session, operation]) { + const dispose = (resource as any)[Symbol.asyncDispose]; + expect(asyncDisposable(resource)).to.equal(resource); + expect((resource as any)[Symbol.asyncDispose]).to.equal(dispose); + } + + const closeSpy = sinon.spy(operation, 'close'); + await asyncDisposable(operation)[Symbol.asyncDispose](); + expect(closeSpy.calledOnce).to.be.true; + }); + + it('adds a non-enumerable disposer that closes a custom implementation', async () => { + const operation = new OperationStub([]); + const disposable = asyncDisposable(operation); + + expect(disposable).to.equal(operation); + expect(Object.getOwnPropertyDescriptor(operation, Symbol.asyncDispose)?.enumerable).to.be.false; + expect(await disposable[Symbol.asyncDispose]()).to.be.undefined; + expect(operation.closed).to.be.true; + }); + + it('keeps an existing disposer of a custom implementation', async () => { + const dispose = sinon.stub().resolves(); + const resource = { close: async () => Status.success(), [Symbol.asyncDispose]: dispose }; + + await asyncDisposable(resource)[Symbol.asyncDispose](); + expect(dispose.calledOnce).to.be.true; + }); + + it('propagates close errors from the added disposer', async () => { + const error = new Error('close failed'); + const resource = { close: () => Promise.reject(error) }; + + try { + await asyncDisposable(resource)[Symbol.asyncDispose](); + expect.fail('It should throw an error'); + } catch (e) { + expect(e).to.equal(error); + } + }); + + it('throws a TypeError for a non-extensible custom implementation', () => { + const resource = Object.freeze({ close: async () => Status.success() }); + expect(() => asyncDisposable(resource)).to.throw(TypeError); + }); +}); diff --git a/tests/unit/asyncDispose.types.test.ts b/tests/unit/asyncDispose.types.test.ts new file mode 100644 index 00000000..ee30d5f4 --- /dev/null +++ b/tests/unit/asyncDispose.types.test.ts @@ -0,0 +1,289 @@ +import path from 'path'; +import { expect } from 'chai'; +import ts from 'typescript'; + +// Consumer type tests for `Symbol.asyncDispose` support: compile small consumer programs against the +// declarations emitted from `lib/` (with the `tsconfig.build.json` settings used to build `dist/`). The +// declarations and the consumer sources are kept in memory; nothing is written to disk. + +const repoRoot = path.resolve(__dirname, '../..'); +// Siblings of `lib/`, so the emitted `../thrift/...` imports resolve like they do from `dist/` +const declarationsDir = path.join(repoRoot, '.types-test-dist'); +const consumerDir = path.join(repoRoot, '.types-test-consumer'); + +const virtualFiles = new Map(); + +function emitDeclarations() { + const configPath = path.join(repoRoot, 'tsconfig.build.json'); + const { config, error } = ts.readConfigFile(configPath, ts.sys.readFile); + if (error) { + throw new Error(ts.flattenDiagnosticMessageText(error.messageText, '\n')); + } + const parsed = ts.parseJsonConfigFileContent( + config, + ts.sys, + repoRoot, + { outDir: declarationsDir, emitDeclarationOnly: true, sourceMap: false, declarationMap: false }, + configPath, + ); + const program = ts.createProgram({ rootNames: parsed.fileNames, options: parsed.options }); + const result = program.emit(undefined, (fileName, text) => { + virtualFiles.set(path.resolve(fileName), text); + }); + const diagnostics = [...ts.getPreEmitDiagnostics(program), ...result.diagnostics]; + expect(formatDiagnostics(diagnostics)).to.equal(''); + expect(virtualFiles.has(path.join(declarationsDir, 'index.d.ts'))).to.be.true; +} + +function formatDiagnostics(diagnostics: ReadonlyArray): string { + return ts.formatDiagnostics(diagnostics, { + getCanonicalFileName: (fileName) => fileName, + getCurrentDirectory: () => repoRoot, + getNewLine: () => '\n', + }); +} + +function isVirtual(fileName: string): boolean { + const resolved = path.resolve(fileName); + return [declarationsDir, consumerDir].some((dir) => resolved === dir || resolved.startsWith(`${dir}${path.sep}`)); +} + +type ConsumerLib = 'es2018' | 'esnext.disposable'; + +// `lib.dom.d.ts` is part of TypeScript's default libs and is needed by `@types/thrift` (`XMLHttpRequest`) +const consumerOptions: Record = { + // No disposable declarations at all: neither TypeScript's lib nor `@types/node` (the installed 18.11 typings + // predate `Symbol.asyncDispose`) declare it, so only the driver's own declaration shim can. + es2018: { target: ts.ScriptTarget.ES2018, lib: ['lib.es2018.d.ts', 'lib.dom.d.ts'] }, + 'esnext.disposable': { + target: ts.ScriptTarget.ES2022, + lib: ['lib.es2022.d.ts', 'lib.esnext.disposable.d.ts', 'lib.dom.d.ts'], + }, +}; + +/** Type-checks `source` as a consumer of the published typings and returns the formatted errors */ +function compileConsumer(source: string, consumerLib: ConsumerLib): string { + const fileName = path.join(consumerDir, 'consumer.ts'); + virtualFiles.set(fileName, source); + + const options: ts.CompilerOptions = { + ...consumerOptions[consumerLib], + module: ts.ModuleKind.CommonJS, + moduleResolution: ts.ModuleResolutionKind.Node10, + strict: true, + esModuleInterop: true, + noEmit: true, + skipLibCheck: false, + baseUrl: repoRoot, + paths: { + '@databricks/sql': [path.join(declarationsDir, 'index.d.ts')], + '@databricks/sql/dist/*': [path.join(declarationsDir, '*')], + }, + typeRoots: [path.join(repoRoot, 'node_modules', '@types')], + types: ['node'], + }; + + const host = ts.createCompilerHost(options); + const { fileExists, readFile, getSourceFile, directoryExists, realpath } = host; + host.fileExists = (name) => (isVirtual(name) ? virtualFiles.has(path.resolve(name)) : fileExists.call(host, name)); + host.readFile = (name) => (isVirtual(name) ? virtualFiles.get(path.resolve(name)) : readFile.call(host, name)); + host.directoryExists = (name) => { + if (isVirtual(name)) { + const dir = `${path.resolve(name)}${path.sep}`; + return [...virtualFiles.keys()].some((file) => file.startsWith(dir)); + } + return directoryExists ? directoryExists.call(host, name) : true; + }; + host.realpath = (name) => (isVirtual(name) || !realpath ? name : realpath.call(host, name)); + host.getSourceFile = (name, languageVersion, onError) => { + if (isVirtual(name)) { + const text = virtualFiles.get(path.resolve(name)); + return text === undefined ? undefined : ts.createSourceFile(name, text, languageVersion); + } + return getSourceFile.call(host, name, languageVersion, onError); + }; + + const program = ts.createProgram({ rootNames: [fileName], options, host }); + return formatDiagnostics(ts.getPreEmitDiagnostics(program)); +} + +// Implementations and subclasses written against the contracts before async disposal was added +const legacyConsumer = ` +import { Readable } from 'stream'; +import { DBSQLClient, DBSQLSession } from '@databricks/sql'; +import IDBSQLClient, { ConnectionOptions, OpenSessionRequest } from '@databricks/sql/dist/contracts/IDBSQLClient'; +import IDBSQLSession, { ExecuteStatementOptions, CatalogsRequest } from '@databricks/sql/dist/contracts/IDBSQLSession'; +import IOperation, { + IOperationChunksIterator, + IOperationRowsIterator, +} from '@databricks/sql/dist/contracts/IOperation'; +import Status from '@databricks/sql/dist/dto/Status'; + +function notImplemented(): never { + throw new Error('Not implemented'); +} + +const operationMock: IOperation = { + id: 'operation', + fetchChunk: async () => [], + fetchAll: async () => [], + status: async () => notImplemented(), + cancel: async () => Status.success(), + close: async () => Status.success(), + finished: async () => {}, + hasMoreRows: async () => false, + getSchema: async () => null, + getResultMetadata: async () => notImplemented(), + iterateChunks: (): IOperationChunksIterator => notImplemented(), + iterateRows: (): IOperationRowsIterator => notImplemented(), + toNodeStream: (): Readable => notImplemented(), +}; + +class LegacySession implements IDBSQLSession { + public readonly id = 'session'; + public getInfo = async () => notImplemented(); + public executeStatement = async (): Promise => operationMock; + public getTypeInfo = async (): Promise => operationMock; + public getCatalogs = async (): Promise => operationMock; + public getSchemas = async (): Promise => operationMock; + public getTables = async (): Promise => operationMock; + public getTableTypes = async (): Promise => operationMock; + public getColumns = async (): Promise => operationMock; + public getFunctions = async (): Promise => operationMock; + public getPrimaryKeys = async (): Promise => operationMock; + public getCrossReference = async (): Promise => operationMock; + public close = async () => Status.success(); +} + +class LegacyClient implements IDBSQLClient { + public async connect(): Promise { + return this; + } + + public async openSession(): Promise { + return new LegacySession(); + } + + public async close(): Promise {} +} + +// Subclasses overriding the factories with their original return types +class LoggingClient extends DBSQLClient { + public async connect(options: ConnectionOptions): Promise { + return super.connect(options); + } + + public async openSession(request?: OpenSessionRequest): Promise { + return super.openSession(request); + } +} + +class LoggingSession extends DBSQLSession { + public async executeStatement(statement: string, options?: ExecuteStatementOptions): Promise { + return super.executeStatement(statement, options); + } + + public async getCatalogs(request?: CatalogsRequest): Promise { + return super.getCatalogs(request); + } +} + +export const clients: Array = [new LegacyClient(), new DBSQLClient(), new LoggingClient()]; +export const sessionClass = LoggingSession; +`; + +// Explicit disposal, without `await using` +const explicitDisposalConsumer = ` +import { DBSQLClient, asyncDisposable, AsyncDisposableResource } from '@databricks/sql'; +import IDBSQLSession from '@databricks/sql/dist/contracts/IDBSQLSession'; +import IOperation from '@databricks/sql/dist/contracts/IOperation'; + +export async function main(): Promise { + const client = new DBSQLClient(); + await client.connect({ host: 'host', path: 'path', token: 'token' }); + const session: AsyncDisposableResource = asyncDisposable(await client.openSession()); + const operation: AsyncDisposableResource = asyncDisposable(await session.executeStatement('SELECT 1')); + const catalogs = asyncDisposable(await session.getCatalogs()); + const disposals: Array> = [ + catalogs[Symbol.asyncDispose](), + operation[Symbol.asyncDispose](), + session[Symbol.asyncDispose](), + client[Symbol.asyncDispose](), + ]; + await Promise.all(disposals); +} +`; + +// Each class declaration must make \`Symbol.asyncDispose\` available on its own +const deepImportConsumers: Record = { + DBSQLOperation: ` +import DBSQLOperation from '@databricks/sql/dist/DBSQLOperation'; +export const dispose = (operation: DBSQLOperation): Promise => operation[Symbol.asyncDispose](); +`, + DBSQLSession: ` +import DBSQLSession from '@databricks/sql/dist/DBSQLSession'; +export const dispose = (session: DBSQLSession): Promise => session[Symbol.asyncDispose](); +`, + DBSQLClient: ` +import DBSQLClient from '@databricks/sql/dist/DBSQLClient'; +export const dispose = (client: DBSQLClient): Promise => client[Symbol.asyncDispose](); +`, +}; + +const awaitUsingConsumer = ` +import { DBSQLClient, asyncDisposable } from '@databricks/sql'; + +export async function main(): Promise> { + await using client = new DBSQLClient(); + await client.connect({ host: 'host', path: 'path', token: 'token' }); + await using session = asyncDisposable(await client.openSession()); + await using operation = asyncDisposable(await session.executeStatement('SELECT 1')); + await using tables = asyncDisposable(await session.getTables({ catalogName: 'main' })); + await tables.fetchAll(); + return operation.fetchAll(); +} +`; + +describe('Symbol.asyncDispose consumer typings', function () { + this.timeout(180000); + + before(() => { + emitDeclarations(); + }); + + after(() => { + virtualFiles.clear(); + }); + + (['es2018', 'esnext.disposable'] as const).forEach((consumerLib) => { + it(`keeps legacy interface implementations, mocks and subclasses compiling (lib ${consumerLib})`, () => { + expect(compileConsumer(legacyConsumer, consumerLib)).to.equal(''); + }); + + it(`supports explicit disposal (lib ${consumerLib})`, () => { + expect(compileConsumer(explicitDisposalConsumer, consumerLib)).to.equal(''); + }); + + Object.entries(deepImportConsumers).forEach(([className, source]) => { + it(`declares Symbol.asyncDispose for a deep import of ${className} (lib ${consumerLib})`, () => { + expect(compileConsumer(source, consumerLib)).to.equal(''); + }); + }); + }); + + it('supports an `await using` client -> session -> operation chain (lib esnext.disposable)', () => { + expect(compileConsumer(awaitUsingConsumer, 'esnext.disposable')).to.equal(''); + }); + + it('rejects `await using` of an interface-typed session, which may lack Symbol.asyncDispose', () => { + const source = ` +import { DBSQLClient } from '@databricks/sql'; + +export async function main(): Promise { + await using client = new DBSQLClient(); + await using session = await client.openSession(); +} +`; + expect(compileConsumer(source, 'esnext.disposable')).to.include('Symbol.asyncDispose'); + }); +}); diff --git a/tests/unit/kernel/execution.test.ts b/tests/unit/kernel/execution.test.ts index 2658768e..9f4160ea 100644 --- a/tests/unit/kernel/execution.test.ts +++ b/tests/unit/kernel/execution.test.ts @@ -21,6 +21,7 @@ import KernelSessionBackend from '../../../lib/kernel/KernelSessionBackend'; import KernelOperationBackend from '../../../lib/kernel/KernelOperationBackend'; import DBSQLSession from '../../../lib/DBSQLSession'; import DBSQLOperation from '../../../lib/DBSQLOperation'; +import asyncDisposable from '../../../lib/asyncDisposable'; import { KernelNativeBinding, KernelConnection, KernelStatement } from '../../../lib/kernel/KernelNativeLoader'; import IClientContext, { ClientConfig } from '../../../lib/contracts/IClientContext'; import { CrossReferenceRequest, PrimaryKeysRequest } from '../../../lib/contracts/IDBSQLSession'; @@ -1700,7 +1701,7 @@ describe('DBSQLSession / DBSQLOperation over kernel backends — Symbol.asyncDis it('disposing a session closes its operations and the native connection; re-disposing is a no-op', async () => { const connection = new FakeNativeConnection(); const session = makeFacadeSession(connection); - const operation = await session.executeStatement('SELECT 1'); + const operation = asyncDisposable(await session.executeStatement('SELECT 1')); const closeSpy = sinon.spy(connection, 'close'); expect(await session[Symbol.asyncDispose]()).to.equal(undefined); @@ -1714,7 +1715,7 @@ describe('DBSQLSession / DBSQLOperation over kernel backends — Symbol.asyncDis it('disposing a cancelled operation closes the native statement exactly once', async () => { const connection = new FakeNativeConnection(); const session = makeFacadeSession(connection); - const operation = await session.executeStatement('SELECT 1'); + const operation = asyncDisposable(await session.executeStatement('SELECT 1')); const closeSpy = sinon.spy(connection.statementToReturn, 'close'); await operation.cancel(); From 78aea2eff6251f0f5b41ce64b5b44c4c74ff7eca Mon Sep 17 00:00:00 2001 From: Vu Anh Phung Date: Thu, 12 Nov 2026 19:23:21 +0000 Subject: [PATCH 5/5] Keep async disposal scoped to existing close behavior Signed-off-by: Vu Anh Phung --- CHANGELOG.md | 4 +- README.md | 12 ++- lib/DBSQLClient.ts | 13 +-- lib/DBSQLOperation.ts | 6 +- lib/DBSQLSession.ts | 26 +++--- lib/utils/CloseableCollection.ts | 38 +------- tests/unit/DBSQLClient.test.ts | 89 ------------------- tests/unit/DBSQLOperation.test.ts | 23 +---- tests/unit/DBSQLSession.test.ts | 92 -------------------- tests/unit/kernel/execution.test.ts | 17 ---- tests/unit/utils/CloseableCollection.test.ts | 32 +------ 11 files changed, 27 insertions(+), 325 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 97a56270..0e93656e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,9 +2,7 @@ ## Unreleased -- `DBSQLClient`, sessions, and operations now implement `Symbol.asyncDispose`, so `await using` closes them automatically; disposing an already-closed object is a no-op. The `IDBSQLClient`, `IDBSQLSession`, and `IOperation` interfaces and the return types of `connect()`, `openSession()`, and the session's operation methods are unchanged, so existing custom implementations, mocks, and subclasses keep compiling. Because those methods return the interface types, wrap their results with the new `asyncDisposable()` helper to bind them with `await using` in TypeScript (`await using session = asyncDisposable(await client.openSession())`); it returns the driver's own objects unchanged and gives a custom implementation without the method a `[Symbol.asyncDispose]()` that calls `close()`. The typings declare `Symbol.asyncDispose` themselves, so calling it explicitly doesn't need the `esnext.disposable` lib. (#331) -- `DBSQLSession.close()` and `DBSQLClient.close()` now attempt every cleanup step even if one fails: an operation that fails to close no longer prevents closing its sibling operations, the session's backend session, or the client's backend, feature-flag, and telemetry resources. A single failure is rethrown unchanged once cleanup completes; several failures are reported as an `AggregateError`. A client whose backend fails to close is now also reset, like a successfully closed one. -- Closing a cancelled operation now releases its backend handle (Thrift `CloseOperation` / kernel statement close) instead of being a no-op, and a cancelled operation stays tracked by its session until it is closed, so `session.close()` and `await using` release it too. +- Add `Symbol.asyncDispose` to clients, sessions, and operations, delegating to `close()`. Use `asyncDisposable()` with interface-typed results in TypeScript; existing interfaces and factory return types remain unchanged. (#331) - CloudFetch retry-exhaustion errors now identify the download and the last failure: the `RetryError` message reads `CloudFetch result download failed: (last error: )`. `` is `HTTP : ` (or `HTTP ` when the status text is empty) for HTTP failures, and `network error `, `network error ()` (e.g. `aborted`, when there is no `code`) or a bare `network error` for network failures (the raw network-error message, which can contain the presigned URL, is not copied). If no cause can be determined, the ` (last error: …)` suffix is omitted. The `RetryError` instance, `errorCode` and `payload` (including the node-fetch `Request` / `Response`) are unchanged. (#471) - Report untyped NULL columns as STRING in kernel result metadata, matching Thrift. (#470) - Reject non-scalar query parameter values at bind time with a `ParameterError` on both the Thrift and kernel backends, instead of stringifying them (which could leave a Thrift operation stuck before FINISHED). Newly rejected on the Thrift backend: arrays, plain objects, boxed primitives (e.g. `new String('x')`, `new Number(1)`), functions, symbols, and a `DBSQLParameter` that wraps another `DBSQLParameter`. Primitives, `Date`, `Int64`, and `DBSQLParameter` are still accepted. diff --git a/README.md b/README.md index 1f08c55d..0fc3eafc 100644 --- a/README.md +++ b/README.md @@ -53,12 +53,10 @@ client ### Automatic cleanup with `await using` -The client, sessions, and operations implement `Symbol.asyncDispose`, so an `await using` declaration closes them when -it goes out of scope, even if an error is thrown (disposing an already-closed object is a no-op). Closing is -best-effort: if an operation fails to close, its session and the client are still closed, and the failure is rethrown -once the remaining cleanup has been attempted (several failures from one `close()` are reported as an -`AggregateError`). If both the block and a disposal throw, `await using` throws a `SuppressedError` whose `error` is the -disposal failure and whose `suppressed` is the block's error. Node.js 24+ supports +The client, sessions, and operations implement `Symbol.asyncDispose`. On scope exit, `await using` calls `close()`, +including when the block throws. Disposal follows the existing `close()` behavior, and cleanup errors propagate. +If both the block and a disposal fail, `await using` throws a `SuppressedError` containing both errors. +Node.js 24+ supports `await using` natively; on Node.js 22, compile it with TypeScript 5.2+. TypeScript also needs the disposable declarations (such as the global `AsyncDisposable` type) to compile `await using`: if your typings don't already provide them, add `"esnext.disposable"` (or `"esnext"`) to `compilerOptions.lib` in your `tsconfig.json`, otherwise the @@ -79,7 +77,7 @@ async function main() { await using session = asyncDisposable(await client.openSession()); await using operation = asyncDisposable(await session.executeStatement('SELECT "Hello, World!"')); console.table(await operation.fetchAll()); -} // closes `operation`, `session`, then `client` +} // disposes `operation`, `session`, then `client` ``` ## Configuration diff --git a/lib/DBSQLClient.ts b/lib/DBSQLClient.ts index 04bd0065..a287d4f0 100644 --- a/lib/DBSQLClient.ts +++ b/lib/DBSQLClient.ts @@ -33,7 +33,7 @@ import { } from './connection/auth/tokenProvider'; import IDBSQLLogger, { LogLevel } from './contracts/IDBSQLLogger'; import DBSQLLogger from './DBSQLLogger'; -import CloseableCollection, { throwCollectedErrors } from './utils/CloseableCollection'; +import CloseableCollection from './utils/CloseableCollection'; import IConnectionProvider from './connection/contracts/IConnectionProvider'; import TelemetryClient from './telemetry/TelemetryClient'; import TelemetryClientProvider from './telemetry/TelemetryClientProvider'; @@ -923,18 +923,9 @@ export default class DBSQLClient extends EventEmitter implements IDBSQLClient, I * method on shutdown so the aggregator drains its remaining metrics. */ public async close(): Promise { - // A failing session must not prevent releasing the backend and telemetry: attempt every - // cleanup step and report the collected failures at the end. - const errors: Array = []; try { await this.sessions.closeAll(); - } catch (error) { - errors.push(error); - } - try { await this.backend?.close(); - } catch (error) { - errors.push(error); } finally { this.releaseFeatureFlagCache(); } @@ -960,8 +951,6 @@ export default class DBSQLClient extends EventEmitter implements IDBSQLClient, I this.client = undefined; this.connectionProvider = undefined; this.authProvider = undefined; - - throwCollectedErrors(errors, 'Failed to close client'); } /** diff --git a/lib/DBSQLOperation.ts b/lib/DBSQLOperation.ts index d3497744..0f693991 100644 --- a/lib/DBSQLOperation.ts +++ b/lib/DBSQLOperation.ts @@ -209,8 +209,7 @@ export default class DBSQLOperation implements IOperation { } const result = await this.backend.cancel(); this.cancelled = true; - // Cancelling doesn't release the backend handle, so the operation stays registered with its - // session until it's closed (explicitly, by `await using`, or when the session is closed). + this.onClose?.(); return result; } @@ -223,8 +222,7 @@ export default class DBSQLOperation implements IOperation { } private async closeInternal(): Promise { - // A cancelled operation still holds a backend handle, so only an already closed one is a no-op. - if (this.closed) { + if (this.closed || this.cancelled) { return Status.success(); } const result = await this.backend.close(); diff --git a/lib/DBSQLSession.ts b/lib/DBSQLSession.ts index be0f5106..cc567e64 100644 --- a/lib/DBSQLSession.ts +++ b/lib/DBSQLSession.ts @@ -19,7 +19,7 @@ import IOperation from './contracts/IOperation'; import DBSQLOperation from './DBSQLOperation'; import Status from './dto/Status'; import InfoValue from './dto/InfoValue'; -import CloseableCollection, { throwCollectedErrors } from './utils/CloseableCollection'; +import CloseableCollection from './utils/CloseableCollection'; import { LogLevel } from './contracts/IDBSQLLogger'; import HiveDriverError from './errors/HiveDriverError'; import ParameterError from './errors/ParameterError'; @@ -330,14 +330,7 @@ export default class DBSQLSession implements IDBSQLSession { return Status.success(); } - // A failing operation must not prevent closing the session itself: attempt every - // cleanup step and report the collected failures at the end. - const errors: Array = []; - try { - await this.operations.closeAll(); - } catch (err) { - errors.push(err); - } + await this.operations.closeAll(); // `latencyMs` on CONNECTION_CLOSE is the closeSession RPC duration, not the // session lifetime. It's the symmetric counterpart of CREATE_SESSION's @@ -356,21 +349,22 @@ export default class DBSQLSession implements IDBSQLSession { }); }; - let status: Status | undefined; try { closeStart = Date.now(); - status = await this.backend.close(); + const status = await this.backend.close(); + + this.onClose?.(); + this.isOpen = false; + emitClose(); + this.context.getLogger().log(LogLevel.debug, `Session closed with id: ${this.id}`); + return status; } catch (err) { - errors.push(err); - } finally { this.onClose?.(); this.isOpen = false; emitClose(); + throw err; } - - throwCollectedErrors(errors, `Failed to close session with id: ${this.id}`); - return status ?? Status.success(); } /** diff --git a/lib/utils/CloseableCollection.ts b/lib/utils/CloseableCollection.ts index 4d89bc70..91b8bfcf 100644 --- a/lib/utils/CloseableCollection.ts +++ b/lib/utils/CloseableCollection.ts @@ -21,44 +21,12 @@ export default class CloseableCollection { this.items.delete(item); } - /** - * Attempts to close every item, even if some of them fail. Successfully closed items are removed from the - * collection; failed ones are kept. Failures are reported once all items have been attempted - * (see {@link throwCollectedErrors}). - */ public async closeAll() { - const errors: Array = []; const items = [...this.items]; for (const item of items) { - try { - await item.close(); // eslint-disable-line no-await-in-loop - item.onClose = undefined; - this.items.delete(item); - } catch (error) { - errors.push(error); - } + await item.close(); // eslint-disable-line no-await-in-loop + item.onClose = undefined; + this.items.delete(item); } - throwCollectedErrors(errors, 'Failed to close some resources'); - } -} - -// `AggregateError` is available in every supported Node.js version, but isn't part of the ES2018 lib typings -// this project compiles against, so reference it with a local type instead of a global declaration. -const AggregateErrorConstructor = ( - globalThis as unknown as { - AggregateError: new (errors: Iterable, message?: string) => Error & { errors: Array }; - } -).AggregateError; - -/** - * Rethrows the collected cleanup errors: nothing if there are none, the error itself if there is exactly one - * (so callers see the same error as before), or an `AggregateError` holding all of them otherwise. - */ -export function throwCollectedErrors(errors: Array, message: string): void { - if (errors.length === 1) { - throw errors[0]; - } - if (errors.length > 1) { - throw new AggregateErrorConstructor(errors, message); } } diff --git a/tests/unit/DBSQLClient.test.ts b/tests/unit/DBSQLClient.test.ts index f20495da..f955e57d 100644 --- a/tests/unit/DBSQLClient.test.ts +++ b/tests/unit/DBSQLClient.test.ts @@ -4,7 +4,6 @@ import fs from 'fs'; import { Response } from 'node-fetch'; import DBSQLClient, { ThriftLibrary } from '../../lib/DBSQLClient'; import DBSQLSession from '../../lib/DBSQLSession'; -import asyncDisposable from '../../lib/asyncDisposable'; import ThriftBackend from '../../lib/thrift-backend/ThriftBackend'; import KernelBackend from '../../lib/kernel/KernelBackend'; import * as KernelNativeLoader from '../../lib/kernel/KernelNativeLoader'; @@ -621,94 +620,6 @@ describe('DBSQLClient[Symbol.asyncDispose]', () => { await new DBSQLClient()[Symbol.asyncDispose](); }); - - it('should release every resource when an operation always fails to close', async () => { - const client = new DBSQLClient(); - const thriftClient = sinon.spy(new ThriftClientStub()); - client['client'] = thriftClient; - client['connectionProvider'] = new ConnectionProviderStub(); - client['authProvider'] = new AuthProviderStub(); - const backend = new ThriftBackend({ context: client, onConnectionEvent: () => {} }); - client['backend'] = backend; - const backendClose = sinon.spy(backend, 'close'); - const releaseFeatureFlagCache = sinon.spy(client as any, 'releaseFeatureFlagCache'); - // Simulate an initialized telemetry client so `close()` has to release it - client['host'] = 'test.cloud.databricks.com'; - client['telemetryClient'] = {} as any; - const releaseClient = sinon.stub(TelemetryClientProvider.getInstance(), 'releaseClient').resolves(); - - try { - const session = asyncDisposable(await client.openSession()); - const operation = asyncDisposable(await session.executeStatement('SELECT 1')); - const error = new Error('operation close failed'); - const operationClose = sinon.stub((operation as any).backend, 'close').rejects(error); - - // Mirrors the README's nested `await using`: operation, then session, then client are disposed - const disposalErrors: Array = []; - for (const resource of [operation, session, client]) { - try { - await resource[Symbol.asyncDispose](); // eslint-disable-line no-await-in-loop - } catch (e) { - disposalErrors.push(e); - } - } - - // The operation failure is reported by its own disposal and by the session's cleanup... - expect(disposalErrors).to.deep.equal([error, error]); - // ...but it's attempted only by those two, and never again by the client - expect(operationClose.callCount).to.eq(2); - // The session, backend, feature flags and telemetry are all still released - expect(thriftClient.CloseSession.calledOnce).to.be.true; - expect(client['sessions']['items'].size).to.eq(0); - expect(backendClose.calledOnce).to.be.true; - expect(releaseFeatureFlagCache.calledOnce).to.be.true; - expect(releaseClient.calledOnceWith(client, 'test.cloud.databricks.com')).to.be.true; - expect(client['backend']).to.be.undefined; - expect(client['client']).to.be.undefined; - expect(client['telemetryClient']).to.be.undefined; - } finally { - releaseClient.restore(); - } - }); - - it('should release the backend and telemetry and report failures when a session fails to close', async () => { - const client = new DBSQLClient(); - const thriftClient = sinon.spy(new ThriftClientStub()); - client['client'] = thriftClient; - client['connectionProvider'] = new ConnectionProviderStub(); - client['authProvider'] = new AuthProviderStub(); - const backend = new ThriftBackend({ context: client, onConnectionEvent: () => {} }); - client['backend'] = backend; - const sessionError = new Error('session close failed'); - const backendError = new Error('backend close failed'); - const backendClose = sinon.stub(backend, 'close').rejects(backendError); - client['host'] = 'test.cloud.databricks.com'; - client['telemetryClient'] = {} as any; - const releaseClient = sinon.stub(TelemetryClientProvider.getInstance(), 'releaseClient').resolves(); - - try { - const session = await client.openSession(); - sinon.stub(session, 'close').rejects(sessionError); - - try { - await client[Symbol.asyncDispose](); - expect.fail('It should throw an error'); - } catch (e) { - if (e instanceof AssertionError) { - throw e; - } - // `AggregateError` isn't in the project's ES2018 lib typings - expect(e).to.be.instanceOf((globalThis as any).AggregateError); - expect((e as any).errors).to.deep.equal([sessionError, backendError]); - } - expect(backendClose.calledOnce).to.be.true; - expect(releaseClient.calledOnce).to.be.true; - expect(client['backend']).to.be.undefined; - expect(client['telemetryClient']).to.be.undefined; - } finally { - releaseClient.restore(); - } - }); }); describe('DBSQLClient.createAuthProvider', () => { diff --git a/tests/unit/DBSQLOperation.test.ts b/tests/unit/DBSQLOperation.test.ts index b1c0567d..078fe639 100644 --- a/tests/unit/DBSQLOperation.test.ts +++ b/tests/unit/DBSQLOperation.test.ts @@ -338,7 +338,7 @@ describe('DBSQLOperation', () => { expect(operation['closed']).to.be.true; }); - it('should release the backend handle of a cancelled operation', async () => { + it('should return immediately if already cancelled', async () => { const context = new ClientContextStub(); const driver = sinon.spy(context.driver); const operation = createOperationForTest({ handle: operationHandleStub({ hasResultSet: true }), context }); @@ -352,12 +352,9 @@ describe('DBSQLOperation', () => { expect(operation['closed']).to.be.false; await operation.close(); - expect(driver.closeOperation.callCount).to.be.equal(1); + expect(driver.closeOperation.callCount).to.be.equal(0); expect(operation['cancelled']).to.be.true; - expect(operation['closed']).to.be.true; - - await operation.close(); - expect(driver.closeOperation.callCount).to.be.equal(1); + expect(operation['closed']).to.be.false; }); it('should initialize from directResults', async () => { @@ -444,20 +441,6 @@ describe('DBSQLOperation', () => { expect(driver.closeOperation.callCount).to.be.equal(1); }); - it('should release the backend handle of a cancelled operation exactly once', async () => { - const context = new ClientContextStub(); - const driver = sinon.spy(context.driver); - const cancelled = createOperationForTest({ handle: operationHandleStub({ hasResultSet: true }), context }); - - await cancelled.cancel(); - expect(driver.closeOperation.callCount).to.be.equal(0); - await cancelled[Symbol.asyncDispose](); - expect(driver.closeOperation.callCount).to.be.equal(1); - expect(cancelled['closed']).to.be.true; - await cancelled[Symbol.asyncDispose](); - expect(driver.closeOperation.callCount).to.be.equal(1); - }); - it('should propagate close errors', async () => { const context = new ClientContextStub(); context.driver.closeOperationResp.status.statusCode = TStatusCode.ERROR_STATUS; diff --git a/tests/unit/DBSQLSession.test.ts b/tests/unit/DBSQLSession.test.ts index bd13c004..36ab26e9 100644 --- a/tests/unit/DBSQLSession.test.ts +++ b/tests/unit/DBSQLSession.test.ts @@ -5,7 +5,6 @@ import DBSQLSession, { numberToInt64 } from '../../lib/DBSQLSession'; import InfoValue from '../../lib/dto/InfoValue'; import Status from '../../lib/dto/Status'; import DBSQLOperation from '../../lib/DBSQLOperation'; -import asyncDisposable from '../../lib/asyncDisposable'; import { PrimaryKeysRequest } from '../../lib/contracts/IDBSQLSession'; import ISessionBackend from '../../lib/contracts/ISessionBackend'; import ParameterError from '../../lib/errors/ParameterError'; @@ -805,96 +804,5 @@ describe('DBSQLSession', () => { // A failed close still marks the session closed, so disposing again is a no-op. await session[Symbol.asyncDispose](); }); - - it('should close the session even if closing an operation fails', async () => { - const context = new ClientContextStub(); - const closeSession = sinon.spy(context.driver, 'closeSession'); - const error = new Error('operation close failed'); - const originalCloseOperation = context.driver.closeOperation.bind(context.driver); - // Calls 1 and 2 close the failing operation (its own disposal, then the session's cleanup); - // call 3 closes the other operation, which succeeds. - let closeOperationCalls = 0; - const closeOperation = sinon.stub(context.driver, 'closeOperation').callsFake(async (request) => { - closeOperationCalls += 1; - if (closeOperationCalls <= 2) { - throw error; - } - return originalCloseOperation(request); - }); - const session = createSessionForTest({ handle: sessionHandleStub, context }); - const failing = asyncDisposable(await session.executeStatement('SELECT 1')); - const other = await session.executeStatement('SELECT 2'); - if (!(other instanceof DBSQLOperation)) { - expect.fail('Assertion error: operation is not a DBSQLOperation'); - } - - // Mirrors `await using session = ...; await using operation = ...`: the operation is disposed first - await expectFailure(() => failing[Symbol.asyncDispose]()); - try { - await session[Symbol.asyncDispose](); - expect.fail('It should throw an error'); - } catch (e) { - if (e instanceof AssertionError) { - throw e; - } - expect(e).to.equal(error); - } - // The other operation and the session itself were still closed - expect(other['closed']).to.be.true; - expect(session['isOpen']).to.be.false; - expect(closeSession.callCount).to.eq(1); - expect(closeOperation.callCount).to.eq(3); - - // Disposing the closed session again is a no-op - await session[Symbol.asyncDispose](); - expect(closeSession.callCount).to.eq(1); - expect(closeOperation.callCount).to.eq(3); - }); - - it('should report both operation and session close failures', async () => { - const context = new ClientContextStub(); - const operationError = new Error('operation close failed'); - const sessionError = new Error('session close failed'); - sinon.stub(context.driver, 'closeOperation').rejects(operationError); - sinon.stub(context.driver, 'closeSession').rejects(sessionError); - const session = createSessionForTest({ handle: sessionHandleStub, context }); - await session.executeStatement('SELECT 1'); - - try { - await session[Symbol.asyncDispose](); - expect.fail('It should throw an error'); - } catch (e) { - if (e instanceof AssertionError) { - throw e; - } - // `AggregateError` isn't in the project's ES2018 lib typings - expect(e).to.be.instanceOf((globalThis as any).AggregateError); - expect((e as any).errors).to.deep.equal([operationError, sessionError]); - } - expect(session['isOpen']).to.be.false; - }); - - it('should release cancelled operations exactly once', async () => { - const context = new ClientContextStub(); - const driver = sinon.spy(context.driver); - const session = createSessionForTest({ handle: sessionHandleStub, context }); - const operation = await session.executeStatement('SELECT * FROM table'); - if (!(operation instanceof DBSQLOperation)) { - expect.fail('Assertion error: operation is not a DBSQLOperation'); - } - - await operation.cancel(); - // A cancelled operation still holds a backend handle, so the session keeps tracking it - expect(session['operations']['items'].has(operation)).to.be.true; - expect(driver.closeOperation.callCount).to.eq(0); - - await session[Symbol.asyncDispose](); - expect(driver.closeOperation.callCount).to.eq(1); - expect(operation['closed']).to.be.true; - expect(session['operations']['items'].size).to.eq(0); - - await operation[Symbol.asyncDispose](); - expect(driver.closeOperation.callCount).to.eq(1); - }); }); }); diff --git a/tests/unit/kernel/execution.test.ts b/tests/unit/kernel/execution.test.ts index 9f4160ea..360bb5ea 100644 --- a/tests/unit/kernel/execution.test.ts +++ b/tests/unit/kernel/execution.test.ts @@ -1711,21 +1711,4 @@ describe('DBSQLSession / DBSQLOperation over kernel backends — Symbol.asyncDis await operation[Symbol.asyncDispose](); expect(closeSpy.callCount).to.equal(1); }); - - it('disposing a cancelled operation closes the native statement exactly once', async () => { - const connection = new FakeNativeConnection(); - const session = makeFacadeSession(connection); - const operation = asyncDisposable(await session.executeStatement('SELECT 1')); - const closeSpy = sinon.spy(connection.statementToReturn, 'close'); - - await operation.cancel(); - expect(connection.statementToReturn.cancelled).to.equal(true); - expect(closeSpy.callCount).to.equal(0); - - await operation[Symbol.asyncDispose](); - expect(connection.statementToReturn.closed).to.equal(true); - await operation[Symbol.asyncDispose](); - await session[Symbol.asyncDispose](); - expect(closeSpy.callCount).to.equal(1); - }); }); diff --git a/tests/unit/utils/CloseableCollection.test.ts b/tests/unit/utils/CloseableCollection.test.ts index 9a9c86d4..da7ab147 100644 --- a/tests/unit/utils/CloseableCollection.test.ts +++ b/tests/unit/utils/CloseableCollection.test.ts @@ -121,7 +121,7 @@ describe('CloseableCollection', () => { expect(collection['items'].size).to.be.eq(0); }); - it('should attempt to close all items and keep only failed ones', async () => { + it('should close all and delete only first successfully closed items', async () => { const collection = new CloseableCollection(); expect(collection['items'].size).to.be.eq(0); @@ -141,10 +141,8 @@ describe('CloseableCollection', () => { }, }; - let item3Closed = false; const item3: ICloseable = { close() { - item3Closed = true; this.onClose?.(); return Promise.resolve(); }, @@ -165,36 +163,10 @@ describe('CloseableCollection', () => { if (error instanceof AssertionError || !(error instanceof Error)) { throw error; } - // A single failure is rethrown as is expect(error.message).to.eq(errorMessage); expect(item1.onClose).to.be.undefined; expect(item2.onClose).to.be.not.undefined; - // Items after the failed one are still closed - expect(item3Closed).to.be.true; - expect(item3.onClose).to.be.undefined; - expect(collection['items'].size).to.be.eq(1); - expect(collection['items'].has(item2)).to.be.true; - } - }); - - it('should report multiple failures as an AggregateError', async () => { - const collection = new CloseableCollection(); - const error1 = new Error('Error from item 1'); - const error2 = new Error('Error from item 2'); - - collection.add({ close: () => Promise.reject(error1) }); - collection.add({ close: () => Promise.reject(error2) }); - - try { - await collection.closeAll(); - expect.fail('It should throw an error'); - } catch (error) { - if (error instanceof AssertionError) { - throw error; - } - // `AggregateError` isn't in the project's ES2018 lib typings - expect(error).to.be.instanceOf((globalThis as any).AggregateError); - expect((error as any).errors).to.deep.equal([error1, error2]); + expect(item3.onClose).to.be.not.undefined; expect(collection['items'].size).to.be.eq(2); } });