Skip to content

Commit ee5500f

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 1aca203 commit ee5500f

6 files changed

Lines changed: 755 additions & 80 deletions

File tree

.github/workflows/ci.yml

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

0 commit comments

Comments
 (0)