Merge ~d0ugal/maas:twisted-patch-disconnect into maas:master

Proposed by Dougal Matthews
Status: Merged
Approved by: Dougal Matthews
Approved revision: bdd78ce833666a2bcf07d748dc1c7978cfe37294
Merge reported by: MAAS Lander
Merged at revision: not available
Proposed branch: ~d0ugal/maas:twisted-patch-disconnect
Merge into: maas:master
Diff against target: 105 lines (+73/-1)
2 files modified
src/maasserver/monkey.py (+33/-1)
src/maasserver/tests/test_monkey.py (+40/-0)
Reviewer Review Type Date Requested Status
Adam Collard (community) Approve
MAAS Lander Approve
Review via email: mp+393043@code.launchpad.net

Commit message

LP 1741913: Patch twisted.web.http.Request to include the upstream fix

See https://github.com/twisted/twisted/commit/169fd1d93b7af06bf0f6893b193ce19970881868

To post a comment you must log in.
d18058d... by Dougal Matthews

Make a note about the test origin and isort

Revision history for this message
MAAS Lander (maas-lander) wrote :

UNIT TESTS
-b twisted-patch-disconnect lp:~d0ugal/maas/+git/maas into -b master lp:~maas-committers/maas

STATUS: FAILED
LOG: http://maas-ci.internal:8080/job/maas/job/branch-tester/8574/console
COMMIT: 448505e89982e3a46784f4e14cbfb6093bd1cbde

review: Needs Fixing
Revision history for this message
MAAS Lander (maas-lander) wrote :

UNIT TESTS
-b twisted-patch-disconnect lp:~d0ugal/maas/+git/maas into -b master lp:~maas-committers/maas

STATUS: FAILED
LOG: http://maas-ci.internal:8080/job/maas/job/branch-tester/8575/console
COMMIT: d18058d203a07445b62414d854d1afcbb43e76a4

review: Needs Fixing
073b2d2... by Dougal Matthews

Return the parent. Not sure if this is needed.

016494d... by Dougal Matthews

Don't use super, is there a nicer way?

1474744... by Dougal Matthews

Blacken

Revision history for this message
MAAS Lander (maas-lander) wrote :

UNIT TESTS
-b twisted-patch-disconnect lp:~d0ugal/maas/+git/maas into -b master lp:~maas-committers/maas

STATUS: FAILED
LOG: http://maas-ci.internal:8080/job/maas/job/branch-tester/8576/console
COMMIT: 073b2d2f35193a896dfb358afb71f52b42e09d17

review: Needs Fixing
Revision history for this message
MAAS Lander (maas-lander) wrote :

UNIT TESTS
-b twisted-patch-disconnect lp:~d0ugal/maas/+git/maas into -b master lp:~maas-committers/maas

STATUS: SUCCESS
COMMIT: 1474744a2d0e4521563936fdce61600f82bca34a

review: Approve
Revision history for this message
Adam Collard (adam-collard) :
review: Needs Information
1d85128... by Dougal Matthews

Fix typo and add a test to check the version of Twisted needs patched

ae6faa3... by Dougal Matthews

Fix comments

bdd78ce... by Dougal Matthews

Update the test name

Revision history for this message
MAAS Lander (maas-lander) wrote :

UNIT TESTS
-b twisted-patch-disconnect lp:~d0ugal/maas/+git/maas into -b master lp:~maas-committers/maas

STATUS: FAILED
LOG: http://maas-ci.internal:8080/job/maas/job/branch-tester/8581/console
COMMIT: 1d85128bec0c8c965ddff517ce75e2e0348aa53e

review: Needs Fixing
Revision history for this message
Dougal Matthews (d0ugal) wrote :

jenkins: !test

Revision history for this message
MAAS Lander (maas-lander) wrote :

UNIT TESTS
-b twisted-patch-disconnect lp:~d0ugal/maas/+git/maas into -b master lp:~maas-committers/maas

STATUS: SUCCESS
COMMIT: bdd78ce833666a2bcf07d748dc1c7978cfe37294

