Skip to content

Commit e4b6438

Browse files
sawenzelclaude
andcommitted
Remove the o2-sim shared-memory segment when its last user detaches
This stops a killed o2-sim from leaking its SysV shared-memory segment. - The segment (1 GB per worker) was only removed in the driver's normal cleanup. - It is now marked IPC_RMID right after creation; Linux still lets the workers and the merger attach by id. - The kernel frees it once the last process detaches, also after a crash. - The segment is now created with mode 0600 instead of 0666. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent f085736 commit e4b6438

1 file changed

Lines changed: 5 additions & 1 deletion

File tree

‎Common/Utils/src/ShmManager.cxx‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ bool ShmManager::createGlobalSegment(int nsegments)
123123
LOG(info) << "CREATING SIM SHARED MEM SEGMENT FOR " << nsegments << " WORKERS";
124124
// LOG(info) << "SIZEOF ShmMetaInfo " << sizeof(ShmMetaInfo);
125125
const auto totalsize = sizeof(ShmMetaInfo) + SHMPOOLSIZE * nsegments;
126-
if ((mShmID = shmget(IPC_PRIVATE, totalsize, IPC_CREAT | 0666)) == -1) {
126+
if ((mShmID = shmget(IPC_PRIVATE, totalsize, IPC_CREAT | 0600)) == -1) {
127127
perror("shmget: shmget failed");
128128
} else {
129129
// We are attaching once to determine a common virtual address under which everyone else should attach.
@@ -143,6 +143,10 @@ bool ShmManager::createGlobalSegment(int nsegments)
143143
// TODO: consider using named posix shared memory segments to avoid this
144144
setenv(SHMIDNAME, std::to_string(mShmID).c_str(), 1);
145145
setenv(SHMADDRNAME, std::to_string((unsigned long long)(addr)).c_str(), 1);
146+
147+
// mark the segment for removal right away: Linux still lets the workers attach by id,
148+
// and the kernel frees it when the last process detaches, even after a crash
149+
shmctl(mShmID, IPC_RMID, nullptr);
146150
return true;
147151
}
148152
LOG(info) << "SHARED MEM INITIALIZED AT ID " << mShmID;

0 commit comments

Comments
 (0)