Skip to content

Commit 2a4ff54

Browse files
committed
Address errors thrown while waiting for writes to drain onto the network
1 parent 05c11ce commit 2a4ff54

File tree

1 file changed

+31
-18
lines changed

1 file changed

+31
-18
lines changed

lib/thin/sqlnet/ntTcp.js

Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,8 @@ class NTTCP {
240240
}
241241
this.stream = null;
242242
this.connected = false;
243-
this.waiter = null;
243+
this.drainWaiter = null;
244+
this.readWaiter = null;
244245
}
245246

246247
/**
@@ -323,10 +324,7 @@ class NTTCP {
323324
if (err) {
324325
this.savedErr = err;
325326
this.err = true;
326-
if (this.waiter) {
327-
this.waiter();
328-
this.waiter = null;
329-
}
327+
this._notifyWaiters();
330328
}
331329
});
332330
if (!result) {
@@ -352,7 +350,9 @@ class NTTCP {
352350
async pauseWrite() {
353351
this.checkErr();
354352
if (this.needsDrain) {
355-
await new Promise((resolve) => this.stream.once('drain', resolve));
353+
await new Promise((resolve) => {
354+
this.drainWaiter = resolve;
355+
});
356356
this.checkErr();
357357
} else {
358358
await new Promise((resolve) => Timers.setImmediate(resolve));
@@ -397,9 +397,9 @@ class NTTCP {
397397
flags: tempBuf[5]
398398
};
399399
this.packets.push(packet);
400-
if (this.waiter) {
401-
this.waiter();
402-
this.waiter = null;
400+
if (this.readWaiter) {
401+
this.readWaiter();
402+
this.readWaiter = null;
403403
}
404404
if (process.env.NODE_ORACLEDB_DEBUG_PACKETS)
405405
this.printPacket("Receiving packet", packet.buf);
@@ -436,7 +436,7 @@ class NTTCP {
436436
if (this.packets.length === 0) {
437437
this.checkErr();
438438
await new Promise((resolve) => {
439-
this.waiter = resolve;
439+
this.readWaiter = resolve;
440440
this.numPacketsSinceLastWait = 0;
441441
});
442442
this.checkErr();
@@ -472,26 +472,25 @@ class NTTCP {
472472
this.stream.on('error', (err) => {
473473
this.savedErr = err;
474474
this.err = true;
475-
if (this.waiter) {
476-
this.waiter();
477-
this.waiter = null;
478-
}
475+
this._notifyWaiters();
479476
});
480477

481478
this.stream.on('end', () => {
482479
this.err = true;
483-
if (this.waiter) {
484-
this.waiter();
485-
this.waiter = null;
486-
}
480+
this._notifyWaiters();
487481
});
488482

489483
this.stream.on('close', () => {
490484
this.connected = false;
485+
this._notifyWaiters();
491486
});
492487

493488
this.stream.on('drain', () => {
494489
this.needsDrain = false;
490+
if (this.drainWaiter) {
491+
this.drainWaiter();
492+
this.drainWaiter = null;
493+
}
495494
});
496495

497496
}
@@ -516,6 +515,20 @@ class NTTCP {
516515
}
517516
}
518517

518+
/**
519+
* Notify the waiters (drain and read) and reset them, if applicable.
520+
*/
521+
_notifyWaiters() {
522+
if (this.drainWaiter) {
523+
this.drainWaiter();
524+
this.drainWaiter = null;
525+
}
526+
if (this.readWaiter) {
527+
this.readWaiter();
528+
this.readWaiter = null;
529+
}
530+
}
531+
519532
}
520533

521534
module.exports = NTTCP;

0 commit comments

Comments
 (0)