Skip to content
This repository was archived by the owner on Jul 19, 2024. It is now read-only.

Commit 9fac38e

Browse files
ThomasMarquardtrickle-msft
authored andcommitted
SAXParser concurrency bug fix.
DETAILS: There is a concurrency bug in the XML parsing logic which can cause the results of a List Blobs operation to appear empty. Please refer to Utility.java#L131. Utility.saxParserThreadLocal is a ThreadLocal<SAXParser> with a SAXParserFactory member variable. The factory is initialized in ThreadLocal.initialValue which then continues to use the member variable reference. Because initialValue is called once for each thread, it is possible for Thread A to be after the call to "factory.setNamespaceAware(true)" but before the call to "return factory.newSAXParser()" when it yields to Thread B which calls "factory = SAXParserFactory.newInstance()" and then yields to Thread A which then calls "return factory.newSAXParser()". Since this is a reference to the factory member variable, this instance would therefore not be namespace aware since setNamespaceAware(true) has not been called. It therefore returns a SAXParser that is not namespace aware. A SAXParser that is not namespace aware will always return a list count of 0. Please refer to BlobListHandler.java#L84. BlobListHandler.startElement takes the localName parameter and compares it with "Blob" on line 84, to find each blob item in the list. When a SAXParser is not namespace aware, the localName parameter is always empty. Please refer to the documentation for DefaultHandler.startElement, which describes the localName parameter as follows, "The local name (without prefix), or the empty string if Namespace processing is not being performed." This was also confirmed by testing, so the documentation is correct. Thus when the SAXParser is not initialized correctly (is not namespace aware) the results of List Blobs operation will always appear to be empty. TESTS: The test testSAXParserConcurrency was added to validate that the SAXParser returned by Utility.getSAXParser is correctly configured, even when called under a highly concurrent load. This test will occassionaly fail without the fix, but the probability of faiulre is very low. To increease the likelihood of failure you can add calls to sleep before and after the call to "factory.setNamespaceAware(true)" in Utitlity.java as shown below: private static void sleep(long l) { try { Thread.sleep(l); } catch (InterruptedException e) { } } /** * Thread local for SAXParser. */ private static final ThreadLocal<SAXParser> saxParserThreadLocal = new ThreadLocal<SAXParser>() { SAXParserFactory factory; @OverRide public SAXParser initialValue() { factory = SAXParserFactory.newInstance(); sleep(100); factory.setNamespaceAware(true); sleep(10); try { return factory.newSAXParser(); } catch (SAXException e) { throw new RuntimeException("Unable to create SAXParser", e); } catch (ParserConfigurationException e) { throw new RuntimeException("Check parser configuration", e); } } };
1 parent b796935 commit 9fac38e

File tree

2 files changed

+52
-2
lines changed

2 files changed

+52
-2
lines changed

microsoft-azure-storage-test/src/com/microsoft/azure/storage/blob/CloudBlobContainerTests.java

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,14 @@
3232
import java.util.TimeZone;
3333
import java.util.UUID;
3434

35+
import java.util.concurrent.Executors;
36+
import java.util.concurrent.ThreadPoolExecutor;
37+
import java.util.concurrent.TimeUnit;
38+
import java.util.concurrent.atomic.AtomicInteger;
39+
import javax.xml.parsers.SAXParser;
40+
41+
import com.microsoft.azure.storage.core.Utility;
42+
3543
import org.junit.After;
3644
import org.junit.Before;
3745
import org.junit.Test;
@@ -557,6 +565,49 @@ public void testCloudBlobContainerListBlobs() throws StorageException, IOExcepti
557565
assertTrue(blobNames.size() == 0);
558566
}
559567

568+
@Test
569+
@Category({DevFabricTests.class, DevStoreTests.class})
570+
public void testSAXParserConcurrency() throws Exception {
571+
final int totalCount = 200000;
572+
final int numThreads = 200;
573+
final AtomicInteger currentCount = new AtomicInteger(0);
574+
final AtomicInteger pending = new AtomicInteger(0);
575+
final AtomicInteger failureCount = new AtomicInteger(0);
576+
ThreadPoolExecutor executor = (ThreadPoolExecutor) Executors.newFixedThreadPool(numThreads);
577+
578+
do {
579+
final int count = currentCount.incrementAndGet();
580+
pending.incrementAndGet();
581+
executor.execute(new Runnable() {
582+
@Override
583+
public void run() {
584+
pending.decrementAndGet();
585+
if (count > totalCount) {
586+
return;
587+
}
588+
try {
589+
SAXParser parser = Utility.getSAXParser();
590+
if (!parser.isNamespaceAware()) {
591+
failureCount.incrementAndGet();
592+
}
593+
assertEquals(true, parser.isNamespaceAware());
594+
} catch (Exception e) {
595+
fail(e.toString());
596+
}
597+
}
598+
});
599+
600+
assertEquals(0, failureCount.get());
601+
602+
while (pending.get() > numThreads * 2) {
603+
Thread.sleep(10);
604+
}
605+
} while (currentCount.get() < totalCount);
606+
executor.shutdown();
607+
executor.awaitTermination(1, TimeUnit.MINUTES);
608+
executor.shutdownNow();
609+
}
610+
560611
/**
561612
* List the blobs in a container with a prefix
562613
*

microsoft-azure-storage/src/com/microsoft/azure/storage/core/Utility.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,9 +135,8 @@ protected DateFormat initialValue() {
135135
* Thread local for SAXParser.
136136
*/
137137
private static final ThreadLocal<SAXParser> saxParserThreadLocal = new ThreadLocal<SAXParser>() {
138-
SAXParserFactory factory;
139138
@Override public SAXParser initialValue() {
140-
factory = SAXParserFactory.newInstance();
139+
SAXParserFactory factory = SAXParserFactory.newInstance();
141140
factory.setNamespaceAware(true);
142141
try {
143142
return factory.newSAXParser();

0 commit comments

Comments
 (0)