Skip to content

Commit 943a3cb

Browse files
committed
EvictionTimer now restores the current thread's interrupt flag when
catching InterruptedException
1 parent 338564c commit 943a3cb

8 files changed

Lines changed: 31 additions & 66 deletions

File tree

src/main/java/org/apache/commons/pool3/impl/EvictionTimer.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@ static synchronized void cancel(final BaseGenericObjectPool<?, ?>.Evictor evicto
158158
} catch (final InterruptedException e) {
159159
// Swallow
160160
// Significant API changes would be required to propagate this
161+
Thread.currentThread().interrupt();
161162
}
162163
executor.setCorePoolSize(0);
163164
executor = null;

src/main/java/org/apache/commons/pool3/impl/GenericKeyedObjectPool.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -454,6 +454,7 @@ public T borrowObject(final K key, final long borrowMaxWaitMillis) throws E {
454454
p = borrowMaxWaitMillis < 0 ? objectDeque.getIdleObjects().takeFirst() :
455455
objectDeque.getIdleObjects().pollFirst(borrowMaxWaitMillis, TimeUnit.MILLISECONDS);
456456
} catch (final InterruptedException e) {
457+
Thread.currentThread().interrupt();
457458
throw cast(e);
458459
}
459460
}
@@ -778,6 +779,7 @@ private PooledObject<T> create(final K key) throws E {
778779
try {
779780
objectDeque.makeObjectCountLock.wait();
780781
} catch (final InterruptedException e) {
782+
Thread.currentThread().interrupt();
781783
throw cast(e);
782784
}
783785
}

src/main/java/org/apache/commons/pool3/impl/GenericObjectPool.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,7 @@ public T borrowObject(final Duration maxWaitDuration) throws E {
314314
p = negativeDuration ? idleObjects.takeFirst() : idleObjects.pollFirst(maxWaitDuration);
315315
} catch (final InterruptedException e) {
316316
// Don't surface exception type of internal locking mechanism.
317+
Thread.currentThread().interrupt();
317318
throw cast(e);
318319
}
319320
}
@@ -543,6 +544,7 @@ private PooledObject<T> create(final Duration maxWaitDurationRequest) throws E {
543544
wait(makeObjectCountLock, remainingWaitDuration);
544545
} catch (final InterruptedException e) {
545546
// Don't surface exception type of internal locking mechanism.
547+
Thread.currentThread().interrupt();
546548
throw cast(e);
547549
}
548550
}

