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

Aperta Adatta ai principianti
#61 2 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

@SYED-IRTEZA-HUSSAIN-RIZVI ci sta già lavorando.

Dal 9/11/2026.

Valutazione

Difficoltà
2/5
Tempo stimato
1-3 ore
Idoneità per principianti
74/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
json, python
Ambito
backend, databases

Direzione di ricerca

Inizia in bot.py con _save_gap_log() e _load_gap_log(), poi verifica se il progetto include test per la persistenza del registro delle lacune. Riproduci un errore di scrittura dopo l’inizio del salvataggio e verifica che il registro esistente resti leggibile. L’issue è completata quando un salvataggio non riuscito conserva le voci valide precedenti e un salvataggio riuscito memorizza le voci aggiornate.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

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
Lingua principale
Python
Stelle
10
Fork
11
Merge medio
6m
PR unite (30g)
4

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di AOSSIE-Org/SkillBot

Tutte le issue di AOSSIE-Org/SkillBot

Issue simili

Altre issue su Python

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.