Skip to content

Fix thread-safety issue in LockGuard and ScopedLock by using atomic operations for shared_ptr.#3324

Closed
kevingpqi123 wants to merge 1 commit intomainfrom
bugfix/kevingpqi_lockguard
Closed

Fix thread-safety issue in LockGuard and ScopedLock by using atomic operations for shared_ptr.#3324
kevingpqi123 wants to merge 1 commit intomainfrom
bugfix/kevingpqi_lockguard

Conversation

@kevingpqi123
Copy link
Collaborator

Fix a race condition in lock acquisition: when one thread was about to lock rootLocker, another thread could call updateRootLocker to replace the shared_ptr, potentially destroying the mutex before the first thread locked it.

Changes:

  • LockGuard and ScopedLock now accept a pointer to shared_ptr and use std::atomic_load internally to safely snapshot the shared_ptr before locking.
  • updateRootLocker uses std::atomic_store to replace rootLocker atomically.
  • PAGPlayer::setSurfaceInternal uses std::atomic_store when updating PAGSurface::rootLocker to keep read/write atomic operations symmetric.
  • Added missing #include to LockGuard.h and ScopedLock.h.

@codecov-commenter
Copy link

Codecov Report

❌ Patch coverage is 65.32258% with 43 lines in your changes missing coverage. Please review.
✅ Project coverage is 75.25%. Comparing base (0dd6334) to head (8509436).

Files with missing lines Patch % Lines
src/rendering/PAGPlayer.cpp 38.46% 24 Missing ⚠️
src/rendering/PAGSurface.cpp 36.36% 7 Missing ⚠️
src/rendering/layers/PAGLayer.cpp 80.00% 5 Missing ⚠️
src/rendering/layers/PAGImageLayer.cpp 50.00% 3 Missing ⚠️
src/rendering/layers/PAGTextLayer.cpp 80.00% 2 Missing ⚠️
src/rendering/layers/PAGFile.cpp 85.71% 1 Missing ⚠️
src/rendering/utils/LockGuard.h 50.00% 0 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3324      +/-   ##
==========================================
- Coverage   75.25%   75.25%   -0.01%     
==========================================
  Files         503      503              
  Lines       33299    33300       +1     
  Branches    10739    10630     -109     
==========================================
  Hits        25060    25060              
+ Misses       6031     6027       -4     
- Partials     2208     2213       +5     

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

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@domchen
Copy link
Collaborator

domchen commented Mar 16, 2026

改法增加了内存越界访问的风险,要继续分析真正的crash原因,不是这么修改。

@domchen domchen closed this Mar 16, 2026
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