Skip to content

Commit

Permalink
Work around readyState being initially "open" (nodejs/node#38247)
Browse files Browse the repository at this point in the history
  • Loading branch information
brianreavis committed Mar 6, 2022
1 parent 01785e6 commit c8a8054
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -268,8 +268,11 @@ Manager.prototype.allocate = function allocate(fn) {
* @api private
*/
Manager.prototype.isAvailable = function isAvailable(net, ignore) {
// readyState is not reliable (https://github.com/nodejs/node/issues/38247), which is
// why `net.pending !== true` is critical. This fix is inspired by:
// https://github.com/Ayase-252/node/commit/89bcaadff7a2486e33ebb6d0412fa059e8e4bcdc
var readyState = net.readyState
, writable = readyState === 'open' || readyState === 'writeOnly'
, writable = net.pending !== true && (readyState === 'open' || readyState === 'writeOnly')
, writePending = net._pendingWriteReqs || 0
, writeQueue = net._writeQueue || []
, writes = writeQueue.length || writePending;
Expand All @@ -280,7 +283,7 @@ Manager.prototype.isAvailable = function isAvailable(net, ignore) {

// The connection is already closed or has been destroyed, why on earth are we
// getting it then, remove it from the pool and return 0.
if (readyState === 'closed' || net.destroyed) {
if ((net.pending !== true && readyState === 'closed') || net.destroyed) {
this.remove(net);
return 0;
}
Expand Down

0 comments on commit c8a8054

Please sign in to comment.