Skip to content

Commit e720fb1

Browse files
committed
Fix extra / prefix is add in fron the shm name on POSIX
1 parent 7b30703 commit e720fb1

File tree

3 files changed

+30
-19
lines changed

3 files changed

+30
-19
lines changed

slick_queue_py.py

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -59,18 +59,22 @@ class SlickQueue:
5959
"""
6060

6161
def __init__(self, *, name: Optional[str] = None, size: Optional[int] = None, element_size: Optional[int] = None):
62-
# On Linux, POSIX shared memory names must start with /
63-
# The C++ slick_queue library passes the name directly to shm_open(),
64-
# which requires the / prefix. Python's SharedMemory strips it from .name,
65-
# but we need to add it for C++ interop.
62+
# Store the original user-provided name (without / prefix)
63+
# Python's SharedMemory will add the / prefix on POSIX systems automatically.
64+
# We strip any leading / to avoid double-prefixing (//name) on POSIX systems.
6665
self.name = name
67-
if self.name is not None and sys.platform != 'win32' and not self.name.startswith('/'):
68-
self.name = '/' + self.name
66+
if self.name is not None and self.name.startswith('/'):
67+
# Strip leading / if user provided it - Python's SharedMemory will add it back on POSIX
68+
self.name = self.name[1:]
6969

7070
# macOS has a 31-character limit for POSIX shared memory names (including leading /)
71-
if self.name is not None and sys.platform == 'darwin' and len(self.name) > 31:
72-
raise ValueError(f"Shared memory name '{self.name}' is {len(self.name)} characters, "
73-
f"but macOS has a 31-character limit. Please use a shorter name.")
71+
# Check the length that will be used (with / prefix on POSIX systems)
72+
if self.name is not None and sys.platform == 'darwin':
73+
# On macOS, Python's SharedMemory will prepend /, so check total length
74+
final_name = '/' + self.name
75+
if len(final_name) > 31:
76+
raise ValueError(f"Shared memory name '{final_name}' is {len(final_name)} characters, "
77+
f"but macOS has a 31-character limit. Please use a shorter name.")
7478

7579
self.use_shm = name is not None
7680
self._shm: Optional[SharedMemory] = None
@@ -206,17 +210,25 @@ def get_shm_name(self) -> Optional[str]:
206210
"""
207211
Get the actual shared memory name for C++ interop.
208212
209-
Returns the name with POSIX / prefix on Linux (required by C++ shm_open).
210-
Python's SharedMemory.name property strips the / prefix, but this method
211-
returns self.name which preserves it for C++ interop.
213+
Returns the name with POSIX / prefix (required by C++ shm_open).
214+
On POSIX systems (Linux/macOS), this returns the name with the / prefix.
215+
On Windows, it returns the name without modification.
212216
213217
Returns:
214218
The shared memory name that C++ code should use to open the queue.
215-
On Linux, this will have the / prefix that shm_open() requires.
219+
On POSIX systems, this will have the / prefix that shm_open() requires.
216220
"""
217-
# Return self.name (which has / prefix on Linux) rather than self._shm.name
218-
# (which has / stripped by Python)
219-
return self.name
221+
if self._shm is not None:
222+
# Use the actual name from SharedMemory (which has / prefix on POSIX)
223+
return self._shm._name
224+
elif self.name is not None:
225+
# If SharedMemory not created yet, construct the expected name
226+
# On POSIX, need to add / prefix; on Windows, use as-is
227+
if sys.platform != 'win32':
228+
return '/' + self.name
229+
else:
230+
return self.name
231+
return None
220232

221233
# Public API mirroring C++ methods
222234
def reserve(self, n: int = 1) -> int:

tests/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ add_test(
8181
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
8282
)
8383

84+
# Python/C++ interop tests
8485
add_test(
8586
NAME python_producer_cpp_consumer
8687
COMMAND ${Python3_EXECUTABLE} ${CMAKE_SOURCE_DIR}/tests/test_interop.py --test python_producer_cpp_consumer
@@ -109,6 +110,5 @@ set_tests_properties(
109110
python_producer_cpp_consumer
110111
cpp_producer_python_consumer
111112
multi_producer_interop
112-
PROPERTIES
113-
TIMEOUT 120
113+
PROPERTIES TIMEOUT 120
114114
)

tests/test_local_mode.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ def test_local_mode():
2929
data, size, read_index = q.read(read_index)
3030
assert data is not None, f"No data at iteration {i}"
3131
value = struct.unpack("<I", data[:4])[0]
32-
print(f"Read value: {value}")
3332
assert value == i, f"Expected {i}, got {value}"
3433

3534
# Cleanup (should be no-op for unlink in local mode)

0 commit comments

Comments
 (0)