Skip to content

Commit 78f65ae

Browse files
committed
Remove terminated connection queues from the pool
It was previously possible for empty/terminated connection queues to stay in the `SocketConnectionPool` when is was closed concurrently with sessions being acquired. This was not critical because `SocketConnectionPool` is only closed when the whole driver is closed but resulted in a flaky test. This commit adds removal of terminated connection queues.
1 parent ea552d3 commit 78f65ae

File tree

2 files changed

+12
-10
lines changed

2 files changed

+12
-10
lines changed

driver/src/main/java/org/neo4j/driver/internal/net/pooling/SocketConnectionPool.java

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -84,11 +84,7 @@ public PooledConnection get()
8484
};
8585
PooledConnection conn = connections.acquire( supplier );
8686

87-
if ( closed.get() )
88-
{
89-
connections.terminate();
90-
throw poolClosedException();
91-
}
87+
assertNotClosed( address, connections );
9288

9389
conn.updateTimestamp();
9490
return conn;
@@ -140,16 +136,21 @@ public void close()
140136
}
141137
}
142138

143-
private void assertNotClosed()
139+
private void assertNotClosed( BoltServerAddress address, BlockingPooledConnectionQueue connections )
144140
{
145141
if ( closed.get() )
146142
{
147-
throw poolClosedException();
143+
connections.terminate();
144+
pools.remove( address );
145+
assertNotClosed();
148146
}
149147
}
150148

151-
private static RuntimeException poolClosedException()
149+
private void assertNotClosed()
152150
{
153-
return new IllegalStateException( "Pool closed" );
151+
if ( closed.get() )
152+
{
153+
throw new IllegalStateException( "Pool closed" );
154+
}
154155
}
155156
}

driver/src/test/java/org/neo4j/driver/internal/net/pooling/SocketConnectionPoolTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,8 @@ public void closeWithConcurrentAcquisitionsEmptiesThePool() throws InterruptedEx
265265

266266
for ( int i = 0; i < port.intValue(); i++ )
267267
{
268-
assertFalse( pool.hasAddress( new BoltServerAddress( "localhost", i ) ) );
268+
boolean hasAddress = pool.hasAddress( new BoltServerAddress( "localhost", i ) );
269+
assertFalse( "Pool still has connection queues" + pool, hasAddress );
269270
}
270271
for ( Connection connection : createdConnections )
271272
{

0 commit comments

Comments
 (0)