Skip to content

Fix startup with malformed persistent history - #1868

Merged
tleonhardt merged 2 commits into
python-cmd2:mainfrom
Muneer320:fix/corrupt-history-recovery
Dec 5, 2026
Merged

tleonhardt merged 2 commits into
python-cmd2:mainfrom
Muneer320:fix/corrupt-history-recovery

Conversation

@Muneer320

Copy link
Copy Markdown
Contributor

An unreadable persistent-history payload can still prevent a Cmd application from starting, even though other invalid history files already produce an error message and start with empty history.

The missing cases are invalid UTF-8 after LZMA decompression, and JSON values whose shape causes History.from_json() to raise TypeError rather than KeyError or ValueError. For example, this public API reproduction fails on current main:

import atexit
import lzma
from pathlib import Path
from tempfile import TemporaryDirectory

from cmd2 import Cmd

folder = TemporaryDirectory()
atexit.register(folder.cleanup)
history_file = Path(folder.name) / "history.dat"
history_file.write_bytes(lzma.compress(b"[]"))
Cmd(allow_cli_args=False, persistent_history_file=str(history_file))
print("Application started")

The constructor raises TypeError. Replacing b"[]" with b"\xff" reproduces the uncaught UnicodeDecodeError; null and 42 also fail during JSON restoration.

Include those exceptions in the existing recovery handlers. The application then reports the existing diagnostic, starts with empty history, and uses the existing exit handler to rewrite the history file. Valid history loading is unchanged.

The existing bad-JSON test is extended with the four failing payloads and their expected diagnostics. It has four failures and one passing control against original production source, and all five cases pass with the fix.

Local verification on Windows, CPython 3.13:

  • Seven public constructor cases: four startup failures originally; all seven pass fixed. Valid Unicode history is retained, and successful processes save reloadable history on exit.
  • History suite: 838 passed, two platform skips.
  • Full make test, with one pytest worker: 2,976 passed, 264 platform skips; 92.3% source coverage on Windows.
  • make check and the strict make docs-test pass. The docs check used a full Git history, as required by its revision-date plugin.

Other platforms and Python versions remain for upstream CI. The local full-suite run used a checkout path without spaces because existing script tests pass some paths without quoting them.

Closes #1867

@codecov

codecov Bot commented Dec 5, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.76%. Comparing base (fe09282) to head (537b7aa).

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #1868   +/-   ##
=======================================
  Coverage   99.76%   99.76%           
=======================================
  Files          23       23           
  Lines        6777     6777           
=======================================
  Hits         6761     6761           
  Misses         16       16           
Flag Coverage Δ
unittests 99.76% <100.00%> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

@tleonhardt tleonhardt left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the fix, Muneer, and for including a clear reproduction in the issue. I verified that each reported payload starts with empty history and that the rewritten history loads on the next startup. The regression test now checks the empty history directly, and all 28 CI checks passed.

@tleonhardt
tleonhardt merged commit 90be3e7 into python-cmd2:main Dec 5, 2026
28 checks passed
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.

Malformed persistent-history payloads can prevent Cmd startup

2 participants