Skip to content

fix: use RAII render lock everywhere#916

Merged
daveallie merged 2 commits intocrosspoint-reader:masterfrom
ngxson:xsn/raii_render_lock
Feb 16, 2026
Merged

fix: use RAII render lock everywhere#916
daveallie merged 2 commits intocrosspoint-reader:masterfrom
ngxson:xsn/raii_render_lock

Conversation

@ngxson
Copy link
Contributor

@ngxson ngxson commented Feb 16, 2026

Summary

Follow-up to #774


AI Usage

While CrossPoint doesn't have restrictions on AI tools in contributing, please be transparent about their usage as it
helps set the right context for reviewers.

Did you use AI tools to help write this code? NO

Summary by CodeRabbit

Release Notes

  • Refactor
    • Modernized internal synchronization mechanisms across multiple components to improve code reliability and maintainability. All functionality remains unchanged.

// Authenticate
if (!KOREADER_STORE.hasCredentials()) {
// Can't authenticate without credentials - just show message briefly
xSemaphoreGive(renderingMutex);
Copy link
Contributor Author

@ngxson ngxson Feb 16, 2026

Choose a reason for hiding this comment

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

Note: this is a stray mutex unlock, left-over from the original PR

@ngxson ngxson requested a review from a team February 16, 2026 10:59
Copy link
Member

@daveallie daveallie left a comment

Choose a reason for hiding this comment

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

Looks good to me, just going to quickly test on device too

@daveallie
Copy link
Member

@coderabbitai review

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Feb 16, 2026

✅ Actions performed

Review triggered.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Feb 16, 2026

📝 Walkthrough

Walkthrough

The PR systematically replaces explicit semaphore-based locking (xSemaphoreTake/xSemaphoreGive) with RAII-style RenderLock scopes across seven activity files. State updates and rendering-related critical sections now use scoped lock objects instead of manual semaphore management, with one notable exception where a semaphore release was removed.

Changes

Cohort / File(s) Summary
Simple RenderLock Replacements
src/activities/network/WifiSelectionActivity.cpp, src/activities/settings/ClearCacheActivity.cpp
Replaces single semaphore block (xSemaphoreTake/xSemaphoreGive) with scoped RenderLock around state transitions. Minimal scope changes (~4 lines each).
Complex Reader Activity Refactorings
src/activities/reader/EpubReaderActivity.cpp, src/activities/reader/KOReaderSyncActivity.cpp
Multiple semaphore blocks across diverse code paths replaced with RenderLock scopes. Covers navigation, caching, sync operations, and state updates. EpubReaderActivity affects loop() navigation branches and jumpToPercent(); KOReaderSyncActivity affects state transitions, sync stages, and progress handling.
Settings Activity Refactorings
src/activities/settings/KOReaderAuthActivity.cpp, src/activities/settings/OtaUpdateActivity.cpp
Replaces semaphore blocks around state and message updates with RenderLock scopes. Covers authentication flow and OTA state transitions (CHECKING_FOR_UPDATE, FAILED, etc.).
Lock Management Anomaly
src/activities/settings/KOReaderSettingsActivity.cpp
Removes xSemaphoreGive(renderingMutex) call in handleSelection() when no credentials exist, potentially leaving the mutex unreleased on this error path.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Merge Conflict Detection ⚠️ Warning ⚠️ Unable to check for merge conflicts: Failed to fetch base branch: From https://github.com/crosspoint-reader/crosspoint-reader
! [rejected] master -> master (non-fast-forward)
+ 0bc6747...6702060 master -> origin/master (forced update)
✅ 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 objective of the PR: replacing explicit semaphore locking with RAII-style RenderLock across multiple files. This is the core change across all modified files.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.


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

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

🧹 Nitpick comments (3)
src/activities/reader/KOReaderSyncActivity.cpp (1)

203-206: Inconsistent locking pattern: manual semaphore still used here.

This code path still uses xSemaphoreTake/xSemaphoreGive while the rest of this file and the PR migrate to RenderLock. For consistency, consider using RenderLock here as well.

♻️ Suggested refactor for consistency
-          xSemaphoreTake(self->renderingMutex, portMAX_DELAY);
-          self->statusMessage = "Calculating document hash...";
-          xSemaphoreGive(self->renderingMutex);
+          {
+            RenderLock lock(*self);
+            self->statusMessage = "Calculating document hash...";
+          }
src/activities/reader/EpubReaderActivity.cpp (2)

410-431: Consider narrowing the lock scope around I/O operations.

The lock is held during epub->clearCache() and epub->setupCacheDir(), which involve file system I/O. While the atomicity across these operations is desirable, holding the render lock during I/O could potentially cause UI stutter if the render task needs to update. Since this path immediately triggers pendingGoHome, the impact is likely minimal, but it's worth noting.


466-483: Lock scope includes file I/O.

The RenderLock is held during SETTINGS.saveToFile(), which performs file I/O. Similar to the DELETE_CACHE case, this could potentially block the render task. However, the scope ensures that the orientation change, setting persistence, and section reset occur atomically, which may be the intended behavior.

@daveallie
Copy link
Member

daveallie commented Feb 16, 2026

@ngxson CodeRabbit found one additional use of semaphores directly in src/activities/reader/KOReaderSyncActivity.cpp, can this also utilise RenderLock or was there a reason to leave it as is?

It could be:

{
  RenderLock lock(*self);
  self->statusMessage = "Calculating document hash...";
}

right?

@ngxson
Copy link
Contributor Author

ngxson commented Feb 16, 2026

yeah good catch, I fixed in the last commit

@daveallie daveallie merged commit 3d47c08 into crosspoint-reader:master Feb 16, 2026
6 checks passed
Unintendedsideeffects pushed a commit to Unintendedsideeffects/crosspoint-reader that referenced this pull request Feb 17, 2026
## Summary

Follow-up to
crosspoint-reader#774

---

### AI Usage

While CrossPoint doesn't have restrictions on AI tools in contributing,
please be transparent about their usage as it
helps set the right context for reviewers.

Did you use AI tools to help write this code? **NO**


<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->

## Summary by CodeRabbit

## Release Notes

* **Refactor**
* Modernized internal synchronization mechanisms across multiple
components to improve code reliability and maintainability. All
functionality remains unchanged.

<!-- end of auto-generated comment: release notes by coderabbit.ai -->
saslv pushed a commit to saslv/crosspoint-reader that referenced this pull request Feb 19, 2026
## Summary

Follow-up to
crosspoint-reader#774

---

### AI Usage

While CrossPoint doesn't have restrictions on AI tools in contributing,
please be transparent about their usage as it
helps set the right context for reviewers.

Did you use AI tools to help write this code? **NO**


<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->

## Summary by CodeRabbit

## Release Notes

* **Refactor**
* Modernized internal synchronization mechanisms across multiple
components to improve code reliability and maintainability. All
functionality remains unchanged.

<!-- end of auto-generated comment: release notes by coderabbit.ai -->
el pushed a commit to el/crosspoint-reader that referenced this pull request Feb 19, 2026
## Summary

Follow-up to
crosspoint-reader#774

---

### AI Usage

While CrossPoint doesn't have restrictions on AI tools in contributing,
please be transparent about their usage as it
helps set the right context for reviewers.

Did you use AI tools to help write this code? **NO**


<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->

## Summary by CodeRabbit

## Release Notes

* **Refactor**
* Modernized internal synchronization mechanisms across multiple
components to improve code reliability and maintainability. All
functionality remains unchanged.

<!-- end of auto-generated comment: release notes by coderabbit.ai -->
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