CSE 291
2018 April 11: Avoiding timeouts in HW1

The autograder opens a connection to your server, and sends a calculator program (e.g., test6.txt) from one of the test files. It then reads your response, and matches what you sent to what it was expecting (e.g., test6-ans.txt). When your response matches the reference response, the autograder closes the connection and reports success.

Some students have been running into an issue in homework 1 where they are getting a large number of timeouts from the autograder. A commonality between these submissions is that their core logic is structured as follows:

dynamic_buffer dynbuf;

while(connection is still open) {
	data = read(...);
	dynbuf.append(data);
}

CalcFramer framer;
framer.append(dynbuf);

while (framer.hasMessage()) {
	Message m = framer.topMessage();
	framer.popMessage();
	// do something with the message
}

The problem with the approaches that experience timeout errors is that the first while loop keeps reading data from the client and only breaks out when the autograder closes the connection. But the autograder will only close the connection when it has read a response from your server. So there is a deadlock here.

To fix this problem, adopt the following logic in your server:


CalcFramer framer;

while(connection is still open) {
	data = read(...);
	framer.append(data);

	while (framer.hasMessage()) {
		Message m = framer.topMessage();
		framer.popMessage();
		// do something with the message
	}
}

This loop-in-a-loop structure means that your server will read data from the client, process any messages, send back responses, then go back to reading from the client.

This approach should avoid timeout errors.