Skip to content

Commit

Permalink
Don't write data to disconnected http clients
Browse files Browse the repository at this point in the history
This is a fix for trac #9410
  • Loading branch information
Jim Switzer authored and Jim Switzer committed Feb 27, 2019
1 parent 7d78d1f commit 169fd1d
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/twisted/web/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -1078,6 +1078,13 @@ def write(self, data):
if self.finished:
raise RuntimeError('Request.write called on a request after '
'Request.finish was called.')

if self._disconnected:
# Don't attempt to write any data to a disconnected client.
# The RuntimeError exception will be thrown as usual when
# request.finish is called
return

if not self.startedWriting:
self.startedWriting = 1
version = self.clientproto
Expand Down
13 changes: 13 additions & 0 deletions src/twisted/web/test/test_http.py
Original file line number Diff line number Diff line change
Expand Up @@ -2932,6 +2932,19 @@ def test_finishAfterConnectionLost(self):
self.assertRaises(RuntimeError, req.finish)


def test_writeAfterConnectionLost(self):
"""
Calling L{Request.write} after L{Request.connectionLost} has been
called should not throw an exception. L{RuntimeError} will be raised
when finish is called on the request.
"""
channel = DummyChannel()
req = http.Request(channel, False)
req.connectionLost(Failure(ConnectionLost("The end.")))
req.write(b'foobar')
self.assertRaises(RuntimeError, req.finish)


def test_reprUninitialized(self):
"""
L{Request.__repr__} returns the class name, object address, and
Expand Down

0 comments on commit 169fd1d

Please sign in to comment.