Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multi-threading bug in XmlScanner/FixedNsContext #29

Closed
bcoughlan opened this issue May 21, 2015 · 1 comment
Closed

Multi-threading bug in XmlScanner/FixedNsContext #29

bcoughlan opened this issue May 21, 2015 · 1 comment
Milestone

Comments

@bcoughlan
Copy link

I believe that there is a multi-threading bug in XmlScanner/FixedNsContext.

I suspect that this is because XmlScanner uses the static FixedNsContext.EMPTY_CONTEXT initially, so threads are sharing the _tmpDecl array in FixedNsContext.

I am not familiar enough with Aalto to produce a fix, but I did manage to create a reproducible example (you may need to run it a few times to get the error):

package performance;

import com.fasterxml.aalto.AsyncByteBufferFeeder;
import com.fasterxml.aalto.AsyncXMLInputFactory;
import com.fasterxml.aalto.AsyncXMLStreamReader;
import com.fasterxml.aalto.evt.EventAllocatorImpl;
import com.fasterxml.aalto.stax.InputFactoryImpl;

import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

import javax.xml.stream.util.XMLEventAllocator;

public class AaltoBug implements Runnable {

    private static String xml = "<?xml version='1.0'?><stream:stream xmlns='jabber:client' xmlns:stream='http://etherx.jabber.org/streams' id='4095288169' from='localhost' version='1.0' xml:lang='en'>";
    private static int NUM_THREADS = 5;
    private static XMLEventAllocator allocator = EventAllocatorImpl.getDefaultInstance();
    private static AsyncXMLInputFactory inputFactory = new InputFactoryImpl();

    public static void main(String[] args) throws InterruptedException {
        ExecutorService ex = Executors.newFixedThreadPool(NUM_THREADS);

        for (int i = 0; i < 100000; i++) {
            ex.submit(new AaltoBug(i));
        }

        ex.shutdown();
        ex.awaitTermination(Integer.MAX_VALUE, TimeUnit.SECONDS);
    }

    private final int count;

    public AaltoBug(int count) {
        this.count = count;
    }

    @Override
    public void run() {
        try {
            ByteBuffer bb = StandardCharsets.UTF_8.encode(xml);
            AsyncXMLStreamReader<AsyncByteBufferFeeder> parser = inputFactory.createAsyncForByteBuffer();
            parser.getInputFeeder().feedInput(bb);
            while (parser.hasNext()) {
                int eventType = parser.next();
                if (eventType == AsyncXMLStreamReader.EVENT_INCOMPLETE) {
                    break;
                }

                allocator.allocate(parser);
            }
        } catch (Exception e) {
            System.out.println("Error in " + count);
            e.printStackTrace();
        }
    }
}

Which produces this exception:

java.lang.ArrayIndexOutOfBoundsException: 10
    at java.util.ArrayList.add(ArrayList.java:459)
    at com.fasterxml.aalto.in.FixedNsContext.reuseOrCreate(FixedNsContext.java:76)
    at com.fasterxml.aalto.in.XmlScanner.getNonTransientNamespaceContext(XmlScanner.java:916)
    at com.fasterxml.aalto.stax.StreamReaderImpl.getNonTransientNamespaceContext(StreamReaderImpl.java:1528)
    at org.codehaus.stax2.ri.evt.Stax2EventAllocatorImpl.createStartElement(Stax2EventAllocatorImpl.java:160)
    at org.codehaus.stax2.ri.evt.Stax2EventAllocatorImpl.allocate(Stax2EventAllocatorImpl.java:69)
    at com.fasterxml.aalto.evt.EventAllocatorImpl.allocate(EventAllocatorImpl.java:103)
    at performance.AaltoBug.run(AaltoBug.java:61)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)
bcoughlan pushed a commit to bcoughlan/aalto-xml that referenced this issue May 22, 2015
@cowtowncoder cowtowncoder added this to the 1.0.0 milestone Nov 24, 2015
@cowtowncoder
Copy link
Member

First of all, thank you for reporting this and providing reproduction. Second all, sorry for the delay in addressing it.
But for what it is worth, I fixed this and it will be included in 1.0.0, due to release soon.

cowtowncoder added a commit that referenced this issue Jun 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants