Commit graph

877 commits

Author SHA1 Message Date
Lasse Blaauwbroek
b29f18ed64 Cleanup 2023-06-08 02:29:24 +02:00
Lasse Blaauwbroek
97bdeaea12 Disable option to run capnp without asyncio 2023-06-08 02:27:38 +02:00
Lasse Blaauwbroek
6e011cfe78 Get rid of capnp timer functionality.
The asyncio timer should now be used
2023-06-07 20:55:43 +02:00
Lasse Blaauwbroek
8feb377217 Allow reading and writing messages from sockets in async mode 2023-06-07 11:24:43 -07:00
Lasse Blaauwbroek
d53aa24733 Allow async capability implementation methods to return a tuple 2023-06-07 10:51:53 -07:00
Lasse Blaauwbroek
c037342615 Allow capability implementation methods to be async 2023-06-07 10:51:53 -07:00
Lasse Blaauwbroek
d32854eb00
Integrate the KJ event loop into Python's asyncio event loop (#310)
* Integrate the KJ event loop into Python's asyncio event loop

Fix #256

This PR attempts to remove the slow and expensive polling behavior for asyncio
in favor of proper linking of the KJ event loop to the asyncio event loop.

* Don't memcopy buffer

* Improve promise cancellation and prepare for timer implementation

* Add attribution for asyncProvider.cpp

* Implement timeout

* Cleanup

* First round of simplifications

* Add more a_wait functions and a shutdown function

* Fix edge-cases with loop shutdown

* Clean up calculator examples

* Cleanup

* Cleanup

* Reformat

* Fix warnings

* Reformat again

* Compatibility with macos

* Inline the asyncio loop in some places where this is feasible

* Add todo

* Fix

* Remove synchronous wait

* Wrap fd listening callbacks in a class

* Remove poll_forever

* Remove the thread-local/thread-global optimization

This will not matter much soon anyway, and simplifies things

* Share promise code by using fused types

* Improve refcounting of python objects in promises

We replace many instances of PyObject* by Own<PyRefCounter> for more automatic
reference management.

* Code wrapPyFunc in a similar way to wrapPyFuncNoArg

* Refactor capabilityHelper, fix several memory bugs for promises and add __await__

* Improve promise ownership, reduce memory leaks