src/main/java/org/apache/commons/pool3/impl/ResilientPooledObjectFactory.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ public void run() {
8888
try {
8989
sleep(delay.toMillis());
9090
} catch (final InterruptedException e) {
91+
interrupt();
9192
kill();
9293
}
9394
}
@@ -178,6 +179,7 @@ public void run() {
178179
try {
179180
sleep(timeBetweenChecks.toMillis());
180181
} catch (final InterruptedException e) {
182+
interrupt();
181183
monitoring = false;
182184
} catch (final Throwable e) {
183185
monitoring = false;

src/test/java/org/apache/commons/pool3/PoolTest.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
import java.time.Duration;
2424

25+
import org.apache.commons.lang3.ThreadUtils;
2526
import org.apache.commons.pool3.impl.DefaultPooledObject;
2627
import org.apache.commons.pool3.impl.GenericObjectPool;
2728
import org.apache.commons.pool3.impl.GenericObjectPoolConfig;
@@ -59,7 +60,7 @@ public boolean validateObject(final PooledObject<Foo> pooledObject) {
5960
try {
6061
Thread.sleep(VALIDATION_WAIT_IN_MILLIS);
6162
} catch (final InterruptedException e) {
62-
Thread.interrupted();
63+
Thread.currentThread().interrupt();
6364
}
6465
return false;
6566
}
@@ -83,11 +84,7 @@ void testPool() {
8384
pool.setDurationBetweenEvictionRuns(Duration.ofMillis(EVICTION_PERIOD_IN_MILLIS));
8485
assertEquals(EVICTION_PERIOD_IN_MILLIS, pool.getDurationBetweenEvictionRuns().toMillis());
8586
pool.addObject();
86-
try {
87-
Thread.sleep(EVICTION_PERIOD_IN_MILLIS);
88-
} catch (final InterruptedException e) {
89-
Thread.interrupted();
90-
}
87+
ThreadUtils.sleepQuietly(Duration.ofMillis(EVICTION_PERIOD_IN_MILLIS));
9188
}
9289
final Thread[] threads = new Thread[Thread.activeCount()];
9390
Thread.enumerate(threads);

src/test/java/org/apache/commons/pool3/impl/DisconnectingWaiterFactory.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ private static void waitForConnection(final AtomicBoolean connected,
6969
try {
7070
Thread.sleep(timeBetweenConnectionChecks.toMillis());
7171
} catch (final InterruptedException e) {
72+
Thread.currentThread().interrupt();
7273
e.printStackTrace();
7374
}
7475
if (Duration.between(start, Instant.now()).compareTo(maxWait) > 0) {

src/test/java/org/apache/commons/pool3/impl/TestResilientPooledObjectFactory.java

Lines changed: 15 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import java.time.Duration;
2424
import java.util.UUID;
2525

26+
import org.apache.commons.lang3.ThreadUtils;
2627
import org.apache.commons.pool3.PooledObject;
2728
import org.apache.commons.pool3.PooledObjectFactory;
2829
import org.junit.jupiter.api.Test;
@@ -65,10 +66,7 @@ public PooledObject<String> makeObject() throws Exception {
6566
}
6667
if (hang) {
6768
while (!up) {
68-
try {
69-
Thread.sleep(1000);
70-
} catch (final InterruptedException e) {
71-
}
69+
ThreadUtils.sleepQuietly(Duration.ofSeconds(1));
7270
}
7371
}
7472
return null;
@@ -124,10 +122,7 @@ public void run() {
124122
}
125123
}.start();
126124
// Wait for the borrower to get in the queue
127-
try {
128-
Thread.sleep(50);
129-
} catch (final InterruptedException e) {
130-
}
125+
ThreadUtils.sleepQuietly(Duration.ofMillis(50));
131126
// Crash the base factory
132127
ff.crash();
133128
// Return object will create capacity in the pool
@@ -136,19 +131,13 @@ public void run() {
136131
} catch (final Exception e) {
137132
}
138133
// Wait for the adder to run
139-
try {
140-
Thread.sleep(100);
141-
} catch (final InterruptedException e) {
142-
}
134+
ThreadUtils.sleepQuietly(Duration.ofMillis(100));
143135
// Adder should be running
144136
assertTrue(rf.isAdderRunning());
145137
// Restart the factory
146138
ff.recover();
147139
// Wait for the adder to succeed
148-
try {
149-
Thread.sleep(200);
150-
} catch (final InterruptedException e) {
151-
}
140+
ThreadUtils.sleepQuietly(Duration.ofMillis(200));
152141
// Pool should have no waiters
153142
assertEquals(0, pool.getNumWaiters());
154143
// Adder should still be running because there is a failure in the log
@@ -160,10 +149,7 @@ public void run() {
160149
pool.addObject();
161150
}
162151
// Wait for the monitor to run
163-
try {
164-
Thread.sleep(200);
165-
} catch (final InterruptedException e) {
166-
}
152+
ThreadUtils.sleepQuietly(Duration.ofMillis(200));
167153
assertTrue(rf.isUp());
168154
// Adder should be stopped
169155
assertFalse(rf.isAdderRunning());
@@ -201,10 +187,7 @@ void testIsMonitorRunning() throws Exception {
201187
rf.startMonitor();
202188
pool.close();
203189
// Wait for monitor to run so it can kill itself
204-
try {
205-
Thread.sleep(200);
206-
} catch (final InterruptedException e) {
207-
}
190+
ThreadUtils.sleepQuietly(Duration.ofMillis(200));
208191
assertFalse(rf.isMonitorRunning());
209192
}
210193

@@ -251,36 +234,24 @@ public void run() {
251234
}.start();
252235
// Return borrowed objects - validation will fail
253236
// Wait for the borrowers to get in the queue
254-
try {
255-
Thread.sleep(50);
256-
} catch (final InterruptedException e) {
257-
}
237+
ThreadUtils.sleepQuietly(Duration.ofMillis(50));
258238
pool.returnObject(s1);
259239
pool.returnObject(s2);
260240
assertEquals(0, pool.getNumIdle());
261241
assertTrue(pool.getNumWaiters() > 0);
262242
// Wait for the monitor to pick up the failed create which should happen on
263243
// validation destroy
264-
try {
265-
Thread.sleep(200);
266-
} catch (final InterruptedException e) {
267-
}
244+
ThreadUtils.sleepQuietly(Duration.ofMillis(200));
268245
assertFalse(rf.isUp());
269246
// Restart the factory
270247
ff.recover();
271248
// Wait for the adder to succeed
272-
try {
273-
Thread.sleep(200);
274-
} catch (final InterruptedException e) {
275-
}
249+
ThreadUtils.sleepQuietly(Duration.ofMillis(200));
276250
// Pool should have no waiters
277251
assertEquals(0, pool.getNumWaiters());
278252
pool.close();
279253
// Wait for monitor to run
280-
try {
281-
Thread.sleep(200);
282-
} catch (final InterruptedException e) {
283-
}
254+
ThreadUtils.sleepQuietly(Duration.ofMillis(200));
284255
// Monitor and adder should be stopped by pool close
285256
assertFalse(rf.isAdderRunning());
286257
assertFalse(rf.isMonitorRunning());
@@ -318,11 +289,7 @@ public void run() {
318289
}
319290
}.start();
320291
// Wait for the borrower to join wait queue
321-
try {
322-
Thread.sleep(200);
323-
} catch (final InterruptedException e) {
324-
}
325-
292+
ThreadUtils.sleepQuietly(Duration.ofMillis(200));
326293
// Crash the base factory
327294
ff.crash();
328295
// Resilient factory does not know the base factory is down until a make is
@@ -335,24 +302,16 @@ public void run() {
335302
assertEquals(1, pool.getNumWaiters());
336303
// Wait for the monitor to pick up the failed create which should happen on
337304
// validation destroy
338-
try {
339-
Thread.sleep(100);
340-
} catch (final InterruptedException e) {
341-
}
305+
ThreadUtils.sleepQuietly(Duration.ofMillis(100));
342306
assertFalse(rf.isUp());
343307
// Adder should be running, but failing
344308
assertTrue(rf.isAdderRunning());
345-
346309
// Pool should have one take waiter
347310
assertEquals(1, pool.getNumWaiters());
348-
349311
// Restart the factory
350312
ff.recover();
351313
// Wait for the adder to succeed
352-
try {
353-
Thread.sleep(100);
354-
} catch (final InterruptedException e) {
355-
}
314+
ThreadUtils.sleepQuietly(Duration.ofMillis(100));
356315
// Pool should have no waiters
357316
assertTrue(pool.getNumWaiters() == 0);
358317
// Add 5 objects to clear the rf log
@@ -362,10 +321,7 @@ public void run() {
362321
pool.addObject();
363322
}
364323
// Wait for monitor to run
365-
try {
366-
Thread.sleep(200);
367-
} catch (final InterruptedException e) {
368-
}
324+
ThreadUtils.sleepQuietly(Duration.ofMillis(200));
369325
// rf should be up now
370326
assertTrue(rf.isUp());
371327

src/test/java/org/apache/commons/pool3/performance/PerformanceTest.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@ private void run(final int iterations, final int nrThreads, final int maxTotal,
158158
try {
159159
futures = threadPool.invokeAll(tasks);
160160
} catch (final InterruptedException e) {
161+
Thread.currentThread().interrupt();
161162
e.printStackTrace();
162163
}
163164

@@ -181,7 +182,10 @@ private void run(final int iterations, final int nrThreads, final int maxTotal,
181182
TaskStats taskStats = null;
182183
try {
183184
taskStats = future.get();
184-
} catch (final InterruptedException | ExecutionException e) {
185+
} catch (final InterruptedException e) {
186+
Thread.currentThread().interrupt();
187+
e.printStackTrace();
188+
} catch (final ExecutionException e) {
185189
e.printStackTrace();
186190
}
187191
if (taskStats != null) {

0 commit comments

Comments
 (0)