Skip to content

Commit e85941d

Browse files
author
concore-dev
committed
fix(shm): serialise concurrent writers with seq# + POSIX semaphore (fixes #195)
Two writers calling write_SM() on the same shmget() key used to interleave strncpy() byte writes with no mutex, no semaphore, no memory barrier, and no version counter - producing torn payloads on the reader side. Layout (any reader that does not know about the header will see a leading integer and fail loudly, which is the desired signal): bytes [0..7] uint64 little-endian sequence number (odd = write in progress, even = ready, 0 = none) bytes [8..end] null-terminated payload Protocol: a seqlock under a POSIX semaphore keyed by (shm_key + 1). The writer acquires the lock, bumps seq# to odd, memcpy's the payload, NUL-terminates, __ATOMIC_RELEASE fence, then bumps seq# back to even. The reader does a seqlock-style read: load seq#1, copy payload, __ATOMIC_ACQUIRE fence, load seq#2 - accept only if seq#1 == seq#2 and seq#1 is even. Readers do not take the semaphore, so fan-out stays cheap. Mirrors the same change in concoredocker.hpp. Adds: tests/test_shm_concurrency.py Python regression (fork-based, Linux) tests/test_shm_cpp_harness.cpp fork-based C++ harness for CI tests/test_shm_cpp_smoke.cpp header self-contained check plus a cpp-shm-test job in .github/workflows/ci.yml that builds and runs the harness on ubuntu-latest.
1 parent a30127e commit e85941d

6 files changed

Lines changed: 782 additions & 80 deletions

File tree

.github/workflows/ci.yml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,3 +96,20 @@ jobs:
9696
- name: Validate Dockerfile build
9797
if: steps.filter.outputs.dockerfile == 'true'
9898
run: docker build -f Dockerfile.py -t concore-py-test .
99+
100+
cpp-shm-test:
101+
runs-on: ubuntu-latest
102+
steps:
103+
- uses: actions/checkout@v4
104+
105+
- name: Install g++
106+
run: sudo apt-get update && sudo apt-get install -y g++
107+
108+
- name: Compile concore.hpp smoke test
109+
run: g++ -std=c++17 -Wall -Wextra -c tests/test_shm_cpp_smoke.cpp -o /tmp/smoke.o
110+
111+
- name: Build C++ SHM harness
112+
run: g++ -std=c++17 -O2 -Wall -Wextra -o /tmp/test_shm_cpp_harness tests/test_shm_cpp_harness.cpp
113+
114+
- name: Run C++ SHM harness
115+
run: /tmp/test_shm_cpp_harness

0 commit comments

Comments
 (0)