Skip to content

Commit

Permalink
Merge pull request #264 from goodboy/runinactor_none_result
Browse files Browse the repository at this point in the history
Fix `Portal.run_in_actor()` returns `None` result
  • Loading branch information
goodboy authored Nov 29, 2021
2 parents b527fdb + f6de7e0 commit ac821bd
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 37 deletions.
8 changes: 8 additions & 0 deletions newsfragments/264.bug.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Fix ``Portal.run_in_actor()`` returns ``None`` result.

``None`` was being used as the cached result flag and obviously breaks
on a ``None`` returned from the remote target task. This would cause an
infinite hang if user code ever called ``Portal.result()`` *before* the
nursery exit. The simple fix is to use the *return message* as the
initial "no-result-received-yet" flag value and, once received, the
return value is read from the message to avoid the cache logic error.
36 changes: 25 additions & 11 deletions tests/test_spawning.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""
Spawning basics
"""
from typing import Dict, Tuple
from typing import Dict, Tuple, Optional

import pytest
import trio
Expand Down Expand Up @@ -93,24 +93,38 @@ async def test_movie_theatre_convo(start_method):
await portal.cancel_actor()


async def cellar_door():
return "Dang that's beautiful"
async def cellar_door(return_value: Optional[str]):
return return_value


@pytest.mark.parametrize(
'return_value', ["Dang that's beautiful", None],
ids=['return_str', 'return_None'],
)
@tractor_test
async def test_most_beautiful_word(start_method):
"""The main ``tractor`` routine.
"""
async with tractor.open_nursery() as n:
async def test_most_beautiful_word(
start_method,
return_value
):
'''
The main ``tractor`` routine.
portal = await n.run_in_actor(
cellar_door,
name='some_linguist',
)
'''
with trio.fail_after(1):
async with tractor.open_nursery() as n:

portal = await n.run_in_actor(
cellar_door,
return_value=return_value,
name='some_linguist',
)

print(await portal.result())
# The ``async with`` will unblock here since the 'some_linguist'
# actor has completed its main task ``cellar_door``.

# this should pull the cached final result already captured during
# the nursery block exit.
print(await portal.result())


Expand Down
64 changes: 38 additions & 26 deletions tractor/_portal.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
from ._exceptions import (
unpack_error,
NoResult,
RemoteActorError,
# RemoteActorError,
ContextCancelled,
)
from ._streaming import Context, ReceiveMsgStream
Expand Down Expand Up @@ -54,8 +54,23 @@ def func_deats(func: Callable) -> Tuple[str, str]:
)


def _unwrap_msg(

msg: dict[str, Any],
channel: Channel

) -> Any:
try:
return msg['return']
except KeyError:
# internal error should never get here
assert msg.get('cid'), "Received internal error at portal?"
raise unpack_error(msg, channel)


class Portal:
"""A 'portal' to a(n) (remote) ``Actor``.
'''
A 'portal' to a(n) (remote) ``Actor``.
A portal is "opened" (and eventually closed) by one side of an
inter-actor communication context. The side which opens the portal
Expand All @@ -71,13 +86,14 @@ class Portal:
function calling semantics are supported transparently; hence it is
like having a "portal" between the seperate actor memory spaces.
"""
'''
def __init__(self, channel: Channel) -> None:
self.channel = channel
# when this is set to a tuple returned from ``_submit()`` then
# it is expected that ``result()`` will be awaited at some point
# during the portal's lifetime
self._result: Optional[Any] = None
self._result_msg: Optional[dict] = None

# set when _submit_for_result is called
self._expect_result: Optional[
Tuple[str, Any, str, Dict[str, Any]]
Expand Down Expand Up @@ -128,16 +144,12 @@ async def _return_once(
recv_chan: trio.abc.ReceiveChannel,
resptype: str,
first_msg: dict
) -> Any:
assert resptype == 'asyncfunc' # single response

) -> dict[str, Any]:

assert resptype == 'asyncfunc' # single response
msg = await recv_chan.receive()
try:
return msg['return']
except KeyError:
# internal error should never get here
assert msg.get('cid'), "Received internal error at portal?"
raise unpack_error(msg, self.channel)
return msg

async def result(self) -> Any:
"""Return the result(s) from the remote actor's "main" task.
Expand All @@ -158,17 +170,11 @@ async def result(self) -> Any:

# expecting a "main" result
assert self._expect_result
if self._result is None:
try:
self._result = await self._return_once(*self._expect_result)
except RemoteActorError as err:
self._result = err

# re-raise error on every call
if isinstance(self._result, RemoteActorError):
raise self._result
if self._result_msg is None:
self._result_msg = await self._return_once(*self._expect_result)

return self._result
return _unwrap_msg(self._result_msg, self.channel)

async def _cancel_streams(self):
# terminate all locally running async generator
Expand Down Expand Up @@ -243,22 +249,25 @@ async def run_from_ns(
instance methods in the remote runtime. Currently this should only
be used for `tractor` internals.
"""
return await self._return_once(
msg = await self._return_once(
*(await self._submit(namespace_path, function_name, kwargs))
)
return _unwrap_msg(msg, self.channel)

async def run(
self,
func: str,
fn_name: Optional[str] = None,
**kwargs
) -> Any:
"""Submit a remote function to be scheduled and run by actor, in
'''
Submit a remote function to be scheduled and run by actor, in
a new task, wrap and return its (stream of) result(s).
This is a blocking call and returns either a value from the
remote rpc task or a local async generator instance.
"""
'''
if isinstance(func, str):
warnings.warn(
"`Portal.run(namespace: str, funcname: str)` is now"
Expand All @@ -284,8 +293,11 @@ async def run(

fn_mod_path, fn_name = func_deats(func)

return await self._return_once(
*(await self._submit(fn_mod_path, fn_name, kwargs))
return _unwrap_msg(
await self._return_once(
*(await self._submit(fn_mod_path, fn_name, kwargs)),
),
self.channel,
)

@asynccontextmanager
Expand Down

0 comments on commit ac821bd

Please sign in to comment.