[BUG]: Non-Atomic Gap Log Writes Can Cause Data Loss After an Interrupted Save

未关闭 适合新手
#61 2 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

@SYED-IRTEZA-HUSSAIN-RIZVI 已经在做这个了。

开始于 2026年11月9日。

评估

难度
2/5
预计耗时
1-3 小时
新手友好度
74/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
活跃
技术栈
json, python
领域
backend, databases

调研方向

从 bot.py 中的 _save_gap_log() 和 _load_gap_log() 开始,然后检查项目是否有针对间隔日志持久化的测试。在保存开始后复现写入失败,并验证现有日志仍可读取。当保存失败时保留之前的有效条目,且保存成功时存储更新后的条目,issue 即完成。

由索引模型根据 Issue 内容生成。

描述

bug enhancement
Bug Description

The _save_gap_log() function writes directly to gap_log.json using write mode ("w"). Opening the file in this mode truncates its existing contents before the new JSON data has been completely written.

If the process is interrupted, crashes, or encounters a write error during saving, the file may be left with incomplete or invalid JSON. When _load_gap_log() subsequently attempts to read the file, it catches json.JSONDecodeError and returns an empty list.

As a result, previously persisted gap-log entries may become inaccessible after an interrupted or failed save operation.

The save operation should use a safer file-writing strategy, such as writing the updated JSON data to a temporary file in the same directory and replacing the original file with os.replace() only after the temporary write completes successfully. This would help prevent an incomplete write from replacing the existing valid log.

This issue concerns data integrity and potential data loss caused by interrupted, non-atomic file writes. It is separate from the event-loop blocking issue tracked in [#57](https://github.com/AOSSIE-Org/SkillBot/issues/57).

Steps to Reproduce
  1. Set up SkillBot and ensure that gap_log.json exists and contains valid JSON with multiple entries.
  2. Start the bot and allow _load_gap_log() to load the existing entries.
  3. Trigger _save_gap_log() with updated gap-log entries.
  4. Observe that _save_gap_log() opens gap_log.json using write mode ("w"), which truncates the existing file.
  5. Simulate a write failure after the original file has been truncated and some JSON content has been written. This can be done in a controlled test by mocking the serialization or write operation to raise an exception partway through.
  6. Inspect gap_log.json and check whether it contains incomplete or invalid JSON.
  7. Invoke _load_gap_log() again or restart the bot.
  8. Observe that the loader encounters a json.JSONDecodeError and returns an empty list, potentially making the previously stored entries inaccessible.
Logs and Screenshots

No response

Environment Details
  • Application: SkillBot Discord Bot
  • Repository: AOSSIE-Org/SkillBot
  • Operating System: Windows 11
  • Python Version: 3.11.3
  • Framework: asyncio
  • Affected File: bot.py
  • Affected Functions: _save_gap_log() and _load_gap_log()
  • Affected File: gap_log.json
  • Storage Format: JSON
  • File Operation: Direct file writing using open() with "w" mode
  • Relevant Exception: json.JSONDecodeError
  • Affected Component: Persistent gap-log storage and recovery
Impact

Medium - Feature works but has issues

Code of Conduct
  • I have joined the Discord server and will post updates there
  • I have searched existing issues to avoid duplicates
主要语言
Python
星标
10
派生
11
平均合并
6 分钟
30 天内合并 PR
4

环境准备

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

AOSSIE-Org/SkillBot 的其他 Issue

查看 AOSSIE-Org/SkillBot 的全部 Issue

相似的 Issue

更多 Python Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。