rocketmq icon indicating copy to clipboard operation
rocketmq copied to clipboard

[Bug] CheckpointFile skips valid backup recovery when the primary numeric header is malformed

Open hickey-ai opened this issue 1 month ago • 1 comments

Before Creating the Bug Report

  • [x] I found a bug, not just asking a question, which should be created in GitHub Discussions.

  • [x] I have searched the GitHub Issues and GitHub Discussions of this repository and believe that this is not a duplicate.

  • [x] I have confirmed that this bug belongs to the current repository, not other repositories of RocketMQ.

Runtime platform environment

Windows 11; local checkpoint files and native common/remoting module tests. No live broker or HA failover claim.

RocketMQ version

develop, revision 5.5.1, commit 57c2dc04f78cc7e73e6b8c9436ee0fff0d0f46c3.

JDK Version

OpenJDK 1.8.0_302 (GraalVM CE 21.2.0), Maven 3.6.3.

Describe the Bug

CheckpointFile.read(String) parses the entry count and CRC with Integer.parseInt. An empty, truncated or nonnumeric header throws NumberFormatException, while public read() only catches IOException to recover from .bak. A valid backup is therefore ignored. EpochFileCache.initCacheFromFile also only catches IOException.

Steps to Reproduce

  1. Create a CheckpointFile whose serializer writes and reads the entry string unchanged.
  2. Write Collections.singletonList("saved") and copy the checkpoint file to checkpoint.getBackFilePath().
  3. Replace the primary file with "invalid\n0\nvalue\n" (or an empty file).
  4. Call checkpoint.read().

The valid backup can be read on its own; the malformed primary prevents reaching it.

What Did You Expect to See?

Malformed numeric headers enter the existing IOException recovery path and the valid backup is returned. If the backup header is also invalid, the final failure is IOException with the failing path and NumberFormatException as its cause.

What Did You See Instead?

An unchecked NumberFormatException escapes from parsing the primary header instead of loading the valid backup.

Additional Context

The proposed fix only wraps the two numeric header parses; serializer exceptions are not swallowed. Existing valid-primary precedence and CRC/count mismatch behavior are regression controls. Six new header tests plus the three existing remoting CheckpointFileTest tests pass on the independent patch (9 native tests, 0 failures/errors/skips). Baseline native Surefire reproduced the NumberFormatException and stopped after the first failure; not all six new methods ran before the fix. Checkstyle/SpotBugs pass.

PR #11032 concerns empty checkpoint state and does not handle malformed numeric headers. Issue/PR/discussion searches did not identify this header-recovery fix.

AI assistance: OpenAI Codex was used for investigation, implementation and tests.

hickey-ai avatar Oct 20 '26 08:10 hickey-ai

Issue Evaluation

Category: type/bug | Status: Confirmed

The reported issue has been verified against the current codebase on the develop branch.

Root Cause: In CheckpointFile.read(String) (lines 107, 110), Integer.parseInt(reader.readLine()) throws NumberFormatException (unchecked) when the primary file header is malformed or empty. The public read() method (line 144) only catches IOException, so the NumberFormatException escapes without attempting the valid .bak backup recovery path.

Impact: common module — checkpoint file recovery is bypassed when the primary file header is corrupted, even if the backup is intact. This affects components relying on CheckpointFile for durable state (e.g., consumer offset checkpoints, HA checkpoint).

Severity: Medium — affects fault-tolerance recovery path, not the normal I/O path.

Verified reproduction:

  • Primary file contains "invalid\n0\nvalue\n" → NumberFormatException escapes
  • Backup file is valid but never reached
  • Control: standalone backup read works correctly

Wrapping the parseInt calls or broadening the catch clause to include NumberFormatException resolves this. An automated fix proposal may be generated. Reply /approve to proceed with PR generation.


Automated evaluation by RockteMQ-AI

RockteMQ-AI avatar Oct 20 '26 09:10 RockteMQ-AI