2013-10-15 22:36:14 -07:00
|
|
|
import pytest
|
2023-10-28 07:03:42 +02:00
|
|
|
import asyncio
|
2013-10-15 22:36:14 -07:00
|
|
|
|
2019-09-26 22:18:28 -07:00
|
|
|
import capnp
|
2013-12-09 17:13:43 -08:00
|
|
|
import test_capability_capnp as capability
|
2013-10-15 22:36:14 -07:00
|
|
|
|
2019-12-11 22:44:44 -08:00
|
|
|
|
2023-10-03 18:04:51 +02:00
|
|
|
@pytest.fixture(autouse=True)
|
|
|
|
async def kj_loop():
|
|
|
|
async with capnp.kj_loop():
|
|
|
|
yield
|
|
|
|
|
|
|
|
|
2013-12-09 17:13:43 -08:00
|
|
|
class Server(capability.TestInterface.Server):
|
2013-10-17 22:42:14 -07:00
|
|
|
def __init__(self, val=1):
|
|
|
|
self.val = val
|
|
|
|
|
2023-06-08 03:56:57 +02:00
|
|
|
async def foo(self, i, j, **kwargs):
|
2013-11-14 23:06:14 -08:00
|
|
|
extra = 0
|
|
|
|
if j:
|
|
|
|
extra = 1
|
|
|
|
return str(i * 5 + extra + self.val)
|
|
|
|
|
2023-06-08 03:56:57 +02:00
|
|
|
async def buz(self, i, **kwargs):
|
2021-10-01 11:00:22 -07:00
|
|
|
return i.host + "_test"
|
2013-10-17 22:42:14 -07:00
|
|
|
|
2023-06-08 03:56:57 +02:00
|
|
|
async def bam(self, i, **kwargs):
|
2021-10-01 11:00:22 -07:00
|
|
|
return str(i) + "_test", i
|
2013-12-11 01:17:23 -08:00
|
|
|
|
2023-11-07 05:18:29 +01:00
|
|
|
async def bak1(self, **kwargs):
|
|
|
|
return [1, 2, 3, 4, 5]
|
|
|
|
|
|
|
|
async def bak2(self, i, **kwargs):
|
|
|
|
assert i[4] == 5
|
|
|
|
|
2019-12-11 22:44:44 -08:00
|
|
|
|
2013-12-09 17:13:43 -08:00
|
|
|
class PipelineServer(capability.TestPipeline.Server):
|
2023-06-08 03:56:57 +02:00
|
|
|
async def getCap(self, n, inCap, _context, **kwargs):
|
|
|
|
response = await inCap.foo(i=n)
|
|
|
|
_results = _context.results
|
|
|
|
_results.s = response.x + "_foo"
|
|
|
|
_results.outBox.cap = Server(100)
|
2013-10-15 22:36:14 -07:00
|
|
|
|
2019-12-11 22:44:44 -08:00
|
|
|
|
2023-06-08 02:27:38 +02:00
|
|
|
async def test_client():
|
2013-12-02 17:38:32 -08:00
|
|
|
client = capability.TestInterface._new_client(Server())
|
2014-11-30 14:27:10 -08:00
|
|
|
|
2021-10-01 11:00:22 -07:00
|
|
|
req = client._request("foo")
|
2013-10-15 22:36:14 -07:00
|
|
|
req.i = 5
|
|
|
|
|
|
|
|
remote = req.send()
|
2023-06-08 03:56:57 +02:00
|
|
|
response = await remote
|
2013-10-15 22:36:14 -07:00
|
|
|
|
2021-10-01 11:00:22 -07:00
|
|
|
assert response.x == "26"
|
2014-11-30 14:27:10 -08:00
|
|
|
|
2013-10-17 19:15:40 -07:00
|
|
|
req = client.foo_request()
|
|
|
|
req.i = 5
|
|
|
|
|
|
|
|
remote = req.send()
|
2023-06-08 03:56:57 +02:00
|
|
|
response = await remote
|
2013-10-17 19:15:40 -07:00
|
|
|
|
2021-10-01 11:00:22 -07:00
|
|
|
assert response.x == "26"
|
2013-10-17 19:15:40 -07:00
|
|
|
|
2014-04-17 20:53:52 -07:00
|
|
|
with pytest.raises(AttributeError):
|
2013-10-17 19:15:40 -07:00
|
|
|
client.foo2_request()
|
|
|
|
|
|
|
|
req = client.foo_request()
|
|
|
|
|
2014-11-30 14:27:10 -08:00
|
|
|
with pytest.raises(Exception):
|
2021-10-01 11:00:22 -07:00
|
|
|
req.i = "foo"
|
2013-10-17 19:15:40 -07:00
|
|
|
|
|
|
|
req = client.foo_request()
|
|
|
|
|
2014-01-14 09:44:21 -08:00
|
|
|
with pytest.raises(AttributeError):
|
2013-10-17 19:15:40 -07:00
|
|
|
req.baz = 1
|
|
|
|
|
2023-11-07 05:18:29 +01:00
|
|
|
resp = await client.bak1()
|
|
|
|
# Used to fail with
|
|
|
|
# capnp.lib.capnp.KjException: Tried to set field: 'i' with a value of: '[1, 2, 3, 4, 5]'
|
|
|
|
# which is an unsupported type: '<class 'capnp.lib.capnp._DynamicListReader'>'
|
|
|
|
await client.bak2(resp.i)
|
|
|
|
|
2019-12-11 22:44:44 -08:00
|
|
|
|
2023-06-08 02:27:38 +02:00
|
|
|
async def test_simple_client():
|
2013-12-02 17:38:32 -08:00
|
|
|
client = capability.TestInterface._new_client(Server())
|
2014-11-30 14:27:10 -08:00
|
|
|
|
2021-10-01 11:00:22 -07:00
|
|
|
remote = client._send("foo", i=5)
|
2023-06-08 03:56:57 +02:00
|
|
|
response = await remote
|
2013-10-17 19:15:40 -07:00
|
|
|
|
2021-10-01 11:00:22 -07:00
|
|
|
assert response.x == "26"
|
2013-10-17 19:15:40 -07:00
|
|
|
|
|
|
|
remote = client.foo(i=5)
|
2023-06-08 03:56:57 +02:00
|
|
|
response = await remote
|
2013-10-17 19:15:40 -07:00
|
|
|
|
2021-10-01 11:00:22 -07:00
|
|
|
assert response.x == "26"
|
2014-11-30 14:27:10 -08:00
|
|
|
|
2013-11-14 23:06:14 -08:00
|
|
|
remote = client.foo(i=5, j=True)
|
2023-06-08 03:56:57 +02:00
|
|
|
response = await remote
|
2013-11-14 23:06:14 -08:00
|
|
|
|
2021-10-01 11:00:22 -07:00
|
|
|
assert response.x == "27"
|
2013-10-17 19:15:40 -07:00
|
|
|
|
2013-11-14 20:59:21 -08:00
|
|
|
remote = client.foo(5)
|
2023-06-08 03:56:57 +02:00
|
|
|
response = await remote
|
2013-11-14 20:59:21 -08:00
|
|
|
|
2021-10-01 11:00:22 -07:00
|
|
|
assert response.x == "26"
|
2013-11-14 20:59:21 -08:00
|
|
|
|
2013-11-14 23:06:14 -08:00
|
|
|
remote = client.foo(5, True)
|
2023-06-08 03:56:57 +02:00
|
|
|
response = await remote
|
2013-11-14 23:06:14 -08:00
|
|
|
|
2021-10-01 11:00:22 -07:00
|
|
|
assert response.x == "27"
|
2013-11-14 23:06:14 -08:00
|
|
|
|
|
|
|
remote = client.foo(5, j=True)
|
2023-06-08 03:56:57 +02:00
|
|
|
response = await remote
|
2013-11-14 23:06:14 -08:00
|
|
|
|
2021-10-01 11:00:22 -07:00
|
|
|
assert response.x == "27"
|
2013-11-14 23:06:14 -08:00
|
|
|
|
2021-10-01 11:00:22 -07:00
|
|
|
remote = client.buz(capability.TestSturdyRefHostId.new_message(host="localhost"))
|
2023-06-08 03:56:57 +02:00
|
|
|
response = await remote
|
2013-11-14 23:06:14 -08:00
|
|
|
|
2021-10-01 11:00:22 -07:00
|
|
|
assert response.x == "localhost_test"
|
2013-11-14 23:06:14 -08:00
|
|
|
|
2013-12-11 01:17:23 -08:00
|
|
|
remote = client.bam(i=5)
|
2023-06-08 03:56:57 +02:00
|
|
|
response = await remote
|
2013-12-11 01:17:23 -08:00
|
|
|
|
2021-10-01 11:00:22 -07:00
|
|
|
assert response.x == "5_test"
|
2013-12-11 01:17:23 -08:00
|
|
|
assert response.i == 5
|
|
|
|
|
2014-11-30 14:27:10 -08:00
|
|
|
with pytest.raises(Exception):
|
2013-11-14 23:06:14 -08:00
|
|
|
remote = client.foo(5, 10)
|
|
|
|
|
2014-11-30 14:27:10 -08:00
|
|
|
with pytest.raises(Exception):
|
2013-11-14 23:06:14 -08:00
|
|
|
remote = client.foo(5, True, 100)
|
|
|
|
|
2014-11-30 14:27:10 -08:00
|
|
|
with pytest.raises(Exception):
|
2021-10-01 11:00:22 -07:00
|
|
|
remote = client.foo(i="foo")
|
2013-10-17 19:15:40 -07:00
|
|
|
|
2014-04-17 20:53:52 -07:00
|
|
|
with pytest.raises(AttributeError):
|
2013-10-17 19:15:40 -07:00
|
|
|
remote = client.foo2(i=5)
|
2013-10-15 22:36:14 -07:00
|
|
|
|
2014-11-30 14:27:10 -08:00
|
|
|
with pytest.raises(Exception):
|
2013-10-17 19:15:40 -07:00
|
|
|
remote = client.foo(baz=5)
|
2013-10-17 22:42:14 -07:00
|
|
|
|
2019-12-11 22:44:44 -08:00
|
|
|
|
2023-06-08 02:27:38 +02:00
|
|
|
async def test_pipeline():
|
2013-12-02 17:38:32 -08:00
|
|
|
client = capability.TestPipeline._new_client(PipelineServer())
|
|
|
|
foo_client = capability.TestInterface._new_client(Server())
|
2013-10-17 22:42:14 -07:00
|
|
|
|
|
|
|
remote = client.getCap(n=5, inCap=foo_client)
|
|
|
|
|
2013-10-19 22:38:10 -07:00
|
|
|
outCap = remote.outBox.cap
|
|
|
|
pipelinePromise = outCap.foo(i=10)
|
|
|
|
|
2023-06-08 03:56:57 +02:00
|
|
|
response = await pipelinePromise
|
2021-10-01 11:00:22 -07:00
|
|
|
assert response.x == "150"
|
2013-10-19 22:38:10 -07:00
|
|
|
|
2023-06-08 03:56:57 +02:00
|
|
|
response = await remote
|
2021-10-01 11:00:22 -07:00
|
|
|
assert response.s == "26_foo"
|
2013-10-20 17:24:59 -07:00
|
|
|
|
2019-12-11 22:44:44 -08:00
|
|
|
|
2013-12-09 17:13:43 -08:00
|
|
|
class BadServer(capability.TestInterface.Server):
|
2013-10-20 17:24:59 -07:00
|
|
|
def __init__(self, val=1):
|
|
|
|
self.val = val
|
|
|
|
|
2023-06-08 03:56:57 +02:00
|
|
|
async def foo(self, i, j, **kwargs):
|
2013-11-14 23:06:14 -08:00
|
|
|
extra = 0
|
|
|
|
if j:
|
|
|
|
extra = 1
|
2019-12-11 22:44:44 -08:00
|
|
|
return str(i * 5 + extra + self.val), 10 # returning too many args
|
|
|
|
|
2013-10-20 17:24:59 -07:00
|
|
|
|
2023-06-08 02:27:38 +02:00
|
|
|
async def test_exception_client():
|
2013-12-02 17:38:32 -08:00
|
|
|
client = capability.TestInterface._new_client(BadServer())
|
2014-11-30 14:27:10 -08:00
|
|
|
|
2021-10-01 11:00:22 -07:00
|
|
|
remote = client._send("foo", i=5)
|
2013-11-13 20:54:57 -08:00
|
|
|
with pytest.raises(capnp.KjException):
|
2023-06-08 03:56:57 +02:00
|
|
|
await remote
|
2013-10-20 17:24:59 -07:00
|
|
|
|
2019-12-11 22:44:44 -08:00
|
|
|
|
2013-12-09 17:13:43 -08:00
|
|
|
class BadPipelineServer(capability.TestPipeline.Server):
|
2023-06-08 03:56:57 +02:00
|
|
|
async def getCap(self, n, inCap, _context, **kwargs):
|
|
|
|
try:
|
|
|
|
await inCap.foo(i=n)
|
|
|
|
except capnp.KjException:
|
2021-10-01 11:00:22 -07:00
|
|
|
raise Exception("test was a success")
|
2013-10-20 17:24:59 -07:00
|
|
|
|
2019-12-11 22:44:44 -08:00
|
|
|
|
2023-06-08 02:27:38 +02:00
|
|
|
async def test_exception_chain():
|
2013-12-02 17:38:32 -08:00
|
|
|
client = capability.TestPipeline._new_client(BadPipelineServer())
|
|
|
|
foo_client = capability.TestInterface._new_client(BadServer())
|
2013-10-20 17:24:59 -07:00
|
|
|
|
|
|
|
remote = client.getCap(n=5, inCap=foo_client)
|
|
|
|
|
|
|
|
try:
|
2023-06-08 03:56:57 +02:00
|
|
|
await remote
|
2013-10-20 17:24:59 -07:00
|
|
|
except Exception as e:
|
2021-10-01 11:00:22 -07:00
|
|
|
assert "test was a success" in str(e)
|
2013-10-20 17:24:59 -07:00
|
|
|
|
2019-12-11 22:44:44 -08:00
|
|
|
|
2023-06-08 02:27:38 +02:00
|
|
|
async def test_pipeline_exception():
|
2013-12-02 17:38:32 -08:00
|
|
|
client = capability.TestPipeline._new_client(BadPipelineServer())
|
|
|
|
foo_client = capability.TestInterface._new_client(BadServer())
|
2013-10-20 17:24:59 -07:00
|
|
|
|
|
|
|
remote = client.getCap(n=5, inCap=foo_client)
|
|
|
|
|
|
|
|
outCap = remote.outBox.cap
|
|
|
|
pipelinePromise = outCap.foo(i=10)
|
|
|
|
|
|
|
|
with pytest.raises(Exception):
|
2023-06-08 03:56:57 +02:00
|
|
|
await pipelinePromise
|
2013-10-20 17:24:59 -07:00
|
|
|
|
|
|
|
with pytest.raises(Exception):
|
2023-06-08 03:56:57 +02:00
|
|
|
await remote
|
2013-11-12 20:28:23 -08:00
|
|
|
|
2019-12-11 22:44:44 -08:00
|
|
|
|
2023-06-08 02:27:38 +02:00
|
|
|
async def test_casting():
|
2013-12-02 17:38:32 -08:00
|
|
|
client = capability.TestExtends._new_client(Server())
|
2013-11-12 20:28:23 -08:00
|
|
|
client2 = client.upcast(capability.TestInterface)
|
2019-09-26 22:18:28 -07:00
|
|
|
_ = client2.cast_as(capability.TestInterface)
|
2013-11-12 20:28:23 -08:00
|
|
|
|
|
|
|
with pytest.raises(Exception):
|
|
|
|
client.upcast(capability.TestPipeline)
|
2013-12-05 00:07:51 -08:00
|
|
|
|
2019-12-11 22:44:44 -08:00
|
|
|
|
2013-12-09 17:13:43 -08:00
|
|
|
class TailCallOrder(capability.TestCallOrder.Server):
|
2013-12-05 00:07:51 -08:00
|
|
|
def __init__(self):
|
|
|
|
self.count = -1
|
|
|
|
|
2023-06-08 03:56:57 +02:00
|
|
|
async def getCallSequence(self, expected, **kwargs):
|
2013-12-05 00:07:51 -08:00
|
|
|
self.count += 1
|
|
|
|
return self.count
|
|
|
|
|
2019-12-11 22:44:44 -08:00
|
|
|
|
2013-12-09 17:13:43 -08:00
|
|
|
class TailCaller(capability.TestTailCaller.Server):
|
2013-12-05 00:07:51 -08:00
|
|
|
def __init__(self):
|
|
|
|
self.count = 0
|
|
|
|
|
2023-06-08 03:56:57 +02:00
|
|
|
async def foo(self, i, callee, _context, **kwargs):
|
2013-12-05 00:07:51 -08:00
|
|
|
self.count += 1
|
|
|
|
|
2021-10-01 11:00:22 -07:00
|
|
|
tail = callee.foo_request(i=i, t="from TailCaller")
|
2023-06-08 03:56:57 +02:00
|
|
|
return await _context.tail_call(tail)
|
2013-12-05 00:07:51 -08:00
|
|
|
|
2019-12-11 22:44:44 -08:00
|
|
|
|
2013-12-09 17:13:43 -08:00
|
|
|
class TailCallee(capability.TestTailCallee.Server):
|
2013-12-05 00:07:51 -08:00
|
|
|
def __init__(self):
|
|
|
|
self.count = 0
|
|
|
|
|
2023-06-08 03:56:57 +02:00
|
|
|
async def foo(self, i, t, _context, **kwargs):
|
2013-12-05 00:07:51 -08:00
|
|
|
self.count += 1
|
|
|
|
|
|
|
|
results = _context.results
|
|
|
|
results.i = i
|
|
|
|
results.t = t
|
2013-12-09 17:13:43 -08:00
|
|
|
results.c = TailCallOrder()
|
2013-12-05 00:07:51 -08:00
|
|
|
|
2019-12-11 22:44:44 -08:00
|
|
|
|
2023-06-08 02:27:38 +02:00
|
|
|
async def test_tail_call():
|
2013-12-05 00:07:51 -08:00
|
|
|
callee_server = TailCallee()
|
|
|
|
caller_server = TailCaller()
|
|
|
|
|
|
|
|
callee = capability.TestTailCallee._new_client(callee_server)
|
|
|
|
caller = capability.TestTailCaller._new_client(caller_server)
|
|
|
|
|
|
|
|
promise = caller.foo(i=456, callee=callee)
|
|
|
|
dependent_call1 = promise.c.getCallSequence()
|
|
|
|
|
2023-06-08 03:56:57 +02:00
|
|
|
response = await promise
|
2013-12-05 00:07:51 -08:00
|
|
|
|
|
|
|
assert response.i == 456
|
|
|
|
assert response.i == 456
|
|
|
|
|
|
|
|
dependent_call2 = response.c.getCallSequence()
|
|
|
|
dependent_call3 = response.c.getCallSequence()
|
|
|
|
|
2023-06-08 03:56:57 +02:00
|
|
|
result = await dependent_call1
|
2013-12-05 00:07:51 -08:00
|
|
|
assert result.n == 0
|
2023-06-08 03:56:57 +02:00
|
|
|
result = await dependent_call2
|
2013-12-05 00:07:51 -08:00
|
|
|
assert result.n == 1
|
2023-06-08 03:56:57 +02:00
|
|
|
result = await dependent_call3
|
2013-12-05 00:07:51 -08:00
|
|
|
assert result.n == 2
|
|
|
|
|
|
|
|
assert callee_server.count == 1
|
2014-04-10 19:08:24 -07:00
|
|
|
assert caller_server.count == 1
|
|
|
|
|
|
|
|
|
2023-06-08 02:27:38 +02:00
|
|
|
async def test_cancel():
|
2014-04-13 18:15:36 -07:00
|
|
|
client = capability.TestInterface._new_client(Server())
|
|
|
|
|
2021-10-01 11:00:22 -07:00
|
|
|
req = client._request("foo")
|
2014-04-13 18:15:36 -07:00
|
|
|
req.i = 5
|
|
|
|
|
|
|
|
remote = req.send()
|
|
|
|
remote.cancel()
|
|
|
|
|
2014-11-30 14:27:10 -08:00
|
|
|
with pytest.raises(Exception):
|
2023-06-08 03:56:57 +02:00
|
|
|
await remote
|
2014-04-13 18:15:36 -07:00
|
|
|
|
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 20:08:15 +02:00
|
|
|
req = client.foo(5)
|
2023-06-08 03:56:57 +02:00
|
|
|
await req
|
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 20:08:15 +02:00
|
|
|
req.cancel() # Cancel a promise that was already consumed
|
|
|
|
|
|
|
|
req = client.foo(5)
|
|
|
|
req.cancel()
|
|
|
|
with pytest.raises(Exception):
|
2023-06-08 03:56:57 +02:00
|
|
|
await req
|
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 20:08:15 +02:00
|
|
|
|
|
|
|
req = client.foo(5)
|
2023-06-08 03:56:57 +02:00
|
|
|
assert (await req).x == "26"
|
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 20:08:15 +02:00
|
|
|
with pytest.raises(Exception):
|
2023-06-08 03:56:57 +02:00
|
|
|
await req
|
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 20:08:15 +02:00
|
|
|
|
2014-04-13 18:15:36 -07:00
|
|
|
|
2023-06-08 02:27:38 +02:00
|
|
|
async def test_double_send():
|
2014-04-13 18:35:53 -07:00
|
|
|
client = capability.TestInterface._new_client(Server())
|
|
|
|
|
2021-10-01 11:00:22 -07:00
|
|
|
req = client._request("foo")
|
2014-04-13 18:35:53 -07:00
|
|
|
req.i = 5
|
|
|
|
|
2023-06-08 03:56:57 +02:00
|
|
|
await req.send()
|
2014-11-30 14:27:10 -08:00
|
|
|
with pytest.raises(Exception):
|
2023-06-08 03:56:57 +02:00
|
|
|
await req.send()
|
2014-04-18 18:18:54 -07:00
|
|
|
|
|
|
|
|
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 20:08:15 +02:00
|
|
|
class PromiseJoinServer(capability.TestPipeline.Server):
|
2023-06-08 03:56:57 +02:00
|
|
|
async def getCap(self, n, inCap, _context, **kwargs):
|
|
|
|
res = await inCap.foo(i=n)
|
2023-06-08 08:18:50 +02:00
|
|
|
response = await inCap.foo(i=int(res.x) + 1)
|
2023-06-08 03:56:57 +02:00
|
|
|
_results = _context.results
|
|
|
|
_results.s = response.x + "_bar"
|
|
|
|
_results.outBox.cap = inCap
|
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 20:08:15 +02:00
|
|
|
|
|
|
|
|
2023-06-08 02:27:38 +02:00
|
|
|
async def test_promise_joining():
|
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 20:08:15 +02:00
|
|
|
client = capability.TestPipeline._new_client(PromiseJoinServer())
|
|
|
|
foo_client = capability.TestInterface._new_client(Server())
|
|
|
|
|
|
|
|
remote = client.getCap(n=5, inCap=foo_client)
|
2023-06-08 03:56:57 +02:00
|
|
|
assert (await remote).s == "136_bar"
|
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 20:08:15 +02:00
|
|
|
|
|
|
|
|
2014-04-18 18:18:54 -07:00
|
|
|
class ExtendsServer(Server):
|
2023-06-08 03:56:57 +02:00
|
|
|
async def qux(self, **kwargs):
|
2014-04-18 18:18:54 -07:00
|
|
|
pass
|
|
|
|
|
|
|
|
|
2023-06-08 02:27:38 +02:00
|
|
|
async def test_inheritance():
|
2014-04-18 18:18:54 -07:00
|
|
|
client = capability.TestExtends._new_client(ExtendsServer())
|
2023-06-08 03:56:57 +02:00
|
|
|
await client.qux()
|
2014-04-18 18:18:54 -07:00
|
|
|
|
|
|
|
remote = client.foo(i=5)
|
2023-06-08 03:56:57 +02:00
|
|
|
response = await remote
|
2014-04-18 18:18:54 -07:00
|
|
|
|
2021-10-01 11:00:22 -07:00
|
|
|
assert response.x == "26"
|
2015-06-03 14:35:39 -07:00
|
|
|
|
|
|
|
|
2019-07-13 16:36:58 +01:00
|
|
|
class PassedCapTest(capability.TestPassedCap.Server):
|
2023-06-08 03:56:57 +02:00
|
|
|
async def foo(self, cap, _context, **kwargs):
|
|
|
|
res = await cap.foo(5)
|
|
|
|
_context.results.x = res.x
|
2015-06-03 14:35:39 -07:00
|
|
|
|
|
|
|
|
2023-06-08 02:27:38 +02:00
|
|
|
async def test_null_cap():
|
2019-07-13 16:36:58 +01:00
|
|
|
client = capability.TestPassedCap._new_client(PassedCapTest())
|
2023-06-08 03:56:57 +02:00
|
|
|
assert (await client.foo(Server())).x == "26"
|
2015-06-03 14:35:39 -07:00
|
|
|
|
|
|
|
with pytest.raises(capnp.KjException):
|
2023-06-08 03:56:57 +02:00
|
|
|
await client.foo()
|
2015-06-08 13:39:10 -07:00
|
|
|
|
|
|
|
|
2019-07-13 16:36:58 +01:00
|
|
|
class StructArgTest(capability.TestStructArg.Server):
|
2023-06-08 03:56:57 +02:00
|
|
|
async def bar(self, a, b, **kwargs):
|
2015-06-08 13:39:10 -07:00
|
|
|
return a + str(b)
|
|
|
|
|
|
|
|
|
2023-06-08 02:27:38 +02:00
|
|
|
async def test_struct_args():
|
2019-07-13 16:36:58 +01:00
|
|
|
client = capability.TestStructArg._new_client(StructArgTest())
|
2023-06-08 03:56:57 +02:00
|
|
|
assert (await client.bar(a="test", b=1)).c == "test1"
|
2015-06-08 13:39:10 -07:00
|
|
|
with pytest.raises(capnp.KjException):
|
2023-06-08 03:56:57 +02:00
|
|
|
assert (await client.bar("test", 1)).c == "test1"
|
2015-06-08 14:59:21 -07:00
|
|
|
|
|
|
|
|
2019-07-13 16:36:58 +01:00
|
|
|
class GenericTest(capability.TestGeneric.Server):
|
2023-06-08 03:56:57 +02:00
|
|
|
async def foo(self, a, **kwargs):
|
2021-10-01 11:00:22 -07:00
|
|
|
return a.as_text() + "test"
|
2015-06-08 14:59:21 -07:00
|
|
|
|
|
|
|
|
2023-06-08 02:27:38 +02:00
|
|
|
async def test_generic():
|
2019-07-13 16:36:58 +01:00
|
|
|
client = capability.TestGeneric._new_client(GenericTest())
|
2015-06-08 14:59:21 -07:00
|
|
|
|
|
|
|
obj = capnp._MallocMessageBuilder().get_root_as_any()
|
|
|
|
obj.set_as_text("anypointer_")
|
2023-06-08 03:56:57 +02:00
|
|
|
assert (await client.foo(obj)).b == "anypointer_test"
|
2023-10-28 07:03:42 +02:00
|
|
|
|
|
|
|
|
|
|
|
class CancelServer(capability.TestInterface.Server):
|
|
|
|
def __init__(self, val=1):
|
|
|
|
self.val = val
|
|
|
|
|
|
|
|
async def foo(self, i, j, **kwargs):
|
|
|
|
with pytest.raises(asyncio.CancelledError):
|
|
|
|
await asyncio.sleep(10)
|
|
|
|
|
|
|
|
|
|
|
|
async def test_cancel2():
|
|
|
|
client = capability.TestInterface._new_client(CancelServer())
|
|
|
|
|
|
|
|
task = asyncio.ensure_future(client.foo(1, True))
|
|
|
|
await asyncio.sleep(0) # Make sure that the task runs
|
|
|
|
task.cancel()
|
|
|
|
with pytest.raises(asyncio.CancelledError):
|
|
|
|
await task
|