CQD
CQD

Reputation: 31

Socket server hang when testing with ab, waiting BufferedReader.readline()

I am making a Java-based web server. But when I am testing it with ApacheBench, it sometimes stop responding.

On a Macbook Air:

ab -n 20000 -c 40 -d  http://localhost:1080/

is guaranteed to timeout after 16400 or more requests were done.

On Ubuntu desktop

ab -n 20000 -c 1000 -d  http://localhost:1080/

could done successfully most of the time, but sometimes stop responding after several runs.

I've identified (using Eclipse) that when the server stop responding, it is waiting for BufferedReader.readline() which I use it to read HTTP request header. But I have no idea why is it waiting.

Test code is here:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class TestServer {
    public static void main(String[] args){
        ServerSocket socket = null;

        try{
            socket = new ServerSocket(1080);
            ExecutorService pool = Executors.newFixedThreadPool(10);
            while(true){
                Socket s = socket.accept();
                pool.execute(new RequestHandler(s) );
            }
        }
        catch(Exception e){
            e.printStackTrace();
        }
        finally{
            if(null!=socket){
                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }
}

class RequestHandler implements Runnable{
    final Socket s;
    public RequestHandler(Socket s) {
        this.s = s;
    }

    public void run() {
        try {
            BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
            String line = br.readLine();

            PrintWriter pw = new PrintWriter(s.getOutputStream());
            pw.print("HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n");
            pw.print(line);
            pw.flush();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        finally{
            if(s!=null){
                try {
                    s.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    }
}

BTW, when writing the test code, I found something else strange

If

BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
String line = br.readLine();

is replaced with

String line = "don't read the socket";

ab will fail with such message: "apr_socket_recv: Connection refused (111)Connection reset by peer (104)"

But open localhost:1080 with Firefox 4 will see the "don't read the socket" mess show up.

Upvotes: 0

Views: 1021

Answers (1)

Neil Coffey
Neil Coffey

Reputation: 21795

I wonder if this is a deliberate part of the ApacheBench test: to see how your server behaves when a connection is opened to it but then no data sent. Presumably ApacheBench is open source so you can have a look at see if it invokes some special behaviour (my bet is on it opening the socket and then not sending a request) after 16400 tries.

In any case, you probably want to make sure you set an explicit timeout on the socket in case your version of Java is defaulting to 0 (=infinite). Don't assume that every client will behave perfectly and always send you precisely the data you're expecting.

So as a general rule, you need to make sure that your web server doesn't fall over if "something unusual happens"-- networks are like that, and sometimes packets/connections will get randomly dropped and you need to deal with it. Operating systems may well impose limits on e.g. how long a connection can be open for and so your server could suddenly see the "rug pulled from beneath it's feet" by the OS. I imagine the ApacheBench test may simulate a few gremlins like this (which could even be what you're seeing in Ubuntu, though the readLine() hanging is probably a simulation of not sending a request on an open connection as I mention).

Upvotes: 1

Related Questions