mssql 12.7.6 → 12.7.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -430,6 +430,12 @@ function processRows() {
430
430
  }
431
431
  ```
432
432
 
433
+ Nothing is emitted while the request is paused: `row`, `recordset`, `info`, `error`, `rowsaffected` and `done`
434
+ events wait until it is resumed. The msnodesqlv8 driver reads rows 50 at a time and only stops reading at the end
435
+ of a full batch, so the rows it reads before then are held until the request is resumed. A paused request that is
436
+ cancelled does not wait to be resumed: it drops the rows it holds, emits the other events it holds, and completes
437
+ with an `error` (`ECANCEL`) and `done` (see [cancel](#cancel)).
438
+
433
439
  ## Connection Pools
434
440
 
435
441
  An important concept to understand when using this library is [Connection Pooling](https://en.wikipedia.org/wiki/Connection_pool) as this library uses connection pooling extensively. As one Node JS process is able to handle multiple requests at once, we can take advantage of this long running process to create a pool of database connections for reuse; this saves overhead of connecting to the database for each request
@@ -1074,6 +1080,12 @@ pipeline(readableStream, transformStream, writableStream)
1074
1080
  request.query('select * from mytable')
1075
1081
  ```
1076
1082
 
1083
+ The readable stream is destroyed with the request's first error. Later errors are still emitted on the request, but not on the stream.
1084
+
1085
+ If the readable stream is destroyed before the request finishes (for example because a later stream in the pipeline failed), the request still runs to the end and the rows it has left are discarded. Call `request.cancel()` to stop it early.
1086
+
1087
+ With msnodesqlv8 before 5.6 that cancel does not stop it. The request is resumed as soon as its stream is destroyed, so that it does not keep its connection, and those versions can only cancel a paused request.
1088
+
1077
1089
 
1078
1090
  ### pipe (stream)
1079
1091
 
@@ -1097,6 +1109,21 @@ stream.on('finish', () => {
1097
1109
  })
