Skip to content

Conversation

@0xfullex
Copy link
Contributor

@0xfullex 0xfullex commented Jan 6, 2026

Summary

  • Add pthread_spinlock to protect free list operations from race conditions
  • Fix use-after-unmap bug in detach_article_block_shm()

Changes

Race Condition Fix (Free List)

Added pthread_spinlock_t free_list_spinlock to protect concurrent access to the article block free list:

  1. pop_free_article_block(): Lock before accessing p_block_free_list, unlock after
  2. push_free_article_block(): Lock before modifying p_block_free_list, unlock after

Spinlock is chosen over mutex because:

  • Critical section is very short (only a few pointer operations)
  • No blocking operations inside critical section
  • Lower latency than mutex for short critical sections

Use-After-Unmap Fix

In detach_article_block_shm(), the code was accessing p_article_block_pool->shm_count and p_article_block_pool->shm_size after potentially unmapping memory. Fixed by:

  1. Saving shm_count and shm_size to local variables before any unmap operations
  2. Using saved values for loop iteration and final unmap

Lifecycle Management

  • Initialize spinlock in article_block_init()
  • Destroy spinlock in detach_article_block_shm()

Test Plan

  • Compile verification
  • Concurrent access testing with multiple processes
  • Verify no memory leaks on cleanup

🤖 Generated with Claude Code

- Add pthread_spinlock to protect pop_free_article_block() and
  push_free_article_block() operations from race conditions
- Initialize spinlock in article_block_init()
- Destroy spinlock in detach_article_block_shm()
- Fix use-after-unmap in detach_article_block_shm() by saving
  shm_count and shm_size before unmapping the pool

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
leafok88 added a commit that referenced this pull request Jan 7, 2026
Copy link
Collaborator

@leafok88 leafok88 left a comment

Choose a reason for hiding this comment

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

pthread spinlock is not proper for this multi-process application.

@leafok88 leafok88 closed this Jan 7, 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.

2 participants