capnproto / pycapnp

Cap'n Proto serialization/RPC system - Python bindings
BSD 2-Clause "Simplified" License
458 stars 125 forks source link

Experiment: Wrap all capnp code in a context-manager to avoid segfaults #317

Closed LasseBlaauwbroek closed 9 months ago

LasseBlaauwbroek commented 1 year ago

This is me trying some things out. I'm aware that @haata hasn't signed off on #316 yet.

The main goal of this PR is to program defenses into Pycapnp such that a segfault can never be triggered from Python code. To achieve this, I've used the following strategy:

I've added a bunch of tests that used to segfault. Most likely there are more, but my theory is that with the current approach, we can solve all of those.

Feedback from @haata and @kentonv is appreciated on the validity of this approach.

Fixes #316

fabiorossetto commented 1 year ago

We have encountered what seems to be a related issue. We develop a C++ library that links statically to capnproto. This library can be used in Python as well through Python bindings. It seems that when we import our library before pycapnp, the event loop is not created and pycanp encounters a null point dereference when getting the event loop.

Having an explicit context instead of relying on global thread variables (as it seems to be the case for the event loop), would probably help us.

LasseBlaauwbroek commented 9 months ago

After getting a ping from @tobiasah: I believe that this is ready for review/merging. After this is merged, and https://github.com/capnproto/pycapnp/issues/323 is fixed, I'd suggest making a 2.0~beta release.