Skip to content

Commit 1995118

Browse files
voutilad2hdddg
authored andcommitted
Fix connection leak due to race condition in counting resources
Since resources were being acquired in one async context, but being counted in another, it was possible that under high load checks of the total number of connections could report zero when connections were actually created. This commit moves the call to resourceAcquired() to the same async context where it's being created initially or reacquired from the existing pool.
1 parent 938930f commit 1995118

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

src/internal/pool.js

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,7 @@ class Pool {
7474
const key = address.asKey()
7575

7676
if (resource) {
77-
resourceAcquired(key, this._activeResourceCounts)
78-
if (this._log.isDebugEnabled()) {
79-
this._log.debug(`${resource} acquired from the pool ${key}`)
80-
}
77+
// New or existing resource acquired
8178
return resource
8279
}
8380

@@ -190,7 +187,11 @@ class Pool {
190187
}
191188

192189
// idle resource is valid and can be acquired
193-
return Promise.resolve(resource)
190+
resourceAcquired(key, this._activeResourceCounts)
191+
if (this._log.isDebugEnabled()) {
192+
this._log.debug(`${resource} acquired from the pool ${key}`)
193+
}
194+
return resource
194195
} else {
195196
await this._destroy(resource)
196197
}
@@ -215,6 +216,11 @@ class Pool {
215216
try {
216217
// Invoke callback that creates actual connection
217218
resource = await this._create(address, this._release)
219+
220+
resourceAcquired(key, this._activeResourceCounts)
221+
if (this._log.isDebugEnabled()) {
222+
this._log.debug(`${resource} created for the pool ${key}`)
223+
}
218224
} finally {
219225
this._pendingCreates[key] = this._pendingCreates[key] - 1
220226
}
@@ -307,7 +313,6 @@ class Pool {
307313
this._release(address, resource)
308314
} else {
309315
// request is still pending and can be resolved with the newly acquired resource
310-
resourceAcquired(key, this._activeResourceCounts) // increment the active counter
311316
pendingRequest.resolve(resource) // resolve the pending request with the acquired resource
312317
}
313318
}

0 commit comments

Comments
 (0)