Skip to content

prevent file data loss due to replacing an open file handle (partial fix for #5275) - #5276

Merged
softhack007 merged 2 commits into
mainfrom
fix_5275_part1
Jan 5, 2026
Merged

softhack007 merged 2 commits into
mainfrom
fix_5275_part1

Conversation

@softhack007

@softhack007 softhack007 commented Jan 5, 2026 •

Copy link
Copy Markdown
Member

The function writeObjectToFile() was re-assigning f (global file handle, file.cpp) without first completing any pending write operations on f.

Reassigning a file pointer thats still in use can lead to loss of data and file corruption.
This PR provides a fix by first checking for pending writes (doCloseFile == true) and calling f.close() when needed.

The same protection code is already preset in readObjectFromFile(), it looks like protecting writeObjectToFile() was forgotten.

Summary by CodeRabbit

  • Bug Fixes
    • Improved file-handling reliability by ensuring files are properly closed when needed.
    • Ensured cached file data is flushed before files are reopened or written, reducing risk of data loss and improving consistency.

✏️ Tip: You can customize this high-level summary in your review settings.

@coderabbitai

coderabbitai Bot commented Jan 5, 2026 •

Copy link
Copy Markdown
Contributor

Walkthrough

Calls to closeFile() unchanged; an explanatory comment clarifies internal null-check is inside f.close(). writeObjectToFile() now invokes closeFile() before reopening when doCloseFile is set to ensure any cached data is flushed.

Changes

Cohort / File(s) Summary
File I/O guard and flush behavior
wled00/file.cpp
closeFile() left functionally unchanged with added explanatory comment about internal null-check; writeObjectToFile() now performs a pre-close by calling closeFile() when doCloseFile is true to flush cached data before reopening the file.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Pre-merge checks and finishing touches

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 50.00% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately summarizes the main change: adding a safeguard to prevent file data loss when replacing an open file handle in writeObjectToFile(), which directly matches the PR objectives.
✨ Finishing touches
  • 📝 Generate docstrings

📜 Recent review details

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 8e27fe4 and a870474.

📒 Files selected for processing (1)
  • wled00/file.cpp
🚧 Files skipped from review as they are similar to previous changes (1)
  • wled00/file.cpp
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (20)
  • GitHub Check: wled_build / Build Environments (esp32c3dev_qio)
  • GitHub Check: wled_build / Build Environments (lolin_s2_mini)
  • GitHub Check: wled_build / Build Environments (esp32s3dev_8MB_opi)
  • GitHub Check: wled_build / Build Environments (esp32c3dev)
  • GitHub Check: wled_build / Build Environments (esp32dev)
  • GitHub Check: wled_build / Build Environments (esp01_1m_full)
  • GitHub Check: wled_build / Build Environments (esp32s3dev_16MB_opi)
  • GitHub Check: wled_build / Build Environments (esp32S3_wroom2)
  • GitHub Check: wled_build / Build Environments (esp01_1m_full_compat)
  • GitHub Check: wled_build / Build Environments (esp32s3_4M_qspi)
  • GitHub Check: wled_build / Build Environments (esp8266_2m_160)
  • GitHub Check: wled_build / Build Environments (esp32dev_debug)
  • GitHub Check: wled_build / Build Environments (nodemcuv2_compat)
  • GitHub Check: wled_build / Build Environments (esp8266_2m_compat)
  • GitHub Check: wled_build / Build Environments (nodemcuv2_160)
  • GitHub Check: wled_build / Build Environments (esp8266_2m)
  • GitHub Check: wled_build / Build Environments (esp01_1m_full_160)
  • GitHub Check: wled_build / Build Environments (nodemcuv2)
  • GitHub Check: wled_build / Build Environments (esp32_eth)
  • GitHub Check: Analyze (c-cpp)

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@softhack007 softhack007 changed the title prevent file data loss due to replacing an open file pointer (partial fix for #5275) prevent file data loss due to replacing an open file handle (partial fix for #5275) Jan 5, 2026
Comment thread wled00/file.cpp Resolved
@softhack007
softhack007 merged commit 32daa03 into main Jan 5, 2026
softhack007 added a commit that referenced this pull request Jan 5, 2026
prevent file data loss due to replacing an open file handle (partial fix for #5275) - provides better protection again presets.json corruption.
blazoncek added a commit to blazoncek/WLED that referenced this pull request Jan 14, 2026
@softhack007 softhack007 added this to the 0.15.4 milestone Jan 28, 2026
@softhack007
softhack007 deleted the fix_5275_part1 branch March 22, 2026 21:51
aenertia pushed a commit to aenertia/WLED that referenced this pull request Aug 18, 2026
prevent file data loss due to replacing an open file handle (partial fix for wled#5275) - provides better protection again presets.json corruption.
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