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

オープン 初心者向け
#61 コメント 2 件 リアクション 0 件 担当者 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分
マージ済み PR(30日)
4

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

AOSSIE-Org/SkillBot のほかの issue

AOSSIE-Org/SkillBot の issue をすべて見る

似ている issue

Python の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。