From d6261b6d79e43606a70e2acd45d7350faa261a5f Mon Sep 17 00:00:00 2001 From: Lasse Blaauwbroek Date: Fri, 9 Jun 2023 21:59:20 +0200 Subject: [PATCH] Manually handle deallocation of some objects for the benefit of p3.7 Python 3.7 seems to have trouble dealocating objects in a timely fashion. We rely on this, because the c++ destructors need to run before the kj event loop is closed. Hence, we do it manually. --- capnp/lib/capnp.pyx | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/capnp/lib/capnp.pyx b/capnp/lib/capnp.pyx index fa6084e..73ac651 100644 --- a/capnp/lib/capnp.pyx +++ b/capnp/lib/capnp.pyx @@ -2039,6 +2039,11 @@ cdef class _DynamicCapabilityClient: cdef C_DynamicCapability.Client thisptr cdef public object _parent, _cached_schema + def __dealloc__(self): + # Needed to make Python 3.7 happy, which seems to have trouble deallocating stack objects + # appropriately + self.thisptr = C_DynamicCapability.Client() + cdef _init(self, C_DynamicCapability.Client other, object parent): self.thisptr = other self._parent = parent @@ -2194,6 +2199,11 @@ cdef class TwoPartyClient: cdef Own[RpcSystem] thisptr cdef _TwoPartyVatNetwork _network + def __dealloc__(self): + # Needed to make Python 3.7 happy, which seems to have trouble deallocating stack objects + # appropriately + self.thisptr = Own[RpcSystem]() + def __init__(self, socket=None, traversal_limit_in_words=None, nesting_limit=None): cdef schema_cpp.ReaderOptions opts = make_reader_opts(traversal_limit_in_words, nesting_limit) @@ -2224,6 +2234,11 @@ cdef class TwoPartyServer: cdef Own[RpcSystem] thisptr cdef _TwoPartyVatNetwork _network + def __dealloc__(self): + # Needed to make Python 3.7 happy, which seems to have trouble deallocating stack objects + # appropriately + self.thisptr = Own[RpcSystem]() + def __init__(self, socket=None, bootstrap=None, traversal_limit_in_words=None, nesting_limit=None): if not bootstrap: raise KjException("You must provide a bootstrap interface to a server constructor.") @@ -2250,6 +2265,11 @@ cdef class _AsyncIoStream: cdef Own[AsyncIoStream] thisptr cdef _EventLoop _event_loop # We hold a pointer to the event loop here, to ensure it remains alive + def __dealloc__(self): + # Needed to make Python 3.7 happy, which seems to have trouble deallocating stack objects + # appropriately + self.thisptr = Own[AsyncIoStream]() + @staticmethod async def create_connection(host = None, port = None, **kwargs): """Create a TCP connection.