Fix hang when client disconnects

Fix hang when client disconnects uncleanly or reads the stream too slowly.