Concurrency problems with archive catalogs and XmlLoader #182

Closed
opened 2024-06-10 13:19:13 +02:00 by JFK-DXML · 1 comment
JFK-DXML commented 2024-06-10 13:19:13 +02:00 (Migrated from github.com)

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

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
ndw commented 2024-08-05 17:41:33 +02:00 (Migrated from github.com)

Thank you!

Thank you!
Sign in to join this conversation.
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
XmlResolver/xmlresolver#182
No description provided.