Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion Lib/multiprocessing/resource_tracker.py
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,12 @@ def main(fd):
with open(fd, 'rb') as f:
for line in f:
try:
cmd, name, rtype = line.strip().decode('ascii').split(':')
parts = line.strip().decode('ascii').split(':')
if len(parts) < 3:
raise ValueError("malformed resource_tracker message: %r" % (parts,))
cmd = parts[0]
rtype = parts[-1]
name = ':'.join(parts[1:-1])
Comment on lines +288 to +293
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about this?

Suggested change
parts = line.strip().decode('ascii').split(':')
if len(parts) < 3:
raise ValueError("malformed resource_tracker message: %r" % (parts,))
cmd = parts[0]
rtype = parts[-1]
name = ':'.join(parts[1:-1])
cmd, *name_parts, rtype = line.strip().decode('ascii').split(':')
name = ':'.join(name_parts)

cleanup_func = _CLEANUP_FUNCS.get(rtype, None)
if cleanup_func is None:
raise ValueError(
Expand Down
38 changes: 38 additions & 0 deletions Lib/test/_test_multiprocessing.py
Original file line number Diff line number Diff line change
Expand Up @@ -7335,3 +7335,41 @@ def test_forkpty(self):
res = assert_python_failure("-c", code, PYTHONWARNINGS='error')
self.assertIn(b'DeprecationWarning', res.err)
self.assertIn(b'is multi-threaded, use of forkpty() may lead to deadlocks in the child', res.err)

@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::",
]

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 = 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)
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix a failure in multiprocessing resource_tracker when SharedMemory names contain colons.
Patch by Rani Pinchuk.
Loading