File tree

1 file changed

+29
-8
lines changed

1 file changed

+29
-8
lines changed
Original file line numberDiff line numberDiff line change
@@ -1595,6 +1595,7 @@ final class PoolMaintainer {
15951595
Instant lastResetTime = Instant.ofEpochMilli(0);
15961596
int numSessionsToClose = 0;
15971597
int sessionsToClosePerLoop = 0;
1598+
boolean closed = false;
15981599

15991600
@GuardedBy("lock")
16001601
ScheduledFuture<?> scheduledFuture;
@@ -1621,17 +1622,26 @@ public void run() {
16211622

16221623
void close() {
16231624
synchronized (lock) {
1624-
scheduledFuture.cancel(false);
1625-
if (!running) {
1626-
decrementPendingClosures(1);
1625+
if (!closed) {
1626+
closed = true;
1627+
scheduledFuture.cancel(false);
1628+
if (!running) {
1629+
decrementPendingClosures(1);
1630+
}
16271631
}
16281632
}
16291633
}
16301634

1635+
boolean isClosed() {
1636+
synchronized (lock) {
1637+
return closed;
1638+
}
1639+
}
1640+
16311641
// Does various pool maintenance activities.
16321642
void maintainPool() {
16331643
synchronized (lock) {
1634-
if (isClosed()) {
1644+
if (SessionPool.this.isClosed()) {
16351645
return;
16361646
}
16371647
running = true;
@@ -1643,7 +1653,7 @@ void maintainPool() {
16431653
replenishPool();
16441654
synchronized (lock) {
16451655
running = false;
1646-
if (isClosed()) {
1656+
if (SessionPool.this.isClosed()) {
16471657
decrementPendingClosures(1);
16481658
}
16491659
}
@@ -2126,6 +2136,7 @@ private void handleCreateSessionsFailure(SpannerException e, int count) {
21262136
}
21272137
if (isDatabaseOrInstanceNotFound(e)) {
21282138
setResourceNotFoundException((ResourceNotFoundException) e);
2139+
poolMaintainer.close();
21292140
}
21302141
}
21312142
}
@@ -2161,10 +2172,14 @@ ListenableFuture<Void> closeAsync(ClosedException closedException) {
21612172
}
21622173
closureFuture = SettableFuture.create();
21632174
retFuture = closureFuture;
2164-
pendingClosure =
2165-
totalSessions() + numSessionsBeingCreated + 1 /* For pool maintenance thread */;
21662175

2167-
poolMaintainer.close();
2176+
pendingClosure = totalSessions() + numSessionsBeingCreated;
2177+
2178+
if (!poolMaintainer.isClosed()) {
2179+
pendingClosure += 1; // For pool maintenance thread
2180+
poolMaintainer.close();
2181+
}
2182+
21682183
sessions.clear();
21692184
for (PooledSessionFuture session : checkedOutSessions) {
21702185
if (session.Exception != null) {
@@ -2180,7 +2195,13 @@ ListenableFuture<Void> closeAsync(ClosedException closedException) {
21802195
closeSessionAsync(session);
21812196
}
21822197
}
2198+
2199+
// Nothing to be closed, mark as complete
2200+
if (pendingClosure == 0) {
2201+
closureFuture.set(null);
2202+
}
21832203
}
2204+
21842205
retFuture.addListener(
21852206
new Runnable() {
21862207
@Override

0 commit comments

Comments
 (0)