Skip to content

Commit

Permalink
server: fix bug where python async generator aclose is not called on …
Browse files Browse the repository at this point in the history
…rpc objects
  • Loading branch information
koush committed Mar 22, 2023
1 parent b119e5e commit 59008fb
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 11 deletions.
4 changes: 2 additions & 2 deletions server/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 7 additions & 3 deletions server/python/rpc-iterator-test.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import asyncio
import rpc
from rpc_reader import prepare_peer_readloop
from rpc_reader import prepare_peer_readloop, RpcFileTransport
import traceback

class Bar:
pass

async def main():
peer, peerReadLoop = await prepare_peer_readloop(loop, 4, 3)
peer, peerReadLoop = await prepare_peer_readloop(loop, RpcFileTransport(4, 3))
peer.params['foo'] = 3
jsoncopy = {}
jsoncopy[rpc.RpcPeer.PROPERTY_JSON_COPY_SERIALIZE_CHILDREN] = True
Expand Down Expand Up @@ -35,8 +35,12 @@ async def main():
test = await peer.getParam('test')
print(test)
try:
async for c in test:
i = 0
async for c in await test():
print(c)
if i == 5:
break
i = i + 1
except:
traceback.print_exc()
print('all done iterating')
Expand Down
8 changes: 8 additions & 0 deletions server/python/rpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,14 @@ async def __anext__(self):
raise
raise Exception('RpcProxy is not an async iterable')

async def aclose(self):
if self.__dict__[RpcPeer.PROPERTY_PROXY_PROPERTIES] and 'Symbol(Symbol.asyncIterator)' in self.__dict__[RpcPeer.PROPERTY_PROXY_PROPERTIES]:
try:
return await RpcProxyMethod(self, self.__dict__[RpcPeer.PROPERTY_PROXY_PROPERTIES]['Symbol(Symbol.asyncIterator)']['return'])()
except RPCResultError as e:
pass
raise Exception('RpcProxy is not an async iterable')

def __getattr__(self, name):
if name == '__proxy_finalizer_id':
return self.dict['__proxy_entry']['finalizerId']
Expand Down
16 changes: 10 additions & 6 deletions server/test/rpc-python-test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import child_process from 'child_process';
import net from 'net';
import path from 'path';
import type { Readable, Writable } from "stream";
import { createDuplexRpcPeer } from '../src/rpc-serializer';
import assert from 'assert';
import net from 'net';

async function main() {
const server = net.createServer(client => {
Expand All @@ -21,12 +20,17 @@ async function main() {
const rpcPeer = createDuplexRpcPeer('node', 'python', cp.stdio[3] as Readable, cp.stdio[4] as Writable);

async function* test() {
yield 1;
yield 2;
yield 3;
try {
for (let i = 0; ; i++) {
yield i;
}
}
finally {
console.log('closed');
}
}

rpcPeer.params['test'] = test();
rpcPeer.params['test'] = test;

// const foo = await rpcPeer.getParam('foo');
// assert.equal(foo, 3);
Expand Down

0 comments on commit 59008fb

Please sign in to comment.