Merge lp:~blake-rouse/maas/fix-1628213-1628213 into lp:~maas-committers/maas/trunk

Proposed by Blake Rouse
Status: Merged
Approved by: Blake Rouse
Approved revision: no longer in the source branch.
Merged at revision: 5419
Proposed branch: lp:~blake-rouse/maas/fix-1628213-1628213
Merge into: lp:~maas-committers/maas/trunk
Diff against target: 53 lines (+28/-1)
2 files modified
src/maasserver/websockets/tests/test_websockets.py (+10/-1)
src/maasserver/websockets/websockets.py (+18/-0)
To merge this branch: bzr merge lp:~blake-rouse/maas/fix-1628213-1628213
Reviewer Review Type Date Requested Status
Andres Rodriguez (community) Approve
Review via email: mp+307198@code.launchpad.net

Commit message

Fix websocket protocol in Twisted 16.3+.

To post a comment you must log in.
Revision history for this message
Andres Rodriguez (andreserl) wrote :

lgtm!, but there's a comment inline that needs to be addressed before landing.

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'src/maasserver/websockets/tests/test_websockets.py'
2--- src/maasserver/websockets/tests/test_websockets.py 2016-02-23 00:06:54 +0000
3+++ src/maasserver/websockets/tests/test_websockets.py 2016-09-30 13:23:49 +0000
4@@ -50,12 +50,21 @@
5 )
6 from twisted.web.test.test_web import (
7 DummyChannel,
8- DummyRequest,
9+ DummyRequest as DummyRequestBase,
10 )
11 from zope.interface import implementer
12 from zope.interface.verify import verifyObject
13
14
15+class DummyRequest(DummyRequestBase):
16+
17+ content = None
18+
19+ def _cleanup(self):
20+ """Called to cleanup the request."""
21+ pass
22+
23+
24 class TestFrameHelpers(MAASTestCase):
25 """
26 Test functions helping building and parsing WebSockets frames.
27
28=== modified file 'src/maasserver/websockets/websockets.py'
29--- src/maasserver/websockets/websockets.py 2015-11-29 23:59:47 +0000
30+++ src/maasserver/websockets/websockets.py 2016-09-30 13:23:49 +0000
31@@ -719,6 +719,24 @@
32 transport.protocol = protocol
33 protocol.makeConnection(transport)
34
35+ # Starting with Twisted 16.3+ `_cleanup` must be called on the request
36+ # so the socket is placed back into the reactor. `_cleanup` has always
37+ # existed, but was not required to be called until Twisted 16.3+.
38+ # `_cleanup` in Twisted 16.3+ checks to ensure that if content is None
39+ # to not call close, in previous versions it does not. To make this
40+ # code work on both all Twisted versions we must set content to
41+ # EmptyContent so an `AttributeError` is not raied when the content is
42+ # None. Only then can `_cleanup` be called safely. `finalize` also
43+ # exists on the request and is actually the public API, but this
44+ # method cannot be called because it will write invalid content to the
45+ # socket which will break the websocket connection.
46+ if request.content is None:
47+ class EmptyContent:
48+ def close(*args, **kwargs):
49+ pass
50+ request.content = EmptyContent()
51+ request._cleanup()
52+
53 return NOT_DONE_YET
54
55