Promise wrappers now hold a Own<Promise<Own<PyRefCounter>>> object. This might
seem like excessive nesting of objects (which to some degree it is, but with
good reason):
- The outer Own is needed because Cython cannot allocate objects without a
  nullary constructor on the stack (Promise doesn't have a nullary constructor).
  Additionally, I believe it would be difficult or impossible to detect when a
  promise is cancelled/moved if we use a bare Promise.
- Every promise returns a Owned PyRefCounter. PyRefCounter makes sure that a
  reference to the returned object keeps existing until the promise is fulfilled
  or cancelled. Previously, this was attempted using attach, which is redundant
  and makes reasoning about PyINCREF and PyDECREF very difficult.
- Because a promise holds a Own<Promise<...>>, when we perform any kind of
  action on that promise (a_wait, then, ...), we have to explicitly move() the
  ownership around. This will leave the original promise with a NULL-pointer,
  which we can easily detect as a cancelled promise.

Promises now only hold references to their 'parents' when strictly needed. This
should reduce memory pressure.

* Simplify and test the promise joining functionality

* Attach forgotten parent

* Catch exceptions in add_reader and friends

* Further cleanup of memory leaks

* Get rid of a_wait() in examples

* Cancel all fd read operations when the python asyncio loop is closed

* Formatting

* Remove support for capnp < 7000

* Bring asyncProvider.cpp more in line with upstream async-io-unix.c++

It was originally copied from the nodejs implementation, which in turn copied
from async-io-unix.c++. But that copy is pretty old.

* Fix a bug that caused file descriptors to never be closed

* Implement AsyncIoStream based on Python transports and protocols

* Get rid of asyncProvider

All asyncio now goes through _AsyncIoStream

* Formatting

* Add __dict__ to  PyAsyncIoStreamProtocol for python 3.7

* Reintroduce strange ipv4/ipv6 selection code to make ci happy

* Extra pause_reading()

* Work around more python bugs

* Be careful to only close transport when this is still possible

* Move pause_reading() workaround
2023-06-06 11:08:15 -07:00
Jacob Alexander
ed894304a3
Update README.md with stub generation tool. 2023-04-24 09:34:25 -07:00
Lasse Blaauwbroek
6d5ee1c6f2
Use cibuildwheel in ci (#309)
* Use cibuildwheel in ci

`cibuildwheel` is a system that automatically compiles and repairs wheels for
many python versions and architectures at once. This has some advantages vs the
old situation:
- Macosx wheels had inconsistent minimum versions ranging between 10.9 and
  11.0. I'm not sure why this happens, but for some users this means they have
  to build from source on macosx. With cibuildwheel, the build is
  consistent, with 10.9 as the minimum for x86 and 11.0 for arm64.
- Consolidation between the packaging tests and manylinux tests.
- Addition of musllinux targets and additional cross-compilation to ppc64le and
s390x.
- With cibuildwheel, new python versions should be automatically picked up.
- Separation of the sdist build and lint checks. There is not reason to run that
  many times.

All possible build targets succeed, except for ARM64 on Windows. The upstream
capnp build fails. I've disabled it.

The cross-compilation builds on linux are pretty slow. This could potentially be
sped up by separating the builds of manylinux and musllinux, but I'm not sure if
it's worth the extra complexity. (One can also contemplate disabling these
targets.)

Tests for macosx arm64 cannot be run (but also couldn't be run in the previous
system. This should be remedied once apple silicon becomes available on the CI.

I've also added some commented-out code that can automatically take care of
uploading a build to PyPi when a release is created. One might contemplate using this.

* Set CMAKE_OSX_ARCHITECTURES for arm64 and disable universal2
2023-04-03 18:55:17 -07:00
Lasse Blaauwbroek
701cabe61d
Make pycapnp more GIL friendly (#308)
* Mark c++ capnp api with nogil

* Release gil before blocking in capnp during message reading
2023-03-13 09:56:02 -07:00
Jacob Alexander
5061cdd1ee Fix black formatting 2023-03-12 11:33:41 -07:00
Jacob Alexander
d62ba169c9 Update GitHub Actions per deprecation notice
Node.js 12 actions are deprecated. Please update the following actions to use Node.js 16: actions/checkout@v2, actions/setup-python@v1. For more information see: https://github.blog/changelog/2022-09-22-github-actions-all-actions-will-begin-running-on-node16-instead-of-node12/.

Node.js 12 actions are deprecated. Please update the following actions to use Node.js 16: docker/setup-qemu-action@v1. For more information see: https://github.blog/changelog/2022-09-22-github-actions-all-actions-will-begin-running-on-node16-instead-of-node12/.
2023-01-27 14:18:27 -08:00
Jacob Alexander
b791c61f82 Update README.md 2023-01-26 16:55:21 -08:00
Jacob Alexander
33c453eff7 Increment version to v1.3.0 2023-01-26 16:43:40 -08:00
Matt Sachtler
01c4469e08
Add Python 3.11 to Github Actions builds (#306)
* Add Python3.11 to the Github Actions, in the manylinux2014 build as well as the
packaging test.

I added a second image, x86_64, to the manylinux2014 build matrix - the i686
build didn't immediately pass, so I left it out.

* Move the bundled capnproto library forward to 0.10.3.

* Remove the 2010 build entirely, and update the 2014 build to include i686.
2023-01-26 16:28:21 -08:00
André Cruz
1b19b6db13
Prevent race condition in example code (#305)
The example async server code uses timeouts around read() operations.
However, this has a race condition where data can be read, the timeout
fires, and the data is lost.

These timeouts are not really needed in this example code, so I removed
them to prevent people from having strange issues with lost messages
and undefined RPC behavior when using the example code.
2023-01-08 22:29:23 -08:00
Jacob Alexander
9b6ff51f9c Update to v1.2.2 2022-12-01 21:58:30 -08:00
Jacob Alexander
56ef658e46 Update bundled bundled capnp to 0.8.1 due to CVE-2022-46149
- This only affects pycapnp if you're using bundled capnp (otherwise
  just update libcapnp)
  * pypi packages are affected
2022-11-30 10:18:06 -08:00
Lasse Blaauwbroek
928dd13652
Bundle lib/capnp_api.h and helpers/capabilityHelper.cpp (#301) 2022-11-04 11:20:38 -07:00
Sergey Dmitriev
e614da1025
Avoid reading random values for reader options from dangling reference (#300)
This patch fixes a problem of reading random values for reader options
in pycapnp. The code which adds task to the list captures 'opts' by
reference and that causes a problem in case when 'opts' is allocated on
on the caller's stack. By the time when task is handled the stack frame
holding the 'opts' is gone which leaves dangling reference to 'opts' in
lambda's captures. As a result pycapnp reads random values for reader
options which sometimes causes unexpected errors (for example an error
that nesting level ius negative).
2022-11-01 13:00:01 -07:00
Jacob Alexander
1814105967 Incrementing version to v1.2.1 2022-09-11 23:08:45 -07:00
Madhava Jay
f45a9d0331
Fixing issue with m1 build (#297)
* Fixing issue with m1 build

- prevent earlier build step from running on arm64
- forced clean up of bundled dir
- adding --force-bundled-libcapnp to trigger a rebuild
- echoing out env variables and showing final wheel size

* Disable tests on Apple Silicon cross compile
2022-09-09 22:14:23 -07:00
Jacob Alexander
583fe9e42d Incrementing version to v1.2.0 2022-08-29 01:13:39 -07:00
Madhava Jay
7862d2a0c4
Added macos arm64 Apple Silicon to CI (#296)
* Added macos arm64 Apple Silicon to CI

- Added missing python 3.10 metadata to setup.py

* Need all python versions in the matrix

* Fixing github action matrix syntax

* Changing MACOSX_DEPLOYMENT_TARGET for older python versions

- Technically no one would be running python 3.7 on M1 anyway

* Adding windows visualcpp build tools and updated cmake

* Increase max-parallel for all matrix builds

* yaml indent

* trying visualstudio2022-workload-vctools

* disabled everything but windows and added tunshell for debugging

* try older powershell

* trying again

* try python instead of powershell

* bash with wget

* Using old windows client on other side

* wrong way

* backwards

* :(

* urgh back to caveman debugging

- trying older windows runners

* try it again with extra build tools

* Reverting back to windows-2019 runner for windows builds

Fixing error:
CMake Error at CMakeLists.txt:2 (project):
  Generator
    Ninja
  does not support platform specification, but platform
    x64
  was specified.
2022-08-26 08:13:09 -07:00
Jacob Alexander
a6f4ece776
Merge pull request #295 from rmelick-muon/from-bytes-context-manager
Update documentation about from_bytes to use context manager. #287
2022-07-12 09:53:48 -07:00
rmelick
6d531f0575 Update documentation about from_bytes to use context manager. #287 2022-07-12 09:19:54 -07:00
Jacob Alexander
e93b0452cc Incrementing version to v1.1.1 2022-05-23 21:39:51 -07:00
Jacob Alexander
1415cee087
Merge pull request #285 from madhavajay/madhava/python_310
Add Python 3.10 support
2022-05-23 21:08:24 -07:00
Madhava Jay
9c4f80ad21 Fixed linting issues
- re-enabled lint checks
2022-05-24 11:42:53 +10:00
Madhava Jay
fc617142c0 Fixed issue where pickle helpers didnt use new from_bytes with context
- Added with context to benchmarks that use with_bytes
2022-05-24 11:38:27 +10:00
Madhava Jay
26de61d847 Merge branch 'fix-mmap-buf' into madhava/python_310 2022-05-24 10:42:57 +10:00
Madhava Jay
ea776a2f55 Merge branch 'master' into fix-mmap-buf 2022-05-24 10:40:32 +10:00
Madhava Jay
ea70dac5cb Fixed black issues
- Disabled flake8 checks from blocking CI
- Added python 3.10 to packaging and manylinux
2022-04-06 13:32:52 +10:00
Jacob Alexander
d49f4a9813
Merge pull request #282 from poke1024/buf-release-test-case
add test case for buffer release issue
2022-03-09 07:14:23 -08:00
Bernhard Liebl
e2f3e5da46 adapted test cases to:
- work with contextual from_bytes()
- add regression test against original issue with buf release (see test_roundtrip_bytes_buffer)
2022-01-18 12:13:12 +01:00
Bernhard Liebl
c8c168fdd0 add test case for buffer release issue 2022-01-18 12:04:01 +01:00
Bernhard Liebl
4b75fe3b5e fix for unreleased buffers under mmap (issue 280) 2022-01-17 15:48:27 +01:00
Jacob Alexander
d1bd4cb256
Merge pull request #274 from timthelion/patch-1
Fix doc string for _DynamicResizableListBuilder
2021-10-06 11:17:13 -07:00
Timothy Hobbs
edb4dc647d
Fix doc string for _DynamicResizableListBuilder 2021-10-06 10:11:43 +02:00
Jacob Alexander
6e7fffd7de
Applying black formatting
- Fixing flake8 configuration to agree with black
- Adding black validation check to github actions
2021-10-01 11:12:21 -07:00
Jacob Alexander
5dade41aeb
Merge pull request #271 from odidev/odidev_pycapnp
Add linux aarch64 wheel build support
2021-08-25 10:15:26 -07:00
odidev
c952d42e92 Add linux aarch64 wheel build support
Signed-off-by: odidev <odidev@puresoftware.com>
2021-08-25 10:49:01 +00:00
Jacob Alexander
6aa59476a0
Incrementing to v1.1.0 2021-06-07 21:30:48 -07:00
Jacob Alexander
08906920d1
Merge pull request #268 from jayvdb/changelog
Update CHANGELOG.md
2021-06-07 16:23:12 -07:00
John Vandenberg
0a2b10ae02 Update CHANGELOG.md 2021-06-08 07:02:29 +08:00
Jacob Alexander
e7b45a0571
Merge pull request #267 from jayvdb/patch-1
README.md: Mention Python.h is needed
2021-06-02 08:46:30 -07:00
John Vandenberg
4c8e68908f
README.md: Mention Python.h is needed 2021-06-02 14:10:31 +08:00
Jacob Alexander
e73261fe84
Merge pull request #263 from jayvdb/bare-except
Fix bare except
2021-06-01 13:30:55 -07:00
John Vandenberg
91550e5533 tox.ini: Update supported Pythons 2021-06-02 03:06:05 +08:00
John Vandenberg
2850b3792d packagingtest.yml: Prevent bare except
Fixes https://github.com/capnproto/pycapnp/issues/254
2021-06-01 16:31:14 +08:00