Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Aug 26, 2025

Fixes an issue where RedNotebook would crash when encountering corrupted journal files containing null characters or other control characters that are invalid in YAML.

Problem

When RedNotebook encountered a corrupted journal file (e.g., one containing null bytes \x00), the YAML parser would throw a ReaderError with the message "unacceptable character #x0000: control characters are not allowed". The application would catch this exception but then call sys.exit(1), causing RedNotebook to crash completely and preventing users from accessing any of their journal entries.

Solution

This PR modifies the file loading logic to handle corrupted files gracefully:

  1. Detect corrupted files: When a YAML ReaderError mentions control characters, the file is identified as corrupted rather than just malformed
  2. Rename corrupted files: Corrupted files are automatically renamed with a _corrupted suffix (e.g., 2024-10.txt2024-10_corrupted.txt)
  3. Continue loading: The application continues loading other valid journal files instead of crashing
  4. User notification: Users receive a clear error message explaining what happened and how to recover their data

Example

Before this fix:

ERROR: unacceptable character #x0000: control characters are not allowed
[Application crashes - user loses access to all journal entries]

After this fix:

ERROR: unacceptable character #x0000: control characters are not allowed
INFO: Renamed corrupted file 2024-10.txt to 2024-10_corrupted.txt
[Application continues normally, user sees error dialog with recovery instructions]

Changes Made

  • Modified _load_month_from_disk() in storage.py to detect and rename corrupted files
  • Updated load_all_months_from_disk() to return information about corrupted files
  • Added user-friendly error messages in journal.py with recovery instructions
  • Ensured backward compatibility with existing save/load functionality
  • Added comprehensive test suite covering various corruption scenarios

Testing

  • Tested with the actual corrupted file from the issue report
  • Verified normal file operations continue to work correctly
  • Added test cases for multiple corruption scenarios and edge cases
  • Confirmed error messages are properly displayed to users

This change transforms a catastrophic crash into a graceful degradation with clear recovery guidance, significantly improving the user experience when dealing with data corruption.


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI changed the title [WIP] Fix @jendrikseipp/rednotebook/issues/786 Fix #786: Handle corrupted journal files gracefully by renaming them Aug 26, 2025
Copilot AI requested a review from jendrikseipp August 26, 2025 21:25
@jendrikseipp
Copy link
Owner

@copilot fix tests

Copy link
Contributor Author

Copilot AI commented Aug 26, 2025

@copilot fix tests

Fixed the tests by adding comprehensive GTK mocking in conftest.py and updating the journal test to work without GTK dependencies. All tests now pass (94 passed, 2 skipped) in the headless environment. Changes in commit 449d336.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants