Author: tn
Date: Fri Jan 18 10:51:35 2013
New Revision: 1435077
URL: http://svn.apache.org/viewvc?rev=1435077&view=rev
Log:
[LOGGING-119] Prevent potential deadlock in WeakHashtable.
Added:
commons/proper/logging/trunk/src/test/java/org/apache/commons/logging/impl/WeakHashtableTestCase.java
- copied, changed from r1435057,
commons/proper/logging/trunk/src/test/java/org/apache/commons/logging/impl/WeakHashtableTest.java
Removed:
commons/proper/logging/trunk/src/test/java/org/apache/commons/logging/impl/WeakHashtableTest.java
Modified:
commons/proper/logging/trunk/src/changes/changes.xml
commons/proper/logging/trunk/src/main/java/org/apache/commons/logging/impl/WeakHashtable.java
Modified: commons/proper/logging/trunk/src/changes/changes.xml
URL:
http://svn.apache.org/viewvc/commons/proper/logging/trunk/src/changes/changes.xml?rev=1435077&r1=1435076&r2=1435077&view=diff
==============================================================================
--- commons/proper/logging/trunk/src/changes/changes.xml (original)
+++ commons/proper/logging/trunk/src/changes/changes.xml Fri Jan 18 10:51:35
2013
@@ -44,6 +44,9 @@ The <action> type attribute can be add,u
</properties>
<body>
<release version="1.1.2" date="In SVN" description="Bug fixes.">
+ <action type="fix" issue="LOGGING-119">
+ Prevent potential deadlock scenario in WeakHashtable.
+ </action>
<action type="fix" issue="LOGGING-130">
Potential missing privileged block for class loader.
</action>
Modified:
commons/proper/logging/trunk/src/main/java/org/apache/commons/logging/impl/WeakHashtable.java
URL:
http://svn.apache.org/viewvc/commons/proper/logging/trunk/src/main/java/org/apache/commons/logging/impl/WeakHashtable.java?rev=1435077&r1=1435076&r2=1435077&view=diff
==============================================================================
---
commons/proper/logging/trunk/src/main/java/org/apache/commons/logging/impl/WeakHashtable.java
(original)
+++
commons/proper/logging/trunk/src/main/java/org/apache/commons/logging/impl/WeakHashtable.java
Fri Jan 18 10:51:35 2013
@@ -19,11 +19,13 @@ package org.apache.commons.logging.impl;
import java.lang.ref.ReferenceQueue;
import java.lang.ref.WeakReference;
+import java.util.ArrayList;
import java.util.Collection;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.Hashtable;
import java.util.Iterator;
+import java.util.List;
import java.util.Map;
import java.util.Set;
@@ -217,7 +219,7 @@ public final class WeakHashtable extends
/**
*@see Hashtable
*/
- public Object put(Object key, Object value) {
+ public synchronized Object put(Object key, Object value) {
// check for nulls, ensuring semantics match superclass
if (key == null) {
throw new NullPointerException("Null keys are not allowed");
@@ -265,7 +267,7 @@ public final class WeakHashtable extends
/**
*@see Hashtable
*/
- public Object remove(Object key) {
+ public synchronized Object remove(Object key) {
// for performance reasons, only purge every
// MAX_CHANGES_BEFORE_PURGE times
if (changeCount++ > MAX_CHANGES_BEFORE_PURGE) {
@@ -317,12 +319,21 @@ public final class WeakHashtable extends
* have been garbage collected.
*/
private void purge() {
+ final List toRemove = new ArrayList();
synchronized (queue) {
WeakKey key;
while ((key = (WeakKey) queue.poll()) != null) {
- super.remove(key.getReferenced());
+ toRemove.add(key.getReferenced());
}
}
+
+ // LOGGING-119: do the actual removal of the keys outside the sync
block
+ // to prevent deadlock scenarios as purge() may be called from
+ // non-synchronized methods too
+ final int size = toRemove.size();
+ for (int i = 0; i < size; i++) {
+ super.remove(toRemove.get(i));
+ }
}
/**
Copied:
commons/proper/logging/trunk/src/test/java/org/apache/commons/logging/impl/WeakHashtableTestCase.java
(from r1435057,
commons/proper/logging/trunk/src/test/java/org/apache/commons/logging/impl/WeakHashtableTest.java)
URL:
http://svn.apache.org/viewvc/commons/proper/logging/trunk/src/test/java/org/apache/commons/logging/impl/WeakHashtableTestCase.java?p2=commons/proper/logging/trunk/src/test/java/org/apache/commons/logging/impl/WeakHashtableTestCase.java&p1=commons/proper/logging/trunk/src/test/java/org/apache/commons/logging/impl/WeakHashtableTest.java&r1=1435057&r2=1435077&rev=1435077&view=diff
==============================================================================
---
commons/proper/logging/trunk/src/test/java/org/apache/commons/logging/impl/WeakHashtableTest.java
(original)
+++
commons/proper/logging/trunk/src/test/java/org/apache/commons/logging/impl/WeakHashtableTestCase.java
Fri Jan 18 10:51:35 2013
@@ -30,7 +30,7 @@ import java.util.Set;
import junit.framework.TestCase;
-public class WeakHashtableTest extends TestCase {
+public class WeakHashtableTestCase extends TestCase {
private static final int WAIT_FOR_THREAD_COMPLETION = 5000; // 5 seconds
private static final int RUN_LOOPS = 3000;
@@ -50,7 +50,7 @@ public class WeakHashtableTest extends T
private Long valueTwo;
private Long valueThree;
- public WeakHashtableTest(String testName) {
+ public WeakHashtableTestCase(String testName) {
super(testName);
}