diff --git a/CHANGELOG.md b/CHANGELOG.md index 34532b62..0d6f0499 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ## Unreleased +- Concurrent `IOperation.cancel()` calls on one operation now share a single cancel request, so the Thrift backend sends one `CancelOperation` instead of one per call (the kernel backend already sent one). If that request fails, every waiting call rejects and a later `cancel()` retries. - Thrift CloudFetch downloads are now bounded end to end by `retriesTimeout` (default 15 minutes), including every attempt and retry backoff. Previously a stalled download, where the response headers or body never arrived, blocked the fetch forever. A download still running when the deadline expires is aborted and fails with `RetryError` (`TIMEOUT_EXCEEDED`, message `CloudFetch result download failed: Retry timeout exceeded`). (#520) - Thrift connections made through `proxy` now apply `checkServerCertificate`, `customCaCert` and `clientCert`/`clientKey`. Previously the proxied path ignored them and verified against Node's process-wide trust store, so a certificate outside the configured trust set could be accepted. **Compatibility:** proxied connections now verify unless `checkServerCertificate: false` is set; as on direct connections, `NODE_TLS_REJECT_UNAUTHORIZED=0` no longer turns verification off. For an `https` proxy these options also apply to the proxy's own certificate. (#485) - Honor the client `useCloudFetch: false` default on the kernel backend so complete results can be fetched without cloud-storage downloads. (#574) diff --git a/lib/DBSQLOperation.ts b/lib/DBSQLOperation.ts index 2da1506a..22024b35 100644 --- a/lib/DBSQLOperation.ts +++ b/lib/DBSQLOperation.ts @@ -57,6 +57,9 @@ export default class DBSQLOperation implements IOperation { private cancelled: boolean = false; + // In-flight cancel request, shared by concurrent cancel() calls. + private cancelPromise?: Promise; + public readonly _data?: unknown; private metadata?: ResultMetadata; @@ -207,10 +210,21 @@ export default class DBSQLOperation implements IOperation { if (this.closed || this.cancelled) { return Status.success(); } - const result = await this.backend.cancel(); - this.cancelled = true; - this.onClose?.(); - return result; + // Concurrent callers share one cancel request. It is cleared when it + // settles, so a failed cancel can be retried. + if (!this.cancelPromise) { + this.cancelPromise = (async () => { + try { + const result = await this.backend.cancel(); + this.cancelled = true; + this.onClose?.(); + return result; + } finally { + this.cancelPromise = undefined; + } + })(); + } + return this.cancelPromise; } /** diff --git a/tests/unit/DBSQLOperation.test.ts b/tests/unit/DBSQLOperation.test.ts index e37b5059..275f3d05 100644 --- a/tests/unit/DBSQLOperation.test.ts +++ b/tests/unit/DBSQLOperation.test.ts @@ -255,6 +255,34 @@ describe('DBSQLOperation', () => { expect(operation['closed']).to.be.false; }); + it('should send one cancel request for concurrent calls', async () => { + const context = new ClientContextStub(); + const driver = sinon.spy(context.driver); + const operation = createOperationForTest({ handle: operationHandleStub({ hasResultSet: true }), context }); + + await Promise.all([operation.cancel(), operation.cancel(), operation.cancel()]); + + expect(driver.cancelOperation.callCount).to.be.equal(1); + expect(operation['cancelled']).to.be.true; + }); + + it('should allow a retry after concurrent calls share a failed cancel', async () => { + const context = new ClientContextStub(); + const driver = sinon.spy(context.driver); + context.driver.cancelOperationResp.status.statusCode = TStatusCode.ERROR_STATUS; + const operation = createOperationForTest({ handle: operationHandleStub({ hasResultSet: true }), context }); + + const results = await Promise.allSettled([operation.cancel(), operation.cancel()]); + expect(results.map((r) => r.status)).to.deep.equal(['rejected', 'rejected']); + expect(driver.cancelOperation.callCount).to.be.equal(1); + expect(operation['cancelled']).to.be.false; + + context.driver.cancelOperationResp.status.statusCode = TStatusCode.SUCCESS_STATUS; + await operation.cancel(); + expect(driver.cancelOperation.callCount).to.be.equal(2); + expect(operation['cancelled']).to.be.true; + }); + it('should return immediately if already closed', async () => { const context = new ClientContextStub(); const driver = sinon.spy(context.driver);