From 00d9ddf57b814e82257cbdbb60fe8e0b9d2886a5 Mon Sep 17 00:00:00 2001 From: Claude Date: Sat, 21 Nov 2026 10:35:41 +0000 Subject: [PATCH 1/2] Unregister channels before sending close; unwedge main_thread_only after errors Channel.close() sent CHANNEL_CLOSE before removing the id from the channel factory, so a peer could return from waitclose() and run its next remote_exec while the old id was still registered. On a single CPU test_channel_passing_over_channel hit this almost every run (#374). With main_thread_only, executetask() returned early on an exception and never set _executetask_complete, so every later remote_exec on that worker was rejected as concurrent. Set it in a finally. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_014GZsREmCGLBsihTm2x9zm3 --- news/374.bugfix.rst | 1 + src/execnet/gateway_base.py | 38 +++++++++++++++++++++++++------------ 2 files changed, 27 insertions(+), 12 deletions(-) create mode 100644 news/374.bugfix.rst diff --git a/news/374.bugfix.rst b/news/374.bugfix.rst new file mode 100644 index 00000000..c4f9f0fd --- /dev/null +++ b/news/374.bugfix.rst @@ -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. diff --git a/src/execnet/gateway_base.py b/src/execnet/gateway_base.py index 5048c45e..8985192d 100644 --- a/src/execnet/gateway_base.py +++ b/src/execnet/gateway_base.py @@ -858,13 +858,7 @@ 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" @@ -872,7 +866,16 @@ def close(self, error=None) -> None: 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 @@ -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 @@ -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 @@ -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() # From 2e5a8911ca0199bc44afa30c0f2f9e28b1464bac Mon Sep 17 00:00:00 2001 From: Claude Date: Sat, 21 Nov 2026 10:35:41 +0000 Subject: [PATCH 2/2] Key the gw test fixture by execmodel The session group cached gateways by transport only, so e.g. main_thread_only-popen reused whichever popen gateway was created first, usually a thread worker, and main_thread_only was barely exercised. Include the execmodel in the gateway id, reuse the shared sproxy1 and proxy-transport gateways, and skip test_gateway_status_busy for main_thread_only, which rejects concurrent remote_exec by design. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_014GZsREmCGLBsihTm2x9zm3 --- testing/conftest.py | 20 +++++++++++++------- testing/test_gateway.py | 2 ++ 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/testing/conftest.py b/testing/conftest.py index c75f96c7..88e7694b 100644 --- a/testing/conftest.py +++ b/testing/conftest.py @@ -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] @@ -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}" diff --git a/testing/test_gateway.py b/testing/test_gateway.py index 634f237a..732fd0e2 100644 --- a/testing/test_gateway.py +++ b/testing/test_gateway.py @@ -78,6 +78,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()")