Skip to content

Commit 2593213

Browse files
committed
Fix FreeBSD shm_unlink failure by adding '/' prefix in remove()
Problem: Tests fail on the second run on FreeBSD with ShmTest.RemoveByName failing. After the first test run completes, subsequent runs fail because shared memory objects are not properly removed. Root cause: FreeBSD's shm_unlink() is stricter than Linux about POSIX compliance. The remove(char const * name) function was calling shm_unlink() without the '/' prefix, while acquire() was using '/'+name format. This inconsistency caused: - Linux: Silently tolerates both /name and name formats - FreeBSD: Strictly requires /name format, shm_unlink("name") fails When shm_unlink() fails to remove the shared memory object: 1. First test run creates /remove_by_name_test_1 2. Test calls shm::remove("remove_by_name_test_1") 3. shm_unlink("remove_by_name_test_1") fails on FreeBSD (missing '/') 4. Shared memory object remains in the system 5. Second test run tries to reuse the same name -> conflict -> test fails Solution: 1. Fix remove(char const * name) to prepend '/' to the name for consistency with acquire() function, ensuring POSIX compliance 2. Add error checking for all shm_unlink() calls to log failures with errno This ensures proper cleanup on FreeBSD and maintains compatibility with Linux. Changes: - Modified remove(char const * name) to use '/'+name format - Added error logging for all three shm_unlink() calls - Now consistent with POSIX requirement: shared memory names must be /somename Tested on FreeBSD 15: Multiple consecutive test runs now pass without failures.
1 parent 37f16ce commit 2593213

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

src/libipc/platform/posix/shm_posix.cpp

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,10 @@ std::int32_t release(id_t id) noexcept {
173173
else if ((ret = acc_of(ii->mem_, ii->size_).fetch_sub(1, std::memory_order_acq_rel)) <= 1) {
174174
::munmap(ii->mem_, ii->size_);
175175
if (!ii->name_.empty()) {
176-
::shm_unlink(ii->name_.c_str());
176+
int unlink_ret = ::shm_unlink(ii->name_.c_str());
177+
if (unlink_ret == -1) {
178+
ipc::error("fail shm_unlink[%d]: %s\n", errno, ii->name_.c_str());
179+
}
177180
}
178181
}
179182
else ::munmap(ii->mem_, ii->size_);
@@ -190,7 +193,10 @@ void remove(id_t id) noexcept {
190193
auto name = std::move(ii->name_);
191194
release(id);
192195
if (!name.empty()) {
193-
::shm_unlink(name.c_str());
196+
int unlink_ret = ::shm_unlink(name.c_str());
197+
if (unlink_ret == -1) {
198+
ipc::error("fail shm_unlink[%d]: %s\n", errno, name.c_str());
199+
}
194200
}
195201
}
196202

@@ -199,7 +205,12 @@ void remove(char const * name) noexcept {
199205
ipc::error("fail remove: name is empty\n");
200206
return;
201207
}
202-
::shm_unlink(name);
208+
// For portable use, a shared memory object should be identified by name of the form /somename.
209+
ipc::string op_name = ipc::string{"/"} + name;
210+
int unlink_ret = ::shm_unlink(op_name.c_str());
211+
if (unlink_ret == -1) {
212+
ipc::error("fail shm_unlink[%d]: %s\n", errno, op_name.c_str());
213+
}
203214
}
204215

205216
} // namespace shm

0 commit comments

Comments
 (0)