Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Break Circular References #43 #44

Merged
merged 2 commits into from
Sep 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions telnetlib3/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import asyncio
import logging
import signal
from weakref import proxy

# local
from . import server_base
Expand Down Expand Up @@ -111,7 +112,7 @@ def check_negotiation(self, final=False):
encoding = self.encoding(outgoing=True, incoming=True)
if not self.waiter_encoding.done() and result:
self.log.debug('encoding complete: {0!r}'.format(encoding))
self.waiter_encoding.set_result(self)
self.waiter_encoding.set_result(proxy(self))

elif (not self.waiter_encoding.done() and
self.writer.remote_option.get(TTYPE) is False):
Expand All @@ -120,13 +121,13 @@ def check_negotiation(self, final=False):
# the distant end would not support it, declaring encoding failed.
self.log.debug('encoding failed after {0:1.2f}s: {1}'
.format(self.duration, encoding))
self.waiter_encoding.set_result(self)
self.waiter_encoding.set_result(proxy(self))
return parent

elif not self.waiter_encoding.done() and final:
self.log.debug('encoding failed after {0:1.2f}s: {1}'
.format(self.duration, encoding))
self.waiter_encoding.set_result(self)
self.waiter_encoding.set_result(proxy(self))
return parent

return parent and result
Expand Down
13 changes: 9 additions & 4 deletions telnetlib3/server_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import logging
import datetime
import sys
from weakref import proxy

from .stream_writer import (TelnetWriter, TelnetWriterUnicode)
from .stream_reader import (TelnetReader, TelnetReaderUnicode)
Expand Down Expand Up @@ -85,7 +86,11 @@ def connection_lost(self, exc):
self._transport.close()
self._waiter_connected.cancel()
if self.shell is None:
self._waiter_closed.set_result(self)
self._waiter_closed.set_result(proxy(self))

# break circular refrences.
self._transport = None
self.reader.fn_encoding = None

def connection_made(self, transport):
"""
Expand Down Expand Up @@ -134,7 +139,7 @@ def begin_shell(self, result):
if asyncio.iscoroutine(coro):
fut = self._loop.create_task(coro)
fut.add_done_callback(
lambda fut_obj: self._waiter_closed.set_result(self))
lambda fut_obj: self._waiter_closed.set_result(proxy(self)))

def data_received(self, data):
"""Process bytes received by transport."""
Expand Down Expand Up @@ -277,11 +282,11 @@ def _check_negotiation_timer(self):
if self.check_negotiation(final=final):
self.log.debug('negotiation complete after {:1.2f}s.'
.format(self.duration))
self._waiter_connected.set_result(self)
self._waiter_connected.set_result(proxy(self))
elif final:
self.log.debug('negotiation failed after {:1.2f}s.'
.format(self.duration))
self._waiter_connected.set_result(self)
self._waiter_connected.set_result(proxy(self))
else:
# keep re-queuing until complete
self._check_later = self._loop.call_later(
Expand Down
11 changes: 11 additions & 0 deletions telnetlib3/stream_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,17 @@ def __init__(self, transport, protocol, *, client=False, server=False,

# Base protocol methods

def close(self):
super().close()
# break circular refs
self._ext_callback.clear()
self._ext_send_callback.clear()
self._slc_callback.clear()
self._iac_callback.clear()
self.fn_encoding = None
self._protocol = None
self._transport = None

def __repr__(self):
"""Description of stream encoding state."""
info = ['TelnetWriter']
Expand Down
9 changes: 9 additions & 0 deletions telnetlib3/tests/test_shell.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Test the server's shell(reader, writer) callback."""
# std imports
import asyncio
import weakref

# local imports
import telnetlib3
Expand All @@ -22,6 +23,7 @@ async def test_telnet_server_shell_as_coroutine(event_loop, bind_host,
from telnetlib3.telopt import IAC, DO, WONT, TTYPE
# given,
_waiter = asyncio.Future()
_saved = weakref.WeakSet()
send_input = 'Alpha'
expect_output = 'Beta'
expect_hello = IAC + DO + TTYPE
Expand All @@ -33,6 +35,10 @@ def shell(reader, writer):
inp = yield from reader.readexactly(len(send_input))
assert inp == send_input
writer.write(expect_output)
_saved.add(writer)
_saved.add(writer._protocol)
yield from writer.drain()
writer.close()

# exercise,
await telnetlib3.create_server(
Expand Down Expand Up @@ -61,6 +67,9 @@ def shell(reader, writer):

# verify,
assert server_output.decode('ascii') == expect_output

# no leaks
assert len(_saved) == 0


@pytest.mark.asyncio
Expand Down