diff --git a/examples/bench/server.py b/examples/bench/server.py index 3298115..e79e5d4 100644 --- a/examples/bench/server.py +++ b/examples/bench/server.py @@ -32,7 +32,8 @@ async def echo_client(loop, client): if not data: break await loop.sock_sendall(client, data) - print('Connection closed') + if PRINT: + print('Connection closed') async def echo_client_streams(reader, writer): @@ -47,6 +48,7 @@ async def echo_client_streams(reader, writer): await writer.drain() if PRINT: print('Connection closed') + writer.close() async def print_debug(loop): diff --git a/uvloop/cbhandles.pyx b/uvloop/cbhandles.pyx index 8a2049d..36fcd4f 100644 --- a/uvloop/cbhandles.pyx +++ b/uvloop/cbhandles.pyx @@ -24,15 +24,21 @@ cdef class Handle: if self.cancelled == 1 or self.done == 1: return + callback = self.callback + args = self.args + + self.callback = None + self.args = None + self.done = 1 try: - if self.args is not None: - self.callback(*self.args) + if args is not None: + callback(*args) else: - self.callback() + callback() except Exception as ex: self.loop.call_exception_handler({ - 'message': 'Exception in callback {}'.format(self.callback), + 'message': 'Exception in callback {}'.format(callback), 'exception': ex }) diff --git a/uvloop/stream.pyx b/uvloop/stream.pyx index 48c044b..0a6e27f 100644 --- a/uvloop/stream.pyx +++ b/uvloop/stream.pyx @@ -38,13 +38,12 @@ cdef class __StreamWriteContext: return self.closed = 1 - - PyBuffer_Release(&self.py_buf) - + self.callback = None + PyBuffer_Release(&self.py_buf) # void + self.req.data = NULL Py_DECREF(self) - - IF DEBUG: - self.stream._loop._debug_stream_write_ctx_cnt -= 1 + IF not DEBUG: + self.stream = None IF DEBUG: def __dealloc__(self): @@ -52,6 +51,11 @@ cdef class __StreamWriteContext: raise RuntimeError( 'open __StreamWriteContext is being deallocated') + IF DEBUG: + self.stream._loop._debug_stream_write_ctx_cnt -= 1 + + self.stream = None + @cython.internal @cython.no_gc_clear