[Bug] stop() never clears _primary_channel, so mgr.channel hands out a stopped channel after a restart
English is recommended so global developers can help. 推荐使用英文提交,谢谢 ❤️
- I'm on the latest version and searched existing issues (incl. closed) — no duplicate.
Environment
Version: current master (commit 08f64f7)
OS: any — platform-independent
Python: 3.11 / 3.13 (CI matrix)
Install: any
What happened?
ChannelManager.stop() never cleared _primary_channel, so after stopping a channel mgr.channel kept returning the stopped instance for the rest of the process. A restart() then made that permanent.
The cause is an ordering slip — the entry is popped from the registry, and the code then asks the registry whether the primary is that channel:
for name in names:
ch = self._channels.pop(name, None) # <- entry is gone from here on
th = self._threads.pop(name, None)
to_stop.append((name, ch, th))
if channel_name and self._primary_channel is self._channels.get(channel_name):
self._primary_channel = None # <- .get() is always None, so never Trueself._channels.get(channel_name) can never be the primary any more, and a primary channel object is never None, so the guard is always False. stop() with no argument (stop all) is guarded by if channel_name and ..., which is False for the same reason.
Why it is not self-healing
start() only adopts a primary when there is none:
if self._primary_channel is None and name != "web":
self._primary_channel = chrestart() is stop() + start(). Since stop() left the stale reference in place, start()'s is None check fails and the new instance is never adopted. So after a single restart the reference is the dead instance permanently, not just until the next start.
Steps to reproduce
mgr = ChannelManager() # with create_channel patched
mgr.start(["qq"]); ...
primary = mgr.channel # the live QqChannel
mgr.stop("qq")
mgr.channel # still the stopped QqChannel (expected None)
mgr.channel.running # False - it is dead but still handed out
mgr.restart("qq")
mgr.channel # still the ORIGINAL instance, not the new one
mgr.channel is primary # True - the restart's instance was discardedImpact
mgr.channel is the accessor the rest of the process uses. app.py:266 passes _primary_channel to a worker thread, and the scheduler resolves its delivery channel through it (agent/tools/scheduler/integration.py). After a restart, a scheduled task or a worker thread can be handed a channel whose connection is closed, so delivery fails with no indication that the manager's idea of the primary channel is stale.
Suggested fix
Compare against the instance that was just removed, inside the loop, and drop the channel_name condition so stop()-all is covered by the same code:
if ch is not None and self._primary_channel is ch:
self._primary_channel = Nonetests/test_channel_double_start.py already provides the FakeChannel + patched create_channel harness for ChannelManager, so a test for this fits alongside it.
Logs
Not applicable — _primary_channel is plain in-process state, reproducible through ChannelManager directly.
Source: zhayujie/CowAgent