Skip to content

Commit 49ee697

Browse files
committed
ensuring things are properly cleared which fixes Lawouach#20
1 parent bbed6d5 commit 49ee697

4 files changed

Lines changed: 31 additions & 9 deletions

File tree

ws4py/framing.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,11 @@ def __init__(self, opcode=None, body='', masking_key=None, fin=0, rsv1=0, rsv2=0
4444
self.parser = self._parser()
4545
self.parser.next()
4646

47+
def _cleanup(self):
48+
if self.parser:
49+
self.parser.close()
50+
self.parser = None
51+
4752
def build(self):
4853
"""
4954
Builds a frame from the instance's attributes and returns

ws4py/messaging.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# -*- coding: utf-8 -*-
22
import os
33
import struct
4+
import copy
45

56
from ws4py.framing import Frame, OPCODE_CONTINUATION, OPCODE_TEXT, \
67
OPCODE_BINARY, OPCODE_CLOSE, OPCODE_PING, OPCODE_PONG

ws4py/streaming.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ def __init__(self, always_mask=False, expect_masking=True):
8282
self.always_mask = always_mask
8383
self.expect_masking = expect_masking
8484

85-
def release(self):
85+
def _cleanup(self):
8686
"""
8787
Frees the stream's resources rendering it unusable.
8888
"""
@@ -92,6 +92,7 @@ def release(self):
9292
self.errors = None
9393
self.pings = None
9494
self.pongs = None
95+
self.closing = None
9596

9697
def text_message(self, text):
9798
"""
@@ -166,13 +167,15 @@ def receiver(self):
166167
"""
167168
utf8validator = Utf8Validator()
168169
running = True
170+
frame = None
169171
while running:
170172
frame = Frame()
171173
while 1:
172174
try:
173175
bytes = (yield frame.parser.next())
174176
frame.parser.send(bytes)
175177
except StopIteration:
178+
frame._cleanup()
176179
bytes = frame.body or ''
177180

178181
# Let's avoid unmasking when there is no payload
@@ -185,7 +188,7 @@ def receiver(self):
185188
break
186189
elif frame.masking_key and not self.expect_masking:
187190
msg = CloseControlMessage(code=1002, reason='Masked when not expected')
188-
self.errors.append()
191+
self.errors.append(msg)
189192
break
190193
else:
191194
bytes = bytearray(bytes)
@@ -282,7 +285,14 @@ def receiver(self):
282285
break
283286

284287
frame.body = None
285-
frame.parser.close()
288+
frame = None
286289
utf8validator.reset()
290+
291+
if frame:
292+
frame._cleanup()
293+
frame = None
287294

295+
utf8validator.reset()
288296
utf8validator = None
297+
298+
self._cleanup()

ws4py/websocket.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -186,10 +186,11 @@ def _cleanup(self):
186186
Frees up resources used by the endpoint.
187187
"""
188188
self.sender = None
189-
self.stream.release()
190-
self.stream = None
191189
self.sock = None
192-
190+
self.environ = None
191+
self.stream._cleanup()
192+
self.stream = None
193+
193194
def run(self):
194195
"""
195196
Performs the operation of reading from the underlying
@@ -214,9 +215,9 @@ def run(self):
214215
in a thread.
215216
"""
216217
self.sock.setblocking(True)
218+
s = self.stream
217219
try:
218220
self.opened()
219-
s = self.stream
220221
sock = self.sock
221222
fileno = sock.fileno()
222223
process = self.process
@@ -230,13 +231,15 @@ def run(self):
230231

231232
try:
232233
if not s.closing:
233-
self.closed(1006)
234+
self.closed(1006, "Going away")
234235
else:
235236
self.closed(s.closing.code, s.closing.reason)
236237
finally:
238+
s = sock = fileno = process = None
237239
self.close_connection()
238240
self._cleanup()
239241

242+
240243
def process(self, bytes):
241244
""" Takes some bytes and process them through the
242245
internal stream's parser. If a message of any kind is
@@ -263,18 +266,21 @@ def process(self, bytes):
263266
self.close(s.closing.code, s.closing.reason)
264267
else:
265268
self.client_terminated = True
269+
s = None
266270
return False
267271

268272
if s.errors:
269273
for error in s.errors:
270274
self.close(error.code, error.reason)
271275
s.errors = []
276+
s = None
272277
return False
273278

274279
if s.has_message:
275280
self.received_message(s.message)
276281
s.message.data = None
277282
s.message = None
283+
s = None
278284
return True
279285

280286
if s.pings:
@@ -287,6 +293,7 @@ def process(self, bytes):
287293
self.ponged(pong)
288294
s.pongs = []
289295

296+
s = None
290297
return True
291298

292299
class EchoWebSocket(WebSocket):
@@ -296,4 +303,3 @@ def received_message(self, message):
296303
its originating endpoint.
297304
"""
298305
self.send(message.data, message.is_binary)
299-

0 commit comments

Comments
 (0)