Skip to content

Commit

Permalink
Merge pull request #20 from w3c/jgraham/suppress_socket_error
Browse files Browse the repository at this point in the history
Swallow the error if we try to write to a socket the remote end closed
  • Loading branch information
andreastt committed Jul 22, 2014
2 parents cb76946 + 455a29f commit 4f52e3b
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
8 changes: 6 additions & 2 deletions wptserve/request.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,15 @@ def read(self, bytes=-1):
else:
old_data = ""

assert self._buf_position == self._file_position
assert self._buf_position == self._file_position, (
"Before reading buffer position (%i) didn't match file position (%i)" %
(self._buf_position, self._file_position))
new_data = self._file.read(bytes_remaining)
self._buf.write(new_data)
self._file_position += bytes_remaining
assert self._buf_position == self._file_position
assert self._buf_position == self._file_position, (
"After reading buffer position (%i) didn't match file position (%i)" %
(self._buf_position, self._file_position))

return old_data + new_data

Expand Down
13 changes: 11 additions & 2 deletions wptserve/response.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import logging
import types
import uuid
import socket

from constants import response_codes

Expand Down Expand Up @@ -418,7 +419,11 @@ def write(self, data):
"""Write directly to the response, converting unicode to bytes
according to response.encoding. Does not flush."""
self.content_written = True
self._wfile.write(self.encode(data))
try:
self._wfile.write(self.encode(data))
except socket.error:
# This can happen if the socket got closed by the remote end
pass

def encode(self, data):
"""Convert unicode to bytes according to response.encoding."""
Expand All @@ -431,4 +436,8 @@ def encode(self, data):

def flush(self):
"""Flush the output."""
self._wfile.flush()
try:
self._wfile.flush()
except socket.error:
# This can happen if the socket got closed by the remote end
pass

0 comments on commit 4f52e3b

Please sign in to comment.