From 649de65a7f927b9c26df864359bdb79387deea90 Mon Sep 17 00:00:00 2001 From: Theodore Kisner Date: Wed, 20 May 2026 13:16:10 -0700 Subject: [PATCH] Change the naming convention of shared mem segments This aligns the naming with the style used by the python standard library. It also limits the byte length to stay under thresholds on some OS flavors. --- pyproject.toml | 3 ++- src/pshmem/registry.py | 2 ++ src/pshmem/shmem.py | 7 ++----- src/pshmem/utils.py | 23 ++++++++++++----------- 4 files changed, 18 insertions(+), 17 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index cd7fb75..e0e233b 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -10,13 +10,14 @@ readme = "README.md" maintainers = [ { name = "Theodore Kisner", email = "tskisner.public@gmail.com" }, ] +license = "BSD-2-Clause" +license-files = ["LICENSE"] requires-python = ">=3.10" dependencies = [ "numpy", ] classifiers = [ "Development Status :: 5 - Production/Stable", - "License :: OSI Approved :: BSD License", "Programming Language :: Python", "Programming Language :: Python :: 3", "Programming Language :: Python :: 3.10", diff --git a/src/pshmem/registry.py b/src/pshmem/registry.py index f9ccb54..c385f2b 100644 --- a/src/pshmem/registry.py +++ b/src/pshmem/registry.py @@ -62,6 +62,8 @@ def _register_signals(): signal.signal(signal.SIGTERM, _signal_handler) signal.signal(signal.SIGQUIT, _signal_handler) signal.signal(signal.SIGHUP, _signal_handler) + signal.signal(signal.SIGABRT, _signal_handler) + signal.signal(signal.SIGSEGV, _signal_handler) # Register signal handlers on import diff --git a/src/pshmem/shmem.py b/src/pshmem/shmem.py index 0a924de..a3b6324 100644 --- a/src/pshmem/shmem.py +++ b/src/pshmem/shmem.py @@ -12,7 +12,7 @@ from .utils import ( SharedMemory, mpi_data_type, - random_shm_key, + random_shm_name, ) @@ -151,12 +151,9 @@ def __init__(self, shape, dtype, comm, comm_node=None, comm_node_rank=None): self._name = None self._shm_index = None if self._rank == 0: - # Get a random 64bit integer between the supported range of keys - self._shm_index = random_shm_key() # Name, used as global tag. - self._name = f"MPIShared_{self._shm_index}" + self._name = random_shm_name() if self._comm is not None: - self._shm_index = self._comm.bcast(self._shm_index, root=0) self._name = self._comm.bcast(self._name, root=0) # Only allocate our buffers if the total number of elements is > 0 diff --git a/src/pshmem/utils.py b/src/pshmem/utils.py index 72d3a69..4d80788 100644 --- a/src/pshmem/utils.py +++ b/src/pshmem/utils.py @@ -4,7 +4,7 @@ # LICENSE file. ## -import random +import secrets import sys import threading import time @@ -96,21 +96,22 @@ def mpi_data_type(comm, dt): return (dsize, mpitype) -def random_shm_key(): - """Get a random positive integer for using in shared memory naming. +def random_shm_name(): + """Get a random string for use in shared memory naming. - The python random library is used, and seeded with the default source - (either system time or os.urandom). + The naming convention used is similar to the syntax in + multiprocessing.shared_memory.SharedMemory. Returns: - (int): The random integer. + (str): The random name. """ - min_val = 0 - max_val = sys.maxsize - # Seed with default source of randomness - random.seed(a=None) - return random.randint(min_val, max_val) + # Copy the conventions used in python SharedMemory + prefix = "psm_" + rnbytes = 4 # 14 byte limit on BSD, minus prefix, minus slash, minus termination + hex = secrets.token_hex(rnbytes) + name = f"{prefix}{hex}" + return name @contextmanager