Concurrency problems with archive catalogs and XmlLoader #182
Loading…
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
I noticed that in the code to org.xmlresolver.loaders.XmlLoader.loadCatalog(URI, InputSource) that the code to update the org.xmlresolver.loaders.XmlLoader.catalogMap for 'zip' files is not synchronized around the catalogMap, and there is no check that a Thread that is waiting on the synchronized block does not do a second update on the catalog map.
This means that say, for a zip catalog if
Thread 1 (T1) and Thread 2 (T2) are updating the catalog at the same time with the same URI T1 succeeds in getting the lock. T2 waits. T1 fails to load the zip as a plain file initially but sets the org.xmlresolver.loaders.XmlLoader.loadCatalog(URI, SaxProducer).zipcatalog variable to the catalog location within the zip/jar. It then exits the synchronisation block and calls org.xmlresolver.loaders.XmlLoader.loadCatalog(URI) which is not synchronized, and T2 enters the synchronisation block and then we have a race condition as to who updates the catalog. This might look innocuous but if T2 exits having set the catalogMap entry to an empty entry then subsequent calls to org.xmlresolver.loaders.XmlLoader.loadCatalog(URI) will return that value, so T1 may call it after T2 has set it to an empty entry and so it will remain.
What happens after that I do not know but I created a test (org.xmlresolver.ConcurrencyTests) to check and it failed when the XML parser uses the enrty with "The markup declarations contained or pointed to by the document type declaration must be well-formed."
The fix is relatively straightforward and I have created a pull request for it. The solution is to wrap all the catalogMap update code in synchronized blocks on catalogMap and to ensure that only one operation passes through it at a time. We can of course still call
org.xmlresolver.loaders.XmlLoader.loadCatalog(URI) as before because we already have the lock.
The workaround I suppose is to use separate instances of org.xmlresolver.XMLResolverConfiguration per thread.
There is a pull request with the fix and tests https://github.com/xmlresolver/xmlresolver/pull/181
Thank you!