@@ -66,6 +66,8 @@ export default class DBSQLOperation implements IOperation {
6666
6767 private metadata ?: TGetResultSetMetadataResp ;
6868
69+ private metadataPromise ?: Promise < TGetResultSetMetadataResp > ;
70+
6971 private state : TOperationState = TOperationState . INITIALIZED_STATE ;
7072
7173 // Once operation is finished or fails - cache status response, because subsequent calls
@@ -292,6 +294,12 @@ export default class DBSQLOperation implements IOperation {
292294 return false ;
293295 }
294296
297+ // Wait for operation to finish before checking for more rows
298+ // This ensures metadata can be fetched successfully
299+ if ( this . operationHandle . hasResultSet ) {
300+ await this . waitUntilReady ( ) ;
301+ }
302+
295303 // If we fetched all the data from server - check if there's anything buffered in result handler
296304 const resultHandler = await this . getResultHandler ( ) ;
297305 return resultHandler . hasMore ( ) ;
@@ -383,16 +391,33 @@ export default class DBSQLOperation implements IOperation {
383391 }
384392
385393 private async fetchMetadata ( ) {
386- if ( ! this . metadata ) {
394+ // If metadata is already cached, return it immediately
395+ if ( this . metadata ) {
396+ return this . metadata ;
397+ }
398+
399+ // If a fetch is already in progress, wait for it to complete
400+ if ( this . metadataPromise ) {
401+ return this . metadataPromise ;
402+ }
403+
404+ // Start a new fetch and cache the promise to prevent concurrent fetches
405+ this . metadataPromise = ( async ( ) => {
387406 const driver = await this . context . getDriver ( ) ;
388407 const metadata = await driver . getResultSetMetadata ( {
389408 operationHandle : this . operationHandle ,
390409 } ) ;
391410 Status . assert ( metadata . status ) ;
392411 this . metadata = metadata ;
412+ return metadata ;
413+ } ) ( ) ;
414+
415+ try {
416+ return await this . metadataPromise ;
417+ } finally {
418+ // Clear the promise once completed (success or failure)
419+ this . metadataPromise = undefined ;
393420 }
394-
395- return this . metadata ;
396421 }
397422
398423 private async getResultHandler ( ) : Promise < ResultSlicer < any > > {
0 commit comments