Skip to content

fix: reset online client singleton after run finish to avoid stale sid - #1744

Merged
SAKURA-CAT merged 1 commit into
mainfrom
fix/reinit
Aug 20, 2026
Merged

fix: reset online client singleton after run finish to avoid stale sid#1744
SAKURA-CAT merged 1 commit into
mainfrom
fix/reinit

Conversation

@SAKURA-CAT

Copy link
Copy Markdown
Member

Summary

修复 online 模式下实验结束后复用旧 client 的 sid 导致的 401 问题(#1715)。

  • Run.finish() 中,online 模式下重置全局 client 单例,确保下次 init() 重新认证并获取新的 sid。
  • 自动重新登录时抑制欢迎语输出。
  • 清理测试 mock 助手,统一响应体构造,并补充多次 init/finish 场景的测试。

Changes

  • swanlab/sdk/internal/run/__init__.pyfinish() 清理全局运行实例后,在 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

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 SAKURA-CAT added the 🐛 bug Something isn't working label Aug 20, 2026
@SAKURA-CAT SAKURA-CAT self-assigned this Aug 20, 2026

@SAKURA-CAT SAKURA-CAT left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

审核结论: 通过

本次 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 /columns mock 不再被消费;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,其余全部通过

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

🐛 bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant