Fix concurrency issues in XStreamMarshaller
Prior to this commit, if an instance of XStreamMarshaller was used concurrently from multiple threads without having first invoked the afterPropertiesSet() method, the private `xstream` field could be initialized multiple times resulting in a ConcurrentModificationException in XStream's internal DefaultConverterLookup. This commit fixes these concurrency issues by making the `xstream` field volatile and by implementing a double-checked locking algorithm in getXStream() to avoid concurrent instance creation. Closes gh-25017
This commit is contained in:
@@ -21,10 +21,10 @@ import java.io.Reader;
|
||||
import java.io.StringReader;
|
||||
import java.io.StringWriter;
|
||||
import java.io.Writer;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.stream.IntStream;
|
||||
|
||||
import javax.xml.parsers.DocumentBuilder;
|
||||
import javax.xml.parsers.DocumentBuilderFactory;
|
||||
@@ -185,13 +185,23 @@ class XStreamMarshallerTests {
|
||||
@Test
|
||||
void converters() throws Exception {
|
||||
marshaller.setConverters(new EncodedByteArrayConverter());
|
||||
byte[] buf = new byte[]{0x1, 0x2};
|
||||
Writer writer = new StringWriter();
|
||||
marshaller.marshal(buf, new StreamResult(writer));
|
||||
assertThat(XmlContent.from(writer)).isSimilarTo("<byte-array>AQI=</byte-array>");
|
||||
Reader reader = new StringReader(writer.toString());
|
||||
byte[] bufResult = (byte[]) marshaller.unmarshal(new StreamSource(reader));
|
||||
assertThat(Arrays.equals(buf, bufResult)).as("Invalid result").isTrue();
|
||||
byte[] buf = {0x1, 0x2};
|
||||
|
||||
// Execute multiple times concurrently to ensure there are no concurrency issues.
|
||||
// See https://github.com/spring-projects/spring-framework/issues/25017
|
||||
IntStream.rangeClosed(1, 100).parallel().forEach(n -> {
|
||||
try {
|
||||
Writer writer = new StringWriter();
|
||||
marshaller.marshal(buf, new StreamResult(writer));
|
||||
assertThat(XmlContent.from(writer)).isSimilarTo("<byte-array>AQI=</byte-array>");
|
||||
Reader reader = new StringReader(writer.toString());
|
||||
byte[] bufResult = (byte[]) marshaller.unmarshal(new StreamSource(reader));
|
||||
assertThat(bufResult).as("Invalid result").isEqualTo(buf);
|
||||
}
|
||||
catch (Exception ex) {
|
||||
throw new RuntimeException(ex);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
Reference in New Issue
Block a user