XML-RPC

XmlRpcClient is not thread-safe

Created: 24/Oct/07 09:53 PM   Updated: 28/Nov/08 08:53 PM
Return to search
Component/s: None
Affects Version/s: 3.1
Fix Version/s: None

Time Tracking:
Not Specified

Environment: Core2duo, ubuntu gutsy (32bit), tomcat 6.0.13, sun's java 1.6.0_03.

Resolution Date: 12/Jan/08 04:16 PM


 Description  « Hide
The XmlRpcClient javadocs says it's thread-safe. However, when I have 2 or more threads accessing it concurrently I get errors. The errors are often different and look very much like race errors IMHO.

Here is the client code:
    public static void main( String[] args ) throws Exception {
        final XmlRpcClientConfigImpl config = new XmlRpcClientConfigImpl();
        config.setServerURL(MY_SERVER);
        final XmlRpcClient client = new XmlRpcClient();
        client.setConfig(config);
        try {
            final long stop=System.currentTimeMillis()+5000L;
            for (int i=0; i<10; i++) {
                final String id=""+i+": ";
                new Thread(new Runnable() {
                    public void run() {
                        System.err.println("Started thread "+id);
                        try {
                            while ( System.currentTimeMillis() < stop ) {
                                System.err.println( id + client.execute( "echo.echo", new Object[]{id+"foo"} ) );
                            }
                        } catch ( XmlRpcException x ) {
                            throw new RuntimeException( x );
                        }
                    }
                }).start();
            }
        } catch (Exception x) {
            x.printStackTrace();
        }
    }

Here is the server code:
public class MyXmlRpcServlet extends XmlRpcServlet {
    public static class EchoHandler {
        public String echo( String msg ) {
            return msg;
        }
    }
    @Override
    protected XmlRpcHandlerMapping newXmlRpcHandlerMapping() throws XmlRpcException {
        PropertyHandlerMapping handlers =new PropertyHandlerMapping();
        handlers.addHandler( "echo", EchoHandler.class );
        return handlers;
    }


Here are the first lines of a few of the errors I've got:

1)
Exception in thread "Thread-3" java.lang.RuntimeException: org.apache.xmlrpc.XmlRpcException: Failed to read servers response: Cannot write output after reading input.
at MyTest$1.run(MyTest.java:37)
at java.lang.Thread.run(Thread.java:595)
Caused by: org.apache.xmlrpc.XmlRpcException: Failed to read servers response: Cannot write output after reading input.
at org.apache.xmlrpc.client.XmlRpcStreamTransport.sendRequest(XmlRpcStreamTransport.java:150)
...

2)
Exception in thread "Thread-5" java.lang.RuntimeException: org.apache.xmlrpc.XmlRpcException: Failed to create input stream: Socket closed
at MyTest$1.run(MyTest.java:37)
at java.lang.Thread.run(Thread.java:595)
Caused by: org.apache.xmlrpc.XmlRpcException: Failed to create input stream: Socket closed
at org.apache.xmlrpc.client.XmlRpcSunHttpTransport.getInputStream(XmlRpcSunHttpTransport.java:65)
...

3)
Exception in thread "Thread-2" java.lang.NullPointerException
at sun.net.www.protocol.http.HttpURLConnection.writeRequests(HttpURLConnection.java:417)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:955)
at org.apache.xmlrpc.client.XmlRpcSunHttpTransport.getInputStream(XmlRpcSunHttpTransport.java:63)
at org.apache.xmlrpc.client.XmlRpcStreamTransport.sendRequest(XmlRpcStreamTransport.java:141)
...

4)
[Fatal Error] :-1:-1: Premature end of file.
Exception in thread "Thread-9" java.lang.RuntimeException: org.apache.xmlrpc.client.XmlRpcClientException: Failed to parse servers response: Premature end of file.
at MyTest$1.run(MyTest.java:37)
at java.lang.Thread.run(Thread.java:595)
Caused by: org.apache.xmlrpc.client.XmlRpcClientException: Failed to parse servers response: Premature end of file.
at org.apache.xmlrpc.client.XmlRpcStreamTransport.readResponse(XmlRpcStreamTransport.java:177)


 All   Comments   Work Log   Change History   Subversion Commits      Sort Order: Ascending order - Click to sort in descending order
Marcus Sundman added a comment - 24/Oct/07 10:01 PM
If each thread has its own XmlRpcClient instance then there are no errors. I.e., if you move the lines
        final XmlRpcClient client = new XmlRpcClient();
        client.setConfig(config);
into the run()-method then it'll work just fine.

Jochen Wiedmann added a comment - 27/Oct/07 06:34 PM
Indeed I found a problem in the XmlRpcSunHttpTransportFactory. Fixed. Please try the latest snapshot, as available from

    http://people.apache.org/repo/m2-snapshot-repository/org/apache/xmlrpc/
 

Jochen Wiedmann added a comment - 12/Jan/08 04:16 PM
No response from user, so assuming its fixed.

Marcus Sundman added a comment - 12/Jan/08 11:21 PM
Yup, seems to be fixed. Or at least 3.1.1-20071027.183056-3 doesn't throw the same exceptions anymore.

Jochen Wiedmann added a comment - 28/Nov/08 08:53 PM
Closing issues which have been released.