Details
-
Bug
-
Status: Closed
-
Major
-
Resolution: Fixed
-
Nightly Builds
-
None
Description
There does not appear to be sufficient sync protection for the destroyCount and createCount counters when destroy is activated by invalidateObject in GOP, GKOP, v2 trunk. The test below fails when added to GOP tests. The destroyCount is over-incremented due (I think) to the fact that multiple threads can enter destroy before the object being invalidated is removed from allObjects. This problem was originally reported by Thomas Neidhart in a comment on POOL-213.
@Test public void testConcurrentInvalidate() throws Exception { // Get allObjects and idleObjects loaded with some instances final int nObjects = 1000; pool.setMaxTotal(nObjects); pool.setMaxIdle(nObjects); final Object[] obj = new Object[nObjects]; for (int i = 0; i < nObjects; i++) { obj[i] = pool.borrowObject(); } for (int i = 0; i < nObjects; i++) { if (i % 2 == 0) { pool.returnObject(obj[i]); } } final int nThreads = 100; final int nIterations = 100; final InvalidateThread[] threads = new InvalidateThread[nThreads]; // Randomly generated list of distinct invalidation targets final ArrayList<Integer> targets = new ArrayList<Integer>(); final Random random = new Random(); for (int j = 0; j < nIterations; j++) { // Get a new invalidation target Integer targ = new Integer(random.nextInt(nObjects)); while (targets.contains(targ)) { targ = new Integer(random.nextInt(nObjects)); } targets.add(targ); // Launch nThreads threads all trying to invalidate the target for (int i = 0; i < nThreads; i++) { threads[i] = new InvalidateThread(pool, obj[targ]); } for (int i = 0; i < nThreads; i++) { new Thread(threads[i]).start(); } boolean done = false; while (!done) { done = true; for (int i = 0; i < nThreads; i++) { done = done && threads[i].complete(); } Thread.sleep(100); } } Assert.assertEquals(nIterations, pool.getDestroyedCount()); } static class InvalidateThread implements Runnable { final private Object obj; final private ObjectPool<Object> pool; private boolean done = false; public InvalidateThread(ObjectPool<Object> pool, Object obj) { this.obj = obj; this.pool = pool; } public void run() { try { pool.invalidateObject(obj); } catch (IllegalStateException ex) { // Ignore } catch (Exception ex) { Assert.fail("Unexpected exception " + ex.toString()); } finally { done = true; } } public boolean complete() { return done; } }