Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions news/374.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
``Channel.close()`` now unregisters the channel id before sending the close message, so the peer can no longer observe a closed channel whose id is still registered on the closing side. With the ``main_thread_only`` execmodel, a ``remote_exec`` task that raised no longer causes every later ``remote_exec`` on that worker to be rejected as concurrent.
38 changes: 26 additions & 12 deletions src/execnet/gateway_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -858,21 +858,24 @@ def close(self, error=None) -> None:
# but it's never damaging to send too many CHANNEL_CLOSE messages
# however, if the other side triggered a close already, we
# do not send back a closed message.
if not self._receiveclosed.is_set():
put = self.gateway._send
if error is not None:
put(Message.CHANNEL_CLOSE_ERROR, self.id, dumps_internal(error))
else:
put(Message.CHANNEL_CLOSE, self.id)
self._trace("sent channel close message")
send_close = not self._receiveclosed.is_set()
if isinstance(error, RemoteError):
self._remoteerrors.append(error)
self._closed = True # --> "closed"
self._receiveclosed.set()
queue = self._items
if queue is not None:
queue.put(ENDMARKER)
# Unregister before telling the other side, so that once the peer
# sees the close, this side no longer knows the channel id (#374).
self.gateway._channelfactory._no_longer_opened(self.id)
if send_close:
put = self.gateway._send
if error is not None:
put(Message.CHANNEL_CLOSE_ERROR, self.id, dumps_internal(error))
else:
put(Message.CHANNEL_CLOSE, self.id)
self._trace("sent channel close message")

def waitclose(self, timeout: float | None = None) -> None:
"""Wait until this channel is closed (or the remote side
Expand Down Expand Up @@ -1210,6 +1213,8 @@ def join(self, timeout: float | None = None) -> None:


class WorkerGateway(BaseGateway):
_executetask_complete: Event | None = None

def _local_schedulexec(self, channel: Channel, sourcetask: bytes) -> None:
if self._execpool.execmodel.backend == "main_thread_only":
assert self._executetask_complete is not None
Expand Down Expand Up @@ -1277,6 +1282,20 @@ def trace(msg: str) -> None:
def executetask(
self,
item: tuple[Channel, tuple[str, str | None, str | None, dict[str, object]]],
) -> None:
try:
self._executetask(item)
finally:
if self._executetask_complete is not None:
# Indicate that this task has finished executing, meaning
# that there is no possibility of it triggering a deadlock
# for the next spawn call. This must also happen when the
# task failed, or the worker rejects every later task.
self._executetask_complete.set()

def _executetask(
self,
item: tuple[Channel, tuple[str, str | None, str | None, dict[str, object]]],
) -> None:
try:
channel, (source, file_name, call_name, kwargs) = item
Expand Down Expand Up @@ -1306,11 +1325,6 @@ def executetask(
channel.close(errortext)
return
channel.close()
if self._executetask_complete is not None:
# Indicate that this task has finished executing, meaning
# that there is no possibility of it triggering a deadlock
# for the next spawn call.
self._executetask_complete.set()


#
Expand Down
20 changes: 13 additions & 7 deletions testing/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,21 +148,26 @@ def gw(
execmodel: ExecModel,
group: execnet.Group,
) -> Gateway:
# Cache per transport *and* execmodel: the remote execmodel is fixed when
# the gateway is created, so sharing one gateway across execmodels would
# run e.g. main_thread_only tests on a thread worker.
gwid = f"{request.param}-{execmodel.backend}"
try:
return group[request.param]
return group[gwid]
except KeyError:
if request.param == "popen":
gw = group.makegateway("popen//id=popen//execmodel=%s" % execmodel.backend)
gw = group.makegateway(f"popen//id={gwid}//execmodel={execmodel.backend}")
elif request.param == "socket":
# if execmodel.backend != "thread":
# pytest.xfail(
# "cannot set remote non-thread execmodel for sockets")
pname = "sproxy1"
if pname not in group:
proxygw = group.makegateway("popen//id=%s" % pname)
group.makegateway("popen//id=%s" % pname)
proxygw = group[pname]
# assert group['proxygw'].remote_status().receiving
gw = group.makegateway(
f"socket//id=socket//installvia={pname}//execmodel={execmodel.backend}"
f"socket//id={gwid}//installvia={pname}//execmodel={execmodel.backend}"
)
# TODO(typing): Clarify this assignment.
gw.proxygw = proxygw # type: ignore[attr-defined]
Expand All @@ -171,11 +176,12 @@ def gw(
sshhost = request.getfixturevalue("specssh").ssh
# we don't use execmodel.backend here
# but you can set it when specifying the ssh spec
gw = group.makegateway(f"ssh={sshhost}//id=ssh")
gw = group.makegateway(f"ssh={sshhost}//id={gwid}")
elif request.param == "proxy":
group.makegateway("popen//id=proxy-transport")
if "proxy-transport" not in group:
group.makegateway("popen//id=proxy-transport")
gw = group.makegateway(
"popen//via=proxy-transport//id=proxy//execmodel=%s" % execmodel.backend
f"popen//via=proxy-transport//id={gwid}//execmodel={execmodel.backend}"
)
else:
assert 0, f"unknown execmodel: {request.param}"
Expand Down
2 changes: 2 additions & 0 deletions testing/test_gateway.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ def test_gateway_status_no_real_channel(self, gw: Gateway) -> None:

@flakytest
def test_gateway_status_busy(self, gw: Gateway) -> None:
if gw.spec.execmodel == "main_thread_only":
pytest.skip("main_thread_only rejects concurrent remote_exec")
numchannels = gw.remote_status().numchannels
ch1 = gw.remote_exec("channel.send(1); channel.receive()")
ch2 = gw.remote_exec("channel.receive()")
Expand Down