Acquire read lock before entering try block in XMLReaderUtils
Otherwise if lock operation fails for whatever reason
it will be released before lock acquired.
@@ -509,8 +509,8 @@ public class XMLReaderUtils implements Serializable {
int waiting = 0;
while (true) {
PoolDOMBuilder builder = null;
- try {
DOM_READ_WRITE_LOCK.readLock().lock();
+ try {
builder = DOM_BUILDERS.poll(100, TimeUnit.MILLISECONDS);
} catch (InterruptedException e) {
throw new TikaException("interrupted while waiting for DOMBuilder", e);
@@ -547,8 +547,8 @@ public class XMLReaderUtils implements Serializable {
} catch (UnsupportedOperationException e) {
//ignore
}
- try {
DOM_READ_WRITE_LOCK.readLock().lock();
+ try {
//if there are extra parsers (e.g. after a reset of the pool to a smaller size),
// this parser will not be added and will then be gc'd
boolean success = DOM_BUILDERS.offer(builder);
@@ -575,8 +575,8 @@ public class XMLReaderUtils implements Serializable {
int waiting = 0;
while (true) {
PoolSAXParser parser = null;
- try {
SAX_READ_WRITE_LOCK.readLock().lock();
+ try {
parser = SAX_PARSERS.poll(100, TimeUnit.MILLISECONDS);
} catch (InterruptedException e) {
throw new TikaException("interrupted while waiting for SAXParser", e);
@@ -611,8 +611,8 @@ public class XMLReaderUtils implements Serializable {
if (parser.getGeneration() != POOL_GENERATION.get()) {
return;
}
- try {
SAX_READ_WRITE_LOCK.readLock().lock();
+ try {
//if there are extra parsers (e.g. after a reset of the pool to a smaller size),
// this parser will not be added and will then be gc'd
boolean success = SAX_PARSERS.offer(parser);
@@ -634,7 +634,6 @@ public class XMLReaderUtils implements Serializable {
* @param poolSize
*/
public static void setPoolSize(int poolSize) throws TikaException {
- try {
//stop the world with a write lock.
//parsers that are currently in use will be offered later (once the lock is released),
//but not accepted and will be gc'd. We have to do this locking and
@@ -642,6 +641,7 @@ public class XMLReaderUtils implements Serializable {
//parsers have already started. We could have an NPE on SAX_PARSERS
//if we didn't lock.
SAX_READ_WRITE_LOCK.writeLock().lock();
+ try {
//free up any resources before emptying SAX_PARSERS
for (PoolSAXParser parser : SAX_PARSERS) {
parser.reset();
@@ -659,8 +659,9 @@ public class XMLReaderUtils implements Serializable {
} finally {
SAX_READ_WRITE_LOCK.writeLock().unlock();
}
- try {
+
DOM_READ_WRITE_LOCK.writeLock().lock();
+ try {
DOM_BUILDERS.clear();
DOM_BUILDERS = new ArrayBlockingQueue<>(poolSize);
for (int i = 0; i < poolSize; i++) {