1098
1110
  ```
1099
1111
 
1112
+ The stream is destroyed with the request's first error, so listen for `error` on it as above. It does not emit `finish` after an error. The request still runs to the end, and its remaining rows are discarded.
1113
+
1114
+ If the stream fails or closes before the request finishes, or was already destroyed, the request also runs to the end and its remaining rows are discarded, rather than waiting for a stream that will not read any more. Call `request.cancel()` to stop it early, bearing in mind the msnodesqlv8 caveat under [toReadableStream](#toreadablestream).
1115
+
1116
+ `pipe()` passes errors only to the stream it is given. If you pipe through more streams after that one, use `stream.pipeline()` with [`toReadableStream`](#toreadablestream) instead, so that every error reaches you:
1117
+
1118
+ ```javascript
1119
+ const { pipeline } = require('stream')
1120
+ const request = new sql.Request()
1121
+ pipeline(request.toReadableStream(), transformStream, writableStream, err => {
1122
+ // ...
1123
+ })
1124
+ request.query('select * from mytable')
1125
+ ```
1126
+
1100
1127
  ---------------------------------------
1101
1128
 
1102
1129
  ### query (command, [callback])
@@ -1250,7 +1277,7 @@ request.cancel()
1250
1277
 
1251
1278
  A cancelled stream emits an `error` event with code `ECANCEL`, followed by `done`.
1252
1279
 
1253
- With the `msnodesqlv8` driver, a request that is already running can only be cancelled from msnodesqlv8 5.6. Earlier versions can cancel a paused stream, but let any other running request run to the end and complete as if `cancel()` had not been called.
1280
+ With the `msnodesqlv8` driver, a request that is already running can only be cancelled from msnodesqlv8 5.6. Earlier versions can cancel a paused stream, but let any other running request run to the end and complete as if `cancel()` had not been called. A paused stream is cancelled once the driver stops reading, at the end of a batch of 50 rows or the start of the next result set; a query that ends first, or times out, completes as cancelled.
1254
1281
 
1255
1282
  ## Transaction
1256
1283
 
@@ -1354,6 +1381,8 @@ __Errors__
1354
1381
  - ENOTOPEN (`ConnectionError`) - Connection not yet open.
1355
1382
  - EALREADYBEGUN (`TransactionError`) - Transaction has already begun.
1356
1383
 
1384
+ If the transaction fails to begin once it has a connection, the error is a `TransactionError`, with the error it comes from as its `originalError`. The connection goes back to the pool, and the transaction can be begun again. A connection that may have sent the request to begin the transaction is closed rather than lent out again, as the server may have begun the transaction even though the request failed (for example, when it timed out).
1385
+
1357
1386
  ---------------------------------------
1358
1387
 
1359
1388
  ### commit ([callback])
@@ -10,6 +10,17 @@ const { TYPES } = require('../datatypes')
10
10
  const shared = require('../shared')
11
11
  const { CHANNELS, tracePromise, traceCallback, publish } = require('../diagnostics')
12
12
 
13
+ // a Readable runs a single destroy hook, so one the caller passed in its stream options runs after
14
+ // `destroy`, with the stream as `this` as the Readable would have run it. The options are spread to
15
+ // read it, as they are for the Readable, so only an own `destroy` counts and null options are fine
16
+ function withDestroyHook (streamOptions, destroy) {
17
+ const { destroy: hook } = { ...streamOptions }
18
+ if (typeof hook !== 'function') return destroy
19
+ return function (error, callback) {
20
+ destroy(error, () => hook.call(this, error, callback))
21
+ }
22
+ }
23
+
13
24
  /**
14
25
  * Class Request.
15
26
  *
@@ -429,22 +440,36 @@ class Request extends EventEmitter {
429
440
  toReadableStream (streamOptions = {}) {
430
441
  this.stream = true
431
442
  this.pause()
443
+ let requestDone = false
444
+ let requestCanceled = false
432
445
  const readableStream = new Readable({
433
446
  ...streamOptions,
434
447
  objectMode: true,
435
448
  read: (/* size */) => {
436
449
  this.resume()
437
- }
450
+ },
451
+ destroy: withDestroyHook(streamOptions, (error, callback) => {
452
+ // nothing will read from the stream again, so the request must not be left paused: it
453
+ // runs to the end, its remaining rows are dropped and its connection is released. A
454
+ // cancelled request is left to its driver, which is already stopping it: msnodesqlv8
455
+ // reports the cancel once the query is gone, and resuming it then reads a freed statement
456
+ if (!requestDone && !requestCanceled && this.paused) this.resume()
457
+ callback(error)
458
+ })
438
459
  })
439
460
  this.on('row', (row) => {
440
- if (!readableStream.push(row)) {
461
+ if (!readableStream.push(row) && !readableStream.destroyed) {
441
462
  this.pause()
442
463
  }
443
464
  })
444
465
  this.on('error', (error) => {
445
- readableStream.emit('error', error)
466
+ if (error.code === 'ECANCEL') requestCanceled = true
467
+ // the stream ends with the request's first error; destroying an already destroyed stream
468
+ // does nothing, so later errors stay with the request
469
+ readableStream.destroy(error)
446
470
  })
447
471
  this.on('done', () => {
472
+ requestDone = true
448
473
  readableStream.push(null)
449
474
  })
450
475
  return readableStream
@@ -453,6 +478,8 @@ class Request extends EventEmitter {
453
478
  /**
454
479
  * Wrap original request in a Readable stream that supports back pressure and pipe to the Writable stream.
455
480
  * It also sets request to `stream` mode and pulls all rows from all recordsets to a given stream.
481
+ * The Writable stream is destroyed with the request's first error. If the Writable stream fails or
482
+ * closes first, the request runs to the end and its remaining rows are discarded.
456
483
  *
457
484
  * @param {Stream} stream Stream to pipe data into.
458
485
  * @return {Stream}
@@ -460,6 +487,20 @@ class Request extends EventEmitter {
460
487
 
461
488
  pipe (writableStream) {
462
489
  const readableStream = this.toReadableStream()
490
+ // readable.pipe() leaves the source's errors to the source, but only the destination is
491
+ // handed back, so pass them on to it as pipeline() does
492
+ readableStream.on('error', (error) => {
493
+ writableStream.destroy(error)
494
+ })
495
+ // and when the destination fails or closes first, readable.pipe() only unpipes from it, which
496
+ // leaves the request paused with its connection: destroy the stream so the request runs to
497
+ // the end. 'unpipe' comes even from a destination that never emits 'close', and unlike
498
+ // finished() it adds no 'error' listener, which would stop an unhandled failure being thrown
499
+ writableStream.on('unpipe', (source) => {
500
+ if (source === readableStream && !readableStream.readableEnded) readableStream.destroy()
501
+ })
502
+ // a destination that is already destroyed will not fail or close again
503
+ if (writableStream.destroyed) readableStream.destroy()
463
504
  return readableStream.pipe(writableStream)
464
505
  }
465
506
 
@@ -14,6 +14,12 @@ const JSON_COLUMN_ID = 'JSON_F52E2B61-18A1-11d1-B105-00805F49916B'
14
14
  const XML_COLUMN_ID = 'XML_F52E2B61-18A1-11d1-B105-00805F49916B'
15
15
  const EMPTY_BUFFER = Buffer.alloc(0)
16
16
 
17
+ // msnodesqlv8 fetches a result set's rows this many at a time. It is only safe to pause once
18
+ // it has handed over every row of the batch it fetched: paused part way through a result set's
19
+ // last, short batch, it moves on to the next result set, or ends the query, with the rest of
20
+ // that batch still to come.
21
+ const DRIVER_ROW_BATCH_SIZE = 50
22
+
17
23
  // String() converts a Symbol, which only fails to convert implicitly; an object without a
18
24
  // working toString falls back to its tag
19
25
  const thrownMessage = function (value) {
@@ -24,6 +30,12 @@ const thrownMessage = function (value) {
24
30
  }
25
31
  }
26
32
 
33
+ // msnodesqlv8 >= 3 keeps the reader of a query on it, and the reader stops running as soon as
34
+ // the query ends, a few ticks before msnodesqlv8 reports the end with 'done' or 'free'
35
+ const stopped = function (query) {
36
+ return query.queryWorker?.running === false
37
+ }
38
+
27
39
  const castParameter = function (value, type) {
28
40
  if (value == null) {
29
41
  if ((type === TYPES.Binary) || (type === TYPES.VarBinary) || (type === TYPES.Image)) {
@@ -343,10 +355,44 @@ class Request extends BaseRequest {
343
355
  // set while the error just collected is being dispatched, along with whatever came with it
344
356
  let failing = false
345
357
  let cancelRequested = false
346
- // 'done' ends the request. msnodesqlv8 >= 5.6 emits it as soon as a cancel is through, and
347
- // the query can go on emitting after it, so the listeners ignore anything that follows
358
+ // 'done' or 'free' ends the request. msnodesqlv8 >= 5.6 emits 'done' as soon as a cancel is
359
+ // through, and the query can go on emitting after it, so the listeners ignore anything that
360
+ // follows
348
361
  let finished = false
349
362
 
363
+ // A stream goes on receiving rows after it is paused, until the driver reaches the end of a
364
+ // full batch (see DRIVER_ROW_BATCH_SIZE). What the stream emits waits here while it is
365
+ // paused, and is emitted in the order it arrived once it is resumed.
366
+ const pending = []
367
+ let rowsInRecordset = 0
368
+ let driverPaused = false
369
+ // a cancel of a paused stream waits for the driver to reach a point where it can be paused
370
+ let cancelDeferred = false
371
+ // msnodesqlv8 has freed the statement, which it does without 'done' after an error ends the query
372
+ let freed = false
373
+ let resuming = false
374
+ // a refused cancel undoes only the latest one
375
+ let cancelAttempts = 0
376
+
377
+ const emitInOrder = (isRow, emit) => {
378
+ if (this.stream && (this.paused || pending.length)) {
379
+ pending.push({ isRow, emit })
380
+ } else {
381
+ emit()
382
+ }
383
+ }
384
+
385
+ // once a cancel is through, what is waiting is emitted at once, but for the rows of a
386
+ // paused stream, which are dropped
387
+ const emitPendingOnCancel = () => {
388
+ // decided once, so that a listener that resumes the stream part way through cannot let
389
+ // the rows of the cancelled stream through
390
+ const dropRows = this.paused
391
+ for (const { isRow, emit } of pending.splice(0)) {
392
+ if (!isRow || !dropRows) emit()
393
+ }
394
+ }
395
+
350
396
  const handleError = (req, connection, info, moreErrors) => {
351
397
  // a cancel is reported from 'done'. msnodesqlv8 >= 5.6 also sends an 'Operation canceled'
352
398
  // error after a cancel, and one that arrives before 'done' is dropped here
@@ -359,7 +405,7 @@ class Request extends BaseRequest {
359
405
  err.code = 'EREQUEST'
360
406
 
361
407
  if (this.stream) {
362
- this.emit('error', err)
408
+ emitInOrder(false, () => this.emit('error', err))
363
409
  } else {
364
410
  if (doReturn && !hasReturned) {
365
411
  if (req) {
@@ -493,32 +539,144 @@ class Request extends BaseRequest {
493
539
 
494
540
  this._setCurrentRequest(req)
495
541
 
496
- this._cancel = () => {
497
- debug('request(%d): cancel', IDS.get(this))
498
- // newer msnodesqlv8 versions drop a repeat cancel without calling back, so the
499
- // outcome of the first one stands
500
- if (typeof req.getCancelSent === 'function' && req.getCancelSent()) {
501
- return
502
- }
503
- cancelRequested = true
542
+ // msnodesqlv8 does not check that a query is still running when it is paused or resumed.
543
+ // Pausing and resuming one that has ended puts it back on its connection's work queue,
544
+ // where it holds up whatever that connection is sent next, and resuming it can set it
545
+ // reading again from a statement that is gone, or crash the process. So once msnodesqlv8
546
+ // reports that the query has ended, the request no longer pauses, resumes or cancels it,
547
+ // before anything the request emits from then on is handled. Before then it leaves the
548
+ // query alone while an error that may end the batch is being handled and, from msnodesqlv8
549
+ // 3, once the query's reader has stopped
550
+ let detached = false
551
+ let settingAside = false
552
+ let cancelHeld = false
553
+ const detach = () => {
554
+ detached = true
555
+ }
556
+ const reachable = () => !detached && !settingAside && !stopped(req)
557
+
558
+ const cancelDriver = (attempt = cancelAttempts) => {
504
559
  req.cancelQuery(err => {
505
560
  if (err) {
506
561
  debug('request(%d): failed to cancel', IDS.get(this), err)
507
562
  // msnodesqlv8 < 5.6 refuses to cancel a query that is neither paused nor polling,
508
- // and the query runs to the end
509
- if (!hasReturned) cancelRequested = false
563
+ // and the query runs to the end; if it reached the end of a batch meanwhile, a
564
+ // stream that is no longer paused resumes it
565
+ if (!hasReturned && attempt === cancelAttempts) {
566
+ cancelRequested = false
567
+ this._resumeStream()
568
+ }
510
569
  }
511
- // this fixes an issue where paused connections don't emit a done event
570
+ // this fixes an issue where paused connections don't emit a done event; a query whose
571
+ // cancel was refused goes on, though it may have paused at the end of a batch since
512
572
  try {
513
- if (req.isPaused()) req.emit('done')
573
+ if (!err && req.isPaused()) req.emit('done')
514
574
  } catch (err) {
515
575
  // do nothing
516
576
  }
517
577
  })
518
578
  }
519
579
 
580
+ // every version of msnodesqlv8 cancels a paused query; it is paused here only where it stops
581
+ // for a pause, with nothing of its own under way
582
+ const pauseAndCancelDriver = () => {
583
+ cancelDeferred = false
584
+ // msnodesqlv8 5.0 to 5.5 send a query one cancel, which a cancel made before the query
585
+ // started has used up and they refused, so the query runs on
586
+ if (typeof req.getCancelSent === 'function' && req.getCancelSent()) {
587
+ cancelRequested = false
588
+ return false
589
+ }
590
+ driverPaused = true
591
+ req.pauseQuery()
592
+ cancelDriver()
593
+ return true
594
+ }
595
+
596
+ // called with each row once it is complete
597
+ const pauseDriverAtEndOfBatch = () => {
598
+ if (driverPaused || rowsInRecordset % DRIVER_ROW_BATCH_SIZE !== 0) return
599
+ if (cancelDeferred && pauseAndCancelDriver()) return
600
+ if (this.stream && this.paused) {
601
+ driverPaused = true
602
+ req.pauseQuery()
603
+ }
604
+ }
605
+
606
+ // until the first result set the driver has read nothing, so a stream paused before then,
607
+ // or before its query starts, holds it back at once
608
+ this._pauseStream = () => {
609
+ if (!columns && !driverPaused && !finished && reachable()) {
610
+ driverPaused = true
611
+ req.pauseQuery()
612
+ }
613
+ }
614
+ if (this.stream && this.paused) this._pauseStream()
615
+
616
+ this._resumeStream = () => {
617
+ // a listener that resumes the stream while it is emitting what was waiting leaves the
618
+ // rest to this loop, so that every listener gets the events in order
619
+ if (resuming) return
620
+ resuming = true
621
+ try {
622
+ while (pending.length && !this.paused) {
623
+ pending.shift().emit()
624
+ }
625
+ } finally {
626
+ resuming = false
627
+ }
628
+ // cancelled while it emitted what was waiting after the query ended, and then paused, a
629
+ // stream completes as cancelled straight away, as it does when it is paused first
630
+ if (finished && cancelRequested && this.paused && pending.length) emitPendingOnCancel()
631
+ // a query that is being cancelled stays paused, so that the cancel can go through
632
+ if (!pending.length && !this.paused && driverPaused && !finished && !cancelRequested && reachable()) {
633
+ driverPaused = false
634
+ req.resumeQuery()
635
+ }
636
+ }
637
+
638
+ const cancel = () => {
639
+ debug('request(%d): cancel', IDS.get(this))
640
+ // a query that has ended has nothing to cancel, nor one whose reader has stopped. The
641
+ // cancel is held: it is made if msnodesqlv8 goes on to a result set after all, and once
642
+ // the query is through a paused stream completes as cancelled, without waiting to be
643
+ // resumed
644
+ if (detached || stopped(req) || settingAside) {
645
+ cancelHeld = true
646
+ return
647
+ }
648
+ // newer msnodesqlv8 versions drop a repeat cancel without calling back, so the
649
+ // outcome of the first one stands
650
+ if (cancelDeferred || (typeof req.getCancelSent === 'function' && req.getCancelSent())) {
651
+ return
652
+ }
653
+ cancelRequested = true
654
+ cancelAttempts++
655
+ if (!this.stream || !this.paused || driverPaused) {
656
+ return cancelDriver()
657
+ }
658
+ // A paused stream holds the driver back only once it reaches the end of a batch. Until
659
+ // then the driver goes on reading, and pausing or cancelling it part way through a fetch,
660
+ // or as it moves to the next result set or ends the query, can leave its queue broken.
661
+ // So it is paused and cancelled when it next stops, at the end of a batch or the start
662
+ // of a result set, and a query that ends first, at the latest on the request timeout,
663
+ // is reported as cancelled.
664
+ if (freed) return req.emit('done')
665
+ cancelDeferred = true
666
+ }
667
+ this._cancel = cancel
668
+
520
669
  req.on('meta', metadata => {
521
670
  if (finished) return
671
+ // a query that goes on to a result set has not ended: msnodesqlv8 can give the last error
672
+ // of a statement that fails first more=false, and carry on to the statements after it
673
+ if (detached) {
674
+ detached = false
675
+ if (cancelHeld) {
676
+ cancelHeld = false
677
+ cancel()
678
+ }
679
+ }
522
680
 
523
681
  if (row) {
524
682
  if (isChunkedRecordset) {
@@ -536,7 +694,7 @@ class Request extends BaseRequest {
536
694
  const ex2 = new RequestError(`Failed to parse incoming JSON. ${ex.message}`, 'EJSON')
537
695
 
538
696
  if (this.stream) {
539
- this.emit('error', ex2)
697
+ emitInOrder(false, () => this.emit('error', ex2))
540
698
  } else {
541
699
  console.error(ex2)
542
700
  }
@@ -548,12 +706,14 @@ class Request extends BaseRequest {
548
706
  chunksBuffer = null
549
707
  if (row && row.___return___ == null) {
550
708
  // row with ___return___ col is the last row
551
- if (this.stream && !this.paused) this.emit('row', row)
709
+ const last = row
710
+ if (this.stream) emitInOrder(true, () => this.emit('row', last))
552
711
  }
553
712
  }
554
713
  }
555
714
 
556
715
  row = null
716
+ rowsInRecordset = 0
557
717
  columns = metadata
558
718
  recordset = []
559
719
 
@@ -588,16 +748,23 @@ class Request extends BaseRequest {
588
748
  }
589
749
  if (this.stream) {
590
750
  if (!hasReturnColumn) {
591
- this.emit('recordset', recordset.columns)
751
+ const recordsetColumns = recordset.columns
752
+ emitInOrder(false, () => this.emit('recordset', recordsetColumns))
592
753
  }
593
754
  } else {
594
755
  recordsets.push(recordset)
595
756
  }
596
757
  if (this.arrayRowMode) recordsetcolumns.push(recordset.columns)
758
+
759
+ // the driver fetches nothing of a result set it is paused at the start of
760
+ if (cancelDeferred) pauseAndCancelDriver()
597
761
  })
598
762
 
599
763
  req.on('row', rownumber => {
600
- if (finished || (row && isChunkedRecordset)) return
764
+ if (finished) return
765
+
766
+ rowsInRecordset++
767
+ if (row && isChunkedRecordset) return
601
768
 
602
769
  if (this.arrayRowMode) {
603
770
  row = []
@@ -642,11 +809,14 @@ class Request extends BaseRequest {
642
809
  }
643
810
  }
644
811
  if (!hasReturnColumn) {
645
- if (this.stream && !this.paused && idx === columns.length - 1) {
646
- this.emit('row', row)
812
+ if (this.stream && idx === columns.length - 1) {
813
+ const complete = row
814
+ emitInOrder(true, () => this.emit('row', complete))
647
815
  }
648
816
  }
649
817
  }
818
+
819
+ if (idx === columns.length - 1) pauseDriverAtEndOfBatch()
650
820
  })
651
821
 
652
822
  req.on('rowcount', rowCount => {
@@ -654,7 +824,7 @@ class Request extends BaseRequest {
654
824
 
655
825
  rowsAffected.push(rowCount)
656
826
  if (this.stream) {
657
- this.emit('rowsaffected', rowCount)
827
+ emitInOrder(false, () => this.emit('rowsaffected', rowCount))
658
828
  }
659
829
  })
660
830
 
@@ -666,7 +836,7 @@ class Request extends BaseRequest {
666
836
  msg.message = parsedMessage[1]
667
837
  }
668
838
 
669
- this.emit('info', {
839
+ const info = {
670
840
  message: msg.message,
671
841
  number: msg.code,
672
842
  state: msg.sqlstate,
@@ -674,7 +844,8 @@ class Request extends BaseRequest {
674
844
  lineNumber: msg.lineNumber || 0,
675
845
  serverName: msg.serverName,
676
846
  procName: msg.procName
677
- })
847
+ }
848
+ emitInOrder(false, () => this.emit('info', info))
678
849
 
679
850
  // query terminated
680
851
  if (msg.code === 3621 && !hasReturned) {
@@ -682,6 +853,7 @@ class Request extends BaseRequest {
682
853
  // msnodesqlv8 2.0 ends a statement's error with more=true and this message, and then
683
854
  // nothing follows: the error just collected is the one that terminated the statement.
684
855
  // A stream has already emitted it
856
+ detach()
685
857
  if (!this.stream) {
686
858
  const error = errors.pop()
687
859
  handleError(req, connection, error.originalError || error, false)
@@ -701,13 +873,48 @@ class Request extends BaseRequest {
701
873
  connection.hasError = true
702
874
  }
703
875
 
876
+ if (!moreErrors) {
877
+ // an error with nothing more to follow ends the batch
878
+ detach()
879
+ } else if (!detached && !settingAside) {
880
+ // the errors that end a batch are emitted together, all but the last with more=true,
881
+ // and msnodesqlv8 >= 5.1 releases the statement as soon as they are, so the query is
882
+ // set aside while they are handled, and a cancel made meanwhile waits until they have
883
+ // been. If none of them ended the batch it is taken up again and, if the stream was
884
+ // paused meanwhile before its first result set, paused, unless it is being cancelled.
885
+ // A stream that is paused holds its events back, so none is resumed meanwhile
886
+ settingAside = true
887
+ queueMicrotask(() => {
888
+ settingAside = false
889
+ if (cancelHeld) {
890
+ cancelHeld = false
891
+ cancel()
892
+ }
893
+ if (!detached && !cancelRequested && this.paused && this._pauseStream) this._pauseStream()
894
+ })
895
+ }
896
+
704
897
  if (finished) return
705
898
 
706
899
  handleError(req, connection, info, moreErrors)
707
900
  })
708
901
 
709
- req.once('done', () => {
902
+ req.on('free', () => {
903
+ freed = true
904
+ if (cancelDeferred && !finished) {
905
+ cancelDeferred = false
906
+ req.emit('done')
907
+ }
908
+ })
909
+
910
+ // msnodesqlv8 emits 'done' when a batch completes, but not when an error ends it; either
911
+ // way it emits 'free' once it has released the statement. A stream reports its errors as
912
+ // they arrive rather than settling on them, so without 'free' it would never complete
913
+ // after such an error, nor give its connection back
914
+ const complete = () => {
710
915
  finished = true
916
+ detach()
917
+ if (cancelHeld && this.stream && this.paused) cancelRequested = true
711
918
 
712
919
  if (hasReturned) {
713
920
  return
@@ -729,6 +936,9 @@ class Request extends BaseRequest {
729
936
 
730
937
  const err = new RequestError('Canceled.', 'ECANCEL')
731
938
  if (this.stream) {
939
+ delete this._resumeStream
940
+ delete this._pauseStream
941
+ emitPendingOnCancel()
732
942
  // as with tedious, a cancelled stream reports the cancel as an error and still
733
943
  // completes with what was read up to it
734
944
  this.emit('error', err)
@@ -770,7 +980,7 @@ class Request extends BaseRequest {
770
980
  const ex2 = new RequestError(`Failed to parse incoming JSON. ${ex.message}`, 'EJSON')
771
981
 
772
982
  if (this.stream) {
773
- this.emit('error', ex2)
983
+ emitInOrder(false, () => this.emit('error', ex2))
774
984
  } else {
775
985
  console.error(ex2)
776
986
  }
@@ -782,7 +992,8 @@ class Request extends BaseRequest {
782
992
  chunksBuffer = null
783
993
  if (row && row.___return___ == null) {
784
994
  // row with ___return___ col is the last row
785
- if (this.stream && !this.paused) { this.emit('row', row) }
995
+ const last = row
996
+ if (this.stream) emitInOrder(true, () => this.emit('row', last))
786
997
  }
787
998
  }
788
999
  }
@@ -803,17 +1014,38 @@ class Request extends BaseRequest {
803
1014
  }
804
1015
  }
805
1016
 
806
- delete this._cancel
807
1017
  this.parent.release(connection)
808
1018
 
809
1019
  debug('request(%d): completed', IDS.get(this))
810
1020
 
811
1021
  if (this.stream) {
812
- callback(null, this._nested ? row : null, output, rowsAffected, recordsetcolumns)
1022
+ // the driver is through, but a paused stream completes once it has emitted what is
1023
+ // still waiting. Cancelled before then, it completes as cancelled: at once if it is
1024
+ // paused, dropping its rows, or else once it has emitted the rest
1025
+ this._cancel = () => {
1026
+ cancelRequested = true
1027
+ if (this.paused) emitPendingOnCancel()
1028
+ }
1029
+ const last = this._nested ? row : null
1030
+ emitInOrder(false, () => {
1031
+ delete this._cancel
1032
+ delete this._resumeStream
1033
+ delete this._pauseStream
1034
+ if (cancelRequested) {
1035
+ debug('request(%d): canceled', IDS.get(this))
1036
+ this.emit('error', new RequestError('Canceled.', 'ECANCEL'))
1037
+ return callback(null, null, output, rowsAffected, recordsetcolumns)
1038
+ }
1039
+ callback(null, last, output, rowsAffected, recordsetcolumns)
1040
+ })
813
1041
  } else {
1042
+ delete this._cancel
814
1043
  callback(null, recordsets, output, rowsAffected, recordsetcolumns)
815
1044
  }
816
- })
1045
+ }
1046
+
1047
+ req.once('done', complete)
1048
+ req.once('free', complete)
817
1049
  })
818
1050
  })
819
1051
  }
@@ -964,17 +1196,18 @@ class Request extends BaseRequest {
964
1196
  })
965
1197
  }
966
1198
 
1199
+ // the query pauses the driver itself, mostly at the end of a batch (see DRIVER_ROW_BATCH_SIZE)
967
1200
  _pause () {
968
1201
  super._pause()
969
- if (this._currentRequest) {
970
- this._currentRequest.pauseQuery()
1202
+ if (this._pauseStream) {
1203
+ this._pauseStream()
971
1204
  }
972
1205
  }
973
1206
 
974
1207
  _resume () {
975
1208
  super._resume()
976
- if (this._currentRequest) {
977
- this._currentRequest.resumeQuery()
1209
+ if (this._resumeStream) {
1210
+ this._resumeStream()
978
1211
  }
979
1212
  }
980
1213
  }
@@ -23,6 +23,15 @@ class Transaction extends BaseTransaction {
23
23
  super._begin(isolationLevel, err => {
24
24
  if (err) return callback(err)
25
25
 
26
+ // built before a connection is borrowed, so that an invalid isolation level is reported
27
+ // with nothing to give back
28
+ let statement
29
+ try {
30
+ statement = `set transaction isolation level ${isolationLevelDeclaration(this.isolationLevel)};begin tran;`
31
+ } catch (err) {
32
+ return callback(err)
33
+ }
34
+
26
35
  debug('transaction(%d): begin', IDS.get(this))
27
36
 
28
37
  this.parent.acquire(this, (err, connection, config) => {
@@ -33,13 +42,22 @@ class Transaction extends BaseTransaction {
33
42
 
34
43
  const req = new Request(this)
35
44
  req.stream = false
36
- req.query(`set transaction isolation level ${isolationLevelDeclaration(this.isolationLevel)};begin tran;`, err => {
45
+ req.query(statement, err => {
37
46
  if (err) {
38
- this.parent.release(this._acquiredConnection)
47
+ debug('transaction(%d): failed to begin', IDS.get(this), err)
48
+
39
49
  this._acquiredConnection = null
40
50
  this._acquiredConfig = null
41
51
 
42
- return callback(err)
52
+ // the server may have begun the transaction even though the query failed (it can
53
+ // time out after the server has run it), so the pool is left to close the connection
54
+ // rather than lend it out again with a transaction nobody will end. It is not closed
55
+ // here, as msnodesqlv8 never calls back from closing a connection that is already
56
+ // closed, and the pool would wait for it to
57
+ connection.hasError = true
58
+ this.parent.release(connection)
59
+
60
+ return callback(new TransactionError(err))
43
61
  }
44
62
 
45
63
  debug('transaction(%d): begun', IDS.get(this))
@@ -167,7 +167,13 @@ class ConnectionPool extends BaseConnectionPool {
167
167
  const poolId = IDS.get(this)
168
168
 
169
169
  if (tedious.closed) {
170
+ // already closed (tedious closes a connection whose socket ends, for one), but it is
171
+ // leaving the pool all the same
170
172
  debug('connection(%d): already closed', IDS.get(tedious))
173
+ publish(CHANNELS.CONNECTION_DESTROY, () => ({
174
+ connectionId,
175
+ poolId
176
+ }))
171
177
  resolve()
172
178
  } else {
173
179
  tedious.once('end', () => {
@@ -47,13 +47,42 @@ class Transaction extends BaseTransaction {
47
47
  this._acquiredConnection.on('rollbackTransaction', this._abort)
48
48
  this._acquiredConfig = config
49
49
 
50
- connection.beginTransaction(err => {
51
- if (err) err = new TransactionError(err)
52
-
53
- debug('transaction(%d): begun', IDS.get(this))
54
-
55
- callback(err)
56
- }, this.name, this.isolationLevel)
50
+ // the caller hears of one outcome, whatever tedious does
51
+ let calledBack = false
52
+ const done = (err, discard) => {
53
+ if (calledBack) return
54
+ calledBack = true
55
+
56
+ if (!err) {
57
+ debug('transaction(%d): begun', IDS.get(this))
58
+ return callback(null)
59
+ }
60
+
61
+ debug('transaction(%d): failed to begin', IDS.get(this), err)
62
+
63
+ connection.removeListener('rollbackTransaction', this._abort)
64
+ this._acquiredConnection = null
65
+ this._acquiredConfig = null
66
+
67
+ // deferred so that tedious is done with the connection before it is closed, and so
68
+ // that the callback runs outside the pool's promise chain, which would pass anything
69
+ // the callback throws back to the acquire callback above as a second error
70
+ setImmediate(() => {
71
+ if (discard) connection.close()
72
+ this.parent.release(connection)
73
+ callback(new TransactionError(err))
74
+ })
75
+ }
76
+
77
+ try {
78
+ // the server may have begun the transaction even though the request failed (it can
79
+ // time out after the server has run it), so the connection is closed rather than
80
+ // lent out again with a transaction nobody will end
81
+ connection.beginTransaction(err => done(err, true), this.name, this.isolationLevel)
82
+ } catch (err) {
83
+ // tedious validates the arguments before it sends anything
84
+ done(err, false)
85
+ }
57
86
  })
58
87
  })
59
88
  }
package/package.json CHANGED
@@ -21,7 +21,7 @@
21
21
  "azure",
22
22
  "node-mssql"
23
23
  ],
24
- "version": "12.7.6",
24
+ "version": "12.7.7",
25
25
  "main": "index.js",
26
26
  "type": "commonjs",
27
27
  "repository": {