Skip to content

SpiderMonkey 后端删除账户后可能残留仅含 spidermonkey-cache.json 的空壳目录,导致重新导入一直报「账户已存在」 #333

Backlog

Description

@xzhouzhou758

发生了什么 / What happened

在使用 SpiderMonkey WAA 后端时,如果一个账户刚触发过 WAA 初始化/刷新后不久被删除(DELETE /api/accounts/{id}),账户目录下会偶发残留下一个权限为 0755、内部仅包含 spidermonkey-cache.json、不含 account.json 与 storage-state.json 的空壳目录:

drwxr-xr-x 2 root root  4096 ... auth/user@example.com
-rw------- 1 root root 35864 ... auth/user@example.com/spidermonkey-cache.json

由此会造成一个无法通过管理页或 API 自愈的死循环:

  1. 由于目录中缺少 account.json 和 storage-state.json,启动和运行时既不会将该目录加载进账户池(GET /api/accounts 中查不到),也不会归类为可删除的异常目录(DeleteFailure 中也没有),调用 DELETE /api/accounts/user@example.com 会直接返回 404;
  2. 之后再次导入同一邮箱的 Cookie 时,校验通过但走到保存阶段就会稳定报错:账户已存在: user@example.com;
  3. 除非管理员手动登录宿主机删除该残留目录,否则该邮箱再也无法重新导入。

生产环境中已连续遇到 4 个相同特征的残留空壳目录(均表现为 drwxr-xr-x 且目录内仅有 spidermonkey-cache.json,mtime 与该账户执行删除的时间完全吻合)。

源码定位(截至 v0.3.15)

  1. internal/aistudio/page_resources.go:266-280 (loadPageIcons):

    for _, icon := range []string{rich, tab} {
        if icon == "" {
            continue
        }
        go func() {
            _ = runtime.fetchCached(context.WithoutCancel(ctx), icon, subresourceHeaders(icon, imageAccept, "image", "u=6"))
        }()
    }

    这里启动了未纳入 Worker 停止等待、且显式使用 context.WithoutCancel(ctx) 的后台 goroutine 拉取页面图标。当刷新失败或刚完成启动后紧接着调用 DeleteAccount(os.RemoveAll(directory) 删掉账户目录),该 goroutine 仍会在图标请求完成后调用 pageCache.store() -> atomicWriteFile(cache.path, data, 0o600)(page_resources.go:557),把已删除的 auth/<email> 目录以 0755 重新建出来并写入 spidermonkey-cache.json。

  2. internal/aistudio/accounts.go:828-831 (AccountStore.Create):

    directory := filepath.Join(root, id)
    if _, err := os.Stat(directory); err == nil {
        return nil, nil, fmt.Errorf("%w: %s", ErrAccountExists, id)
    }

    导入创建账户时仅通过 os.Stat(directory) 检查目录是否存在,一旦撞上上述仅含 spidermonkey-cache.json 的空壳目录就会直接返回 ErrAccountExists。

修复建议

  1. pageCache.store() 写盘前仅在账户目录(及核心认证文件)仍存在时写入,不通过 MkdirAll 重建已被删除的账户目录;
  2. loadPageIcons 尽量绑定到 Worker 生命周期或可取消上下文,避免账户删除后后台 goroutine 继续向已删除目录写文件;
  3. AccountStore.Create 遇到已存在目录时,若目录内不存在 account.json 与 storage-state.json(仅残留缓存文件或为空目录),建议视为无效残留清理后继续创建,避免卡死重导。

版本 / Version

v0.3.13(已核对 v0.3.15 源码中上述逻辑仍存在)

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions