Skip to content

Commit 0b8f8b5

Browse files
authored
Merge pull request #504 from zhenlineo/4.0-connection-leak
4.0 connection leak
2 parents dfc4b83 + 732d86f commit 0b8f8b5

File tree

2 files changed

+52
-8
lines changed

2 files changed

+52
-8
lines changed

src/internal/pool.js

+18-8
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,18 @@ class Pool {
7373
const key = address.asKey()
7474

7575
if (resource) {
76-
resourceAcquired(key, this._activeResourceCounts)
77-
if (this._log.isDebugEnabled()) {
78-
this._log.debug(`${resource} acquired from the pool ${key}`)
76+
if (
77+
this._maxSize &&
78+
this.activeResourceCount(address) >= this._maxSize
79+
) {
80+
this._destroy(resource)
81+
} else {
82+
resourceAcquired(key, this._activeResourceCounts)
83+
if (this._log.isDebugEnabled()) {
84+
this._log.debug(`${resource} acquired from the pool ${key}`)
85+
}
86+
return resource
7987
}
80-
return resource
8188
}
8289

8390
// We're out of resources and will try to acquire later on when an existing resource is released.
@@ -104,11 +111,11 @@ class Pool {
104111
// request already resolved/rejected by the release operation; nothing to do
105112
} else {
106113
// request is still pending and needs to be failed
114+
const activeCount = this.activeResourceCount(address)
115+
const idleCount = this.has(address) ? this._pools[key].length : 0
107116
request.reject(
108117
newError(
109-
`Connection acquisition timed out in ${
110-
this._acquisitionTimeout
111-
} ms.`
118+
`Connection acquisition timed out in ${this._acquisitionTimeout} ms. Poos status: Active conn count = ${activeCount}, Idle conn count = ${idleCount}.`
112119
)
113120
)
114121
}
@@ -218,7 +225,10 @@ class Pool {
218225
} else {
219226
if (this._installIdleObserver) {
220227
this._installIdleObserver(resource, {
221-
onError: () => {
228+
onError: error => {
229+
this._log.debug(
230+
`Idle connection ${resource} destroyed because of error: ${error}`
231+
)
222232
const pool = this._pools[key]
223233
if (pool) {
224234
this._pools[key] = pool.filter(r => r !== resource)

test/driver.test.js

+34
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,40 @@ describe('#integration driver', () => {
4444
}
4545
})
4646

47+
it('should not decrease active connection count after driver close', done => {
48+
// Given
49+
const config = {
50+
maxConnectionPoolSize: 2,
51+
connectionAcquisitionTimeout: 0,
52+
encrypted: false
53+
}
54+
driver = neo4j.driver('bolt://localhost', sharedNeo4j.authToken, config)
55+
56+
function beginTxWithoutCommit (driver) {
57+
const session = driver.session()
58+
const tx = session.beginTransaction()
59+
return tx.run('RETURN 1')
60+
}
61+
// When
62+
63+
const result1 = beginTxWithoutCommit(driver)
64+
const result2 = beginTxWithoutCommit(driver)
65+
66+
Promise.all([result1, result2]).then(results => {
67+
driver.close()
68+
beginTxWithoutCommit(driver).catch(() => {
69+
var pool = driver._connectionProvider._connectionPool
70+
var serverKey = Object.keys(pool._activeResourceCounts)[0]
71+
expect(pool._activeResourceCounts[serverKey]).toEqual(2)
72+
expect(serverKey in pool._pools).toBeFalsy()
73+
expect(
74+
Object.keys(driver._connectionProvider._openConnections).length
75+
).toEqual(2)
76+
done()
77+
})
78+
})
79+
}, 10000)
80+
4781
it('should expose sessions', () => {
4882
// Given
4983
driver = neo4j.driver('bolt://localhost', sharedNeo4j.authToken)

0 commit comments

Comments
 (0)