Skip to content

Conversation

@softhack007
Copy link
Member

@softhack007 softhack007 commented Jan 5, 2026

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
Copy link
Contributor

coderabbitai bot commented Jan 5, 2026

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
@softhack007 softhack007 merged commit 32daa03 into main Jan 5, 2026
51 checks passed
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.
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.

3 participants