Skip to content

Fix ref cycles in callback handles #225

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

Merged
merged 2 commits into from
Feb 11, 2019
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
33 changes: 20 additions & 13 deletions uvloop/cbhandles.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ cdef class Handle:

if cb_type == 1:
callback = self.arg1
if callback is None:
raise RuntimeError(
'cannot run Handle; callback is not set')

args = self.arg2

if args is None:
Expand Down Expand Up @@ -106,11 +110,11 @@ cdef class Handle:
cdef _cancel(self):
self._cancelled = 1
self.callback = NULL
self.arg2 = self.arg3 = self.arg4 = None
self.arg1 = self.arg2 = self.arg3 = self.arg4 = None

cdef _format_handle(self):
# Mirrors `asyncio.base_events._format_handle`.
if self.cb_type == 1:
if self.cb_type == 1 and self.arg1 is not None:
cb = self.arg1
if isinstance(getattr(cb, '__self__', None), aio_Task):
try:
Expand All @@ -135,7 +139,7 @@ cdef class Handle:
if self._cancelled:
info.append('cancelled')

if self.cb_type == 1:
if self.cb_type == 1 and self.arg1 is not None:
func = self.arg1
# Cython can unset func.__qualname__/__name__, hence the checks.
if hasattr(func, '__qualname__') and func.__qualname__:
Expand All @@ -146,7 +150,7 @@ cdef class Handle:
cb_name = repr(func)

info.append(cb_name)
else:
elif self.meth_name is not None:
info.append(self.meth_name)

if self._source_traceback is not None:
Expand Down Expand Up @@ -214,6 +218,7 @@ cdef class TimerHandle:
if self.timer is None:
return

self.callback = None
self.args = None

try:
Expand All @@ -225,10 +230,11 @@ cdef class TimerHandle:
cdef _run(self):
if self._cancelled == 1:
return
if self.callback is None:
raise RuntimeError('cannot run TimerHandle; callback is not set')

callback = self.callback
args = self.args
self._clear()

Py_INCREF(self) # Since _run is a cdef and there's no BoundMethod,
# we guard 'self' manually.
Expand Down Expand Up @@ -275,15 +281,16 @@ cdef class TimerHandle:
if self._cancelled:
info.append('cancelled')

func = self.callback
if hasattr(func, '__qualname__'):
cb_name = getattr(func, '__qualname__')
elif hasattr(func, '__name__'):
cb_name = getattr(func, '__name__')
else:
cb_name = repr(func)
if self.callback is not None:
func = self.callback
if hasattr(func, '__qualname__'):
cb_name = getattr(func, '__qualname__')
elif hasattr(func, '__name__'):
cb_name = getattr(func, '__name__')
else:
cb_name = repr(func)

info.append(cb_name)
info.append(cb_name)

if self._source_traceback is not None:
frame = self._source_traceback[-1]
Expand Down
10 changes: 0 additions & 10 deletions uvloop/sslproto.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -453,13 +453,8 @@ cdef class SSLProtocol:
self._set_state(DO_HANDSHAKE)

# start handshake timeout count down
# XXX: This is a workaround to call cdef method and clear reference to
# self when the handle is cancelled (because uvloop currently clears
# only args, it'll be okay to remove the lambda x: x() after the fix to
# clear also the callback)
self._handshake_timeout_handle = \
self._loop.call_later(self._ssl_handshake_timeout,
lambda x: x(),
lambda: self._check_handshake_timeout())

try:
Expand Down Expand Up @@ -538,13 +533,8 @@ cdef class SSLProtocol:
self._abort(None)
else:
self._set_state(FLUSHING)
# XXX: This is a workaround to call cdef method and clear reference
# to self when the handle is cancelled (because uvloop currently
# clears only args, it'll be okay to remove the lambda x: x() after
# the fix to clear also the callback)
self._shutdown_timeout_handle = \
self._loop.call_later(self._ssl_shutdown_timeout,
lambda x: x(),
lambda: self._check_shutdown_timeout())
self._do_flush()

Expand Down