This repository was archived by the owner on Jul 19, 2024. It is now read-only.
Commit 15b176d
committed
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 82e86f4 commit 15b176d
File tree
2 files changed
+50
-2
lines changed- microsoft-azure-storage-test/src/com/microsoft/azure/storage/blob
- microsoft-azure-storage/src/com/microsoft/azure/storage/core
2 files changed
+50
-2
lines changedLines changed: 49 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
22 | 22 | | |
23 | 23 | | |
24 | 24 | | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
25 | 30 | | |
26 | 31 | | |
| 32 | + | |
27 | 33 | | |
28 | 34 | | |
29 | 35 | | |
| |||
541 | 547 | | |
542 | 548 | | |
543 | 549 | | |
| 550 | + | |
| 551 | + | |
| 552 | + | |
| 553 | + | |
| 554 | + | |
| 555 | + | |
| 556 | + | |
| 557 | + | |
| 558 | + | |
| 559 | + | |
| 560 | + | |
| 561 | + | |
| 562 | + | |
| 563 | + | |
| 564 | + | |
| 565 | + | |
| 566 | + | |
| 567 | + | |
| 568 | + | |
| 569 | + | |
| 570 | + | |
| 571 | + | |
| 572 | + | |
| 573 | + | |
| 574 | + | |
| 575 | + | |
| 576 | + | |
| 577 | + | |
| 578 | + | |
| 579 | + | |
| 580 | + | |
| 581 | + | |
| 582 | + | |
| 583 | + | |
| 584 | + | |
| 585 | + | |
| 586 | + | |
| 587 | + | |
| 588 | + | |
| 589 | + | |
| 590 | + | |
| 591 | + | |
| 592 | + | |
544 | 593 | | |
545 | 594 | | |
546 | 595 | | |
| |||
Lines changed: 1 addition & 2 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
129 | 129 | | |
130 | 130 | | |
131 | 131 | | |
132 | | - | |
133 | 132 | | |
134 | | - | |
| 133 | + | |
135 | 134 | | |
136 | 135 | | |
137 | 136 | | |
| |||
0 commit comments