review: Approve
Revision history for this message
Adam Collard (adam-collard) wrote :

+1

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1diff --git a/src/maasserver/monkey.py b/src/maasserver/monkey.py
2index d60c612..1b4be21 100644
3--- a/src/maasserver/monkey.py
4+++ b/src/maasserver/monkey.py
5@@ -1,4 +1,4 @@
6-# Copyright 2016-2017 Canonical Ltd. This software is licensed under the
7+# Copyright 2016-2020 Canonical Ltd. This software is licensed under the
8 # GNU Affero General Public License version 3 (see the file LICENSE).
9
10 """
11@@ -10,6 +10,7 @@ __all__ = ["add_patches"]
12 from collections import OrderedDict
13 import inspect
14
15+from twisted.web import http
16 import yaml
17
18 from provisioningserver.monkey import add_patches_to_twisted
19@@ -95,9 +96,40 @@ def fix_ordereddict_yaml_representer():
20 yaml.add_representer(OrderedDict, dumper, Dumper=yaml.SafeDumper)
21
22
23+OriginalRequest = http.Request
24+
25+
26+class PatchedRequest(OriginalRequest):
27+ def write(self, data):
28+ if self.finished:
29+ raise RuntimeError(
30+ "Request.write called on a request after "
31+ "Request.finish was called."
32+ )
33+
34+ if self._disconnected:
35+ # Don't attempt to write any data to a disconnected client.
36+ # The RuntimeError exception will be thrown as usual when
37+ # request.finish is called
38+ return
39+
40+ return OriginalRequest.write(self, data)
41+
42+
43+def fix_twisted_disconnect_write():
44+ """
45+ Patch twisted.web.http.Request to include the upstream fix
46+
47+ See https://github.com/twisted/twisted/commit/169fd1d93b7af06bf0f6893b193ce19970881868
48+ """
49+
50+ http.Request = PatchedRequest
51+
52+
53 def add_patches():
54 add_patches_to_twisted()
55 fix_django_deferred_attribute()
56 fix_piston_emitter_related()
57 fix_piston_consumer_delete()
58 fix_ordereddict_yaml_representer()
59+ fix_twisted_disconnect_write()
60diff --git a/src/maasserver/tests/test_monkey.py b/src/maasserver/tests/test_monkey.py
61new file mode 100644
62index 0000000..408af95
63--- /dev/null
64+++ b/src/maasserver/tests/test_monkey.py
65@@ -0,0 +1,40 @@
66+# Copyright 2020 Canonical Ltd. This software is licensed under the
67+# GNU Affero General Public License version 3 (see the file LICENSE).
68+
69+"""Test monkey patches."""
70+
71+__all__ = []
72+
73+from twisted import version as twisted_version
74+from twisted.internet.error import ConnectionLost
75+from twisted.python.failure import Failure
76+from twisted.web import http
77+from twisted.web.test.requesthelper import DummyChannel
78+
79+from maastesting.testcase import MAASTestCase
80+
81+
82+class TestTwistedDisconnectPatch(MAASTestCase):
83+ def test_write_after_connection_lost(self):
84+ """
85+ Calling L{Request.write} after L{Request.connectionLost} has been
86+ called should not throw an exception. L{RuntimeError} will be raised
87+ when finish is called on the request.
88+
89+ NOTE: This test is taken from the upstream fix to verify the monkey
90+ patch: https://github.com/twisted/twisted/commit/169fd1d93b7af06bf0f6893b193ce19970881868
91+ """
92+ channel = DummyChannel()
93+ req = http.Request(channel, False)
94+ req.connectionLost(Failure(ConnectionLost("The end.")))
95+ req.write(b"foobar")
96+ self.assertRaises(RuntimeError, req.finish)
97+
98+ def test_twisted_version(self):
99+
100+ self.assertLessEqual(
101+ (twisted_version.major, twisted_version.minor),
102+ (19, 7),
103+ "The fix_twisted_disconnect_write monkey patch is not longer "
104+ "required in Twisted versions >= 19.7.0",
105+ )

Subscribers

People subscribed via source and target branches