Skip to content

[Bug]多会话/会话规则并发写入时,保存对话历史会触发 database is locked #6443

Closed

Description

@Ricraft

What happened / 发生了什么

问题描述

AstrBot 在保存 Agent 对话历史时,经常性出现触发 SQLite 锁冲突及一系列问题,在使用ai对话时无论是windows部署还是linux部署,尤其是在导入人格后均会触发。报错如下:

Error occurred while processing agent request: (sqlite3.OperationalError) database is locked
[SQL: UPDATE conversations SET updated_at=?, content=?, token_usage=? WHERE conversations.conversation_id = ?]

### Reproduce / 如何复现?

在 WebUI / WebChat 场景下,以下操作都比较容易触发:

新建会话后立刻发送第一条消息
在启用人格功能后,第一次对话就会触发,后续关闭人格并清空聊天记录,任然触发
停止当前回复后,紧接着切换会话或继续发送消息

### AstrBot version, deployment method (e.g., Windows Docker Desktop deployment), provider used, and messaging platform used. / AstrBot 版本、部署方式(如 Windows Docker Desktop 部署)、使用的提供商、使用的消息平台适配器

Linux操作系统Ubuntu-22.04-x64  使用docker部署,最开始使用宝塔面板应用商店安装,第一次出现该问题,后面改用docker直接拉取,任然出现 
Windows操作系统win11 同样使用docker部署,问题再次复现             
两个操作系统均测试使用onebot v11和官方文档中的QQsocket,触发期间napcat连接及日志均正常

### OS

Not sure

### Logs / 报错日志

```text
[2026-03-13 09:32:29.501] [Core] [ERRO] [v4.19.4] [utils.metrics:69]: 保存指标到数据库失败: (sqlite3.OperationalError) database is locked

[SQL: 

                    INSERT INTO platform_stats (timestamp, platform_id, platform_type, count)

                    VALUES (?, ?, ?, ?)

                    ON CONFLICT(timestamp, platform_id, platform_type) DO UPDATE SET

                        count = platform_stats.count + EXCLUDED.count

                    ]

[parameters: (datetime.datetime(2026, 3, 13, 9, 0), 'aiocqhttp', 'unknown', 1)]

(Background on this error at: https://sqlalche.me/e/20/e3q8)

[2026-03-13 09:32:29.508] [Core] [ERRO] [v4.19.4] [agent_sub_stages.internal:371]: Error occurred while processing agent: (sqlite3.OperationalError) database is locked

[SQL: UPDATE conversations SET updated_at=?, content=? WHERE conversations.conversation_id = ?]

[parameters: ('2026-03-13 09:32:24.433964', '[{"role": "user", "content": [{"type": "text", "text": "\\u4f60\\u662f\\u8c01"}, {"type": "text", "text": "<system_reminder>Current datetime: 2026-03 ... (51276 characters truncated) ... 6fe\\u670d\\u52a1\\u7684\\u90e8\\u7f72\\u5462\\uff1f\\u544a\\u8bc9\\u6211\\u4f60\\u7684\\u60f3\\u6cd5\\uff0c\\u6211\\u6765\\u914d\\u5408\\u4f60~"}]}]', 'e23ab3e2-5b9c-4784-80b5-813ecc1e1e6e')]

(Background on this error at: https://sqlalche.me/e/20/e3q8) 

这时对话会出现
Error occurred while processing agent request: (sqlite3.OperationalError) database is locked
[SQL: UPDATE conversations SET updated_at=?, content=?, token_usage=? WHERE conversations.conversation_id = ?]
[parameters: ('2026-03-16 10:33:29.878621', '[{"role": "user", "content": [{"type": "text", "text": "\\u4f60\\u597d"}, {"type": "text", "text": "<system_reminder>Current datetime: 2026-03-16 18: ... (376 characters truncated) ... ff0c\\u8fd8\\u662f\\u5176\\u4ed6\\u9700\\u8981\\u534f\\u52a9\\u7684\\u4e8b\\u60c5\\uff0c\\u6211\\u90fd\\u5f88\\u4e50\\u610f\\u5e2e\\u5fd9\\u3002"}]}]', 3382, '3835f073-a207-4ca4-87bd-7c1762e00163')]
(Background on this error at: https://sqlalche.me/e/20/e3q8)

### Are you willing to submit a PR? / 你愿意提交 PR 吗?

- [x] Yes!

### Code of Conduct

- [x] I have read and agree to abide by the project's [Code of Conduct](https://docs.github.com/zh/site-policy/github-terms/github-community-code-of-conduct)。

Activity

  1. dosubot commented on Mar 16, 2026

    @dosubot

    Hi @Ricraft! I'm Dosu and I’m helping the AstrBot team.

    这是一个已知问题,AstrBot 存在多个类似的 SQLite database locked 报告。

    问题根因分析:

    虽然 AstrBot 已启用 WAL 模式 并实现了 会话级 asyncio.Lock,但存在以下问题:

    1. 缺少 busy_timeout 配置 - 当写入冲突时会立即失败而非等待
    2. 会话锁按事件循环隔离 - 不同事件循环可能并发访问同一会话
    3. 缺少写入重试机制 - 数据库操作失败后不会自动重试

    如果你愿意提交 PR,建议的修复方向:

    1. 在数据库初始化时添加 busy_timeout:
    await conn.execute(text("PRAGMA busy_timeout=5000"))  # 5秒超时
    1. 为 update_conversation 添加指数退避重试机制

    2. 考虑实现全局写入队列或使用 长连接模式 减少锁竞争

    To reply, just mention @dosu.


    Share context across your team and agents. Try Dosu.

    Leave Feedback Learn about AstrBot with Dosu Add Dosu to your team

  2. added a commit that references this issue on Mar 17, 2026
    1894f4c
  3. added a commit that references this issue on Mar 17, 2026
    acbc515
  4. added 2 commits that reference this issue on Mar 19, 2026
    af26b36
    d70d742
  5. added a commit that references this issue on Mar 28, 2026
    ae67d1e
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

    area:coreThe bug / feature is about astrbot's core, backendbugSomething isn't workingpriority: p0

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions