fix: reset online client singleton after run finish to avoid stale sid - #1744
Merged
Conversation
Reset the global client in online mode after `Run.finish()` so the next `init()` re-authenticates and obtains a fresh sid. This prevents 401 errors when the server invalidates the sid after experiment completion. Also suppress the welcome message during automatic re-login and clean up test mock helpers, consolidating the login/page mock for multiple init/finish cycles.
SAKURA-CAT
commented
Aug 20, 2026
SAKURA-CAT
left a comment
Member
Author
There was a problem hiding this comment.
审核结论: 通过
本次 PR 解决的问题
修复 #1715:服务端在实验结束后使 sid 失效,同进程二次 init() 复用旧 client 单例导致 401 —— finish() 在 online 模式下销毁全局 client 单例,下次 init 重新认证(临时方案,跟踪 #1742)。
审核范围
- Run 生命周期与运行时:
finish()清理顺序、重复 finish、atexit/SIGINT 路径、mode 分支 - 配置、认证与 HTTP 客户端:
client.exists()/reset()全部调用点(init/login/sync)、login_raw(print_welcome=False)、重登凭据来源(merge_settings 持久化 api_key) - Record、Core 与传输: reset 时机在 transport 排空、heartbeat Timer 停止(core.py
_confirm_finish_when_enabled)与 stop_experiment 上报之后,SDK 内无线程在 reset 后使用单例 - 公共 API 与类型兼容:
swanlab.Api创建独立 Client 实例(api/init.py:74),不受单例 reset 影响;无公共 API 变更 - 安全与隐私: 无凭据泄露;reset 仅丢弃内存单例,不触碰 netrc
- 测试与验证: 新增
TestOnlineMultipleInit覆盖重登、stop 失败仍 reset、local 模式保留三个路径;fixture 精简合理(mock project 含"version": 1走新 sender,legacy/columnsmock 不再被消费;presigned fixture 移出组合 fixture 无既有用例受影响) - 横切质量: 无循环导入;临时方案注释标注 issue #1742 便于后续清理
未挂行发现
无
验证
-
uv run pytest tests/unit/sdk/cmd/init/test_init_e2e.py— 71 passed -
uv run pytest tests/unit/sdk/cmd tests/unit/sdk/internal/run— 782 passed -
uv run ruff check .— All checks passed -
uv run basedpyright— 0 errors, 0 warnings -
uv run pytest— 1634 passed, 21 skipped - PR Actions: Type check 通过;三平台 × Python 3.9-3.14 矩阵中 macos-latest/Python 3.10 提交时仍为 pending,其余全部通过
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
修复 online 模式下实验结束后复用旧 client 的 sid 导致的 401 问题(#1715)。
Run.finish()中,online 模式下重置全局 client 单例,确保下次init()重新认证并获取新的 sid。init/finish场景的测试。Changes
swanlab/sdk/internal/run/__init__.py:finish()清理全局运行实例后,在 online 模式且 client 存在时调用client.reset()。swanlab/sdk/cmd/init.py:_ensure_online_client()自动登录时传入print_welcome=False,避免重复打印欢迎语。tests/unit/sdk/cmd/init/test_init_e2e.py:将make_*_resp合并为make_ok_resp,移除未使用的媒体上传 mock 与requests依赖,新增TestOnlineMultipleInit验证 finish 后 client 重置与重新认证。Testing
已运行
uv run pytest tests/unit/sdk/cmd/init/test_init_e2e.py,71 passed。Notes