[3.13] gh-98896: resource_tracker: use json&base64 to allow arbitrary shared memory names (GH-138473) (GH-142014)

(cherry picked from commit 64d6bde38f)

Co-authored-by: Stan Ulbrych <89152624+StanFromIreland@users.noreply.github.com>
Co-authored-by: Rani Pinchuk <33353578+rani-pinchuk@users.noreply.github.com>
This commit is contained in:
Miss Islington (bot) 2025-11-28 11:57:51 +01:00 committed by GitHub
parent 3bbd669abb
commit b3a0101e22
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 98 additions and 8 deletions

View file

@ -6789,3 +6789,47 @@ class SemLock(_multiprocessing.SemLock):
name = f'test_semlock_subclass-{os.getpid()}'
s = SemLock(1, 0, 10, name, False)
_multiprocessing.sem_unlink(name)
@unittest.skipUnless(HAS_SHMEM, "requires multiprocessing.shared_memory")
class TestSharedMemoryNames(unittest.TestCase):
def test_that_shared_memory_name_with_colons_has_no_resource_tracker_errors(self):
# Test script that creates and cleans up shared memory with colon in name
test_script = textwrap.dedent("""
import sys
from multiprocessing import shared_memory
import time
# Test various patterns of colons in names
test_names = [
"a:b",
"a:b:c",
"test:name:with:many:colons",
":starts:with:colon",
"ends:with:colon:",
"::double::colons::",
"name\\nwithnewline",
"name-with-trailing-newline\\n",
"\\nname-starts-with-newline",
"colons:and\\nnewlines:mix",
"multi\\nline\\nname",
]
for name in test_names:
try:
shm = shared_memory.SharedMemory(create=True, size=100, name=name)
shm.buf[:5] = b'hello' # Write something to the shared memory
shm.close()
shm.unlink()
except Exception as e:
print(f"Error with name '{name}': {e}", file=sys.stderr)
sys.exit(1)
print("SUCCESS")
""")
rc, out, err = script_helper.assert_python_ok("-c", test_script)
self.assertIn(b"SUCCESS", out)
self.assertNotIn(b"traceback", err.lower(), err)
self.assertNotIn(b"resource_tracker.py", err, err)