From b854e973d5e759fcc03e9dceabe9d06d560e3986 Mon Sep 17 00:00:00 2001 From: Vu Anh Phung Date: Thu, 19 Nov 2026 07:29:10 +0000 Subject: [PATCH] Avoid speculative reads past complete Thrift responses Signed-off-by: Vu Anh Phung Co-authored-by: Isaac --- CHANGELOG.md | 1 + .../connections/ThriftHttpConnection.ts | 8 +- .../connections/ThriftHttpResponse.test.ts | 125 ++++++++++++++++++ 3 files changed, 133 insertions(+), 1 deletion(-) create mode 100644 tests/unit/connection/connections/ThriftHttpResponse.test.ts diff --git a/CHANGELOG.md b/CHANGELOG.md index 938983a3..a0139352 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ## Unreleased +- Avoid constructing an end-of-buffer exception after every complete Thrift response. - The Thrift token federation exchange (`enableTokenFederation`) now goes through the connection's agent, so `proxy`, `checkServerCertificate`, `customCaCert` and `clientCert`/`clientKey` apply to it. Previously it connected directly with Node's default agent, so behind a required proxy the exchange failed and the driver fell back to the original token. (#611) - Kernel backend: a failed status poll while waiting for a statement (a `runAsync: true` statement, or a default execution that outlives the server's inline wait) and a failed result fetch in `hasMoreRows()` now throw the mapped driver error, such as `HiveDriverError: HTTP 500: Internal Server Error` once retries on HTTP 5xx run out, instead of a raw `Error` whose message is the internal `__databricks_error__:{...}` envelope. As in `fetchChunk()`, a failed fetch in `hasMoreRows()` also closes the statement. (#608) - Honor `cloudFetchConcurrentDownloads` when opening kernel sessions, with a default window of 16 and explicit chunk-setting overrides. Values other than positive safe integers raise `HiveDriverError` at session open. diff --git a/lib/connection/connections/ThriftHttpConnection.ts b/lib/connection/connections/ThriftHttpConnection.ts index fa686596..7c6910b6 100644 --- a/lib/connection/connections/ThriftHttpConnection.ts +++ b/lib/connection/connections/ThriftHttpConnection.ts @@ -244,7 +244,9 @@ export default class ThriftHttpConnection extends EventEmitter { }); } - private handleThriftResponse(transportWithData: TTransport) { + private handleThriftResponse( + transportWithData: TTransport & { borrow?(): { readIndex: number; writeIndex: number } }, + ) { if (!this.client) { throw new Thrift.TApplicationException(Thrift.TApplicationExceptionType.INTERNAL_ERROR, 'Client not available'); } @@ -278,6 +280,10 @@ export default class ThriftHttpConnection extends EventEmitter { 'Received a response to an unknown RPC function', ); } + const remaining = transportWithData.borrow?.(); + if (remaining && remaining.readIndex === remaining.writeIndex) { + break; + } } } catch (error) { if (error instanceof InputBufferUnderrunError) { diff --git a/tests/unit/connection/connections/ThriftHttpResponse.test.ts b/tests/unit/connection/connections/ThriftHttpResponse.test.ts new file mode 100644 index 00000000..cc59ed83 --- /dev/null +++ b/tests/unit/connection/connections/ThriftHttpResponse.test.ts @@ -0,0 +1,125 @@ +import { expect } from 'chai'; +import sinon from 'sinon'; +import { TBinaryProtocol, TBufferedTransport, Thrift } from 'thrift'; +import ThriftHttpConnection from '../../../../lib/connection/connections/ThriftHttpConnection'; +import NullRetryPolicy from '../../../../lib/connection/connections/NullRetryPolicy'; + +describe('Thrift HTTP response boundaries', () => { + function reply(sequenceId: number, value: string | null, method = 'Reply'): Buffer { + let result = Buffer.alloc(0); + const output = new TBinaryProtocol( + new TBufferedTransport(undefined, (buffer) => { + result = buffer as Buffer; + }), + ); + output.writeMessageBegin(method, Thrift.MessageType.REPLY, sequenceId); + output.writeBool(value !== null); + if (value !== null) output.writeString(value); + output.writeMessageEnd(); + output.flush(); + return result; + } + + function reader({ legacy = false, decodeError }: { legacy?: boolean; decodeError?: Error } = {}) { + let reads = 0; + class CountingProtocol extends TBinaryProtocol { + readMessageBegin() { + reads += 1; + return super.readMessageBegin(); + } + } + const connection = new ThriftHttpConnection({ + url: 'http://localhost/', + protocol: CountingProtocol, + getRetryPolicy: async () => new NullRetryPolicy(), + }); + const callbacks = [sinon.spy(), sinon.spy(), sinon.spy()]; + const requests: Record = Object.fromEntries( + callbacks.map((callback, index) => [index + 1, callback]), + ); + connection.client = { + _reqs: requests, + recv_Reply(input: TBinaryProtocol, messageType: number, sequenceId: number) { + if (decodeError) throw decodeError; + expect(messageType).to.equal(Thrift.MessageType.REPLY); + const value = input.readBool() ? input.readString() : null; + input.readMessageEnd(); + const callback = requests[sequenceId]; + delete requests[sequenceId]; + callback(null, value); + }, + } as any; + const receive = TBufferedTransport.receiver((input) => { + if (legacy) Object.defineProperty(input, 'borrow', { value: undefined }); + connection['handleThriftResponse'](input); + }, 0); + return { callbacks, requests, receive, reads: () => reads }; + } + + for (const value of [null, '', 'a short result', 'x'.repeat(4096)]) { + it(`does not attempt another message after a complete ${ + value === null ? 'null' : `${value.length}-byte` + } result`, async () => { + const input = reader(); + input.receive(reply(1, value)); + await new Promise((resolve) => setImmediate(resolve)); + expect(input.callbacks[0].calledOnceWithExactly(null, value)).to.equal(true); + expect(input.reads()).to.equal(1); + expect(input.requests).not.to.have.property('-1'); + }); + } + + it('decodes every response in a buffer and dispatches by sequence ID', async () => { + const input = reader(); + input.receive(Buffer.concat([reply(3, 'third'), reply(1, null), reply(2, 'second')])); + await new Promise((resolve) => setImmediate(resolve)); + expect(input.callbacks[0].calledOnceWithExactly(null, null)).to.equal(true); + expect(input.callbacks[1].calledOnceWithExactly(null, 'second')).to.equal(true); + expect(input.callbacks[2].calledOnceWithExactly(null, 'third')).to.equal(true); + expect(input.reads()).to.equal(3); + expect(input.requests).to.deep.equal({}); + }); + + it('keeps the underrun handling for an empty response', () => { + const input = reader(); + input.receive(Buffer.alloc(0)); + expect(input.reads()).to.equal(1); + expect(input.callbacks.every((callback) => callback.notCalled)).to.equal(true); + }); + + it('rolls back a partial response and resumes without repeating completed callbacks', async () => { + const input = reader(); + const partial = reply(2, 'second'); + input.receive(Buffer.concat([reply(1, 'first'), partial.subarray(0, partial.length - 2)])); + await new Promise((resolve) => setImmediate(resolve)); + expect(input.callbacks[0].calledOnceWithExactly(null, 'first')).to.equal(true); + expect(input.callbacks[1].notCalled).to.equal(true); + input.receive(partial.subarray(partial.length - 2)); + await new Promise((resolve) => setImmediate(resolve)); + expect(input.callbacks[0].calledOnce).to.equal(true); + expect(input.callbacks[1].calledOnceWithExactly(null, 'second')).to.equal(true); + }); + + it('keeps the underrun fallback for transports without borrow', async () => { + const input = reader({ legacy: true }); + input.receive(reply(1, 'legacy')); + await new Promise((resolve) => setImmediate(resolve)); + expect(input.callbacks[0].calledOnceWithExactly(null, 'legacy')).to.equal(true); + expect(input.reads()).to.equal(2); + }); + + it('still rejects unknown methods and removes their temporary callback', () => { + const input = reader(); + expect(() => input.receive(reply(1, 'unexpected', 'Unknown'))).to.throw( + 'Received a response to an unknown RPC function', + ); + expect(input.requests).not.to.have.property('-1'); + expect(input.callbacks[0].notCalled).to.equal(true); + }); + + it('does not swallow decoder errors', () => { + const error = new Error('decoder failure'); + const input = reader({ decodeError: error }); + expect(() => input.receive(reply(1, 'value'))).to.throw(error); + }); +});