Ticket #5043: issue-5043.bugfix

File issue-5043.bugfix, 2.3 KB (added by RN, 6 years ago)

Patch

Line 
1Index: twisted/web/http.py
2===================================================================
3--- twisted/web/http.py (revision 31585)
4+++ twisted/web/http.py (working copy)
5@@ -862,9 +862,11 @@
6         Indicate that all response data has been written to this L{Request}.
7         """
8         if self._disconnected:
9-            raise RuntimeError(
10+            log.msg(
11                 "Request.finish called on a request after its connection was lost; "
12                 "use Request.notifyFinish to keep track of this.")
13+            return
14+
15         if self.finished:
16             warnings.warn("Warning! request.finish called twice.", stacklevel=2)
17             return
18Index: twisted/web/test/test_http.py
19===================================================================
20--- twisted/web/test/test_http.py       (revision 31585)
21+++ twisted/web/test/test_http.py       (working copy)
22@@ -6,8 +6,9 @@
23 """
24 
25 from urlparse import urlparse, urlunsplit, clear_cache
26-import random, urllib, cgi
27+import random, cgi
28 
29+from twisted.python import log
30 from twisted.python.compat import set
31 from twisted.python.failure import Failure
32 from twisted.trial import unittest
33@@ -1439,13 +1440,21 @@
34     def test_finishAfterConnectionLost(self):
35         """
36         Calling L{Request.finish} after L{Request.connectionLost} has been
37-        called results in a L{RuntimeError} being raised.
38+        called results in a log message issued.
39         """
40+        L = []
41+        expected = (
42+            "Request.finish called on a request after its connection " \
43+            "was lost; use Request.notifyFinish to keep track of this.")
44+
45+        log.addObserver(L.append)
46+        self.addCleanup(log.removeObserver, L.append)
47         channel = DummyChannel()
48         transport = channel.transport
49         req = http.Request(channel, False)
50         req.connectionLost(Failure(ConnectionLost("The end.")))
51-        self.assertRaises(RuntimeError, req.finish)
52+        req.finish()
53+        self.assertEquals(L[0]['message'][0], expected)
54 
55 
56 
57Index: twisted/web/topfiles/5043.bugfix
58===================================================================
59--- twisted/web/topfiles/5043.bugfix    (revision 0)
60+++ twisted/web/topfiles/5043.bugfix    (revision 0)
61@@ -0,0 +1,2 @@
62+http.Request.finish will just log a message instead of raising RuntimeError
63+when its connection was previously lost.