Skip to content

Commit c2d3f73

Browse files
gh-96819: multiprocessing.resource_tracker: check if length of pipe write <= 512 (GH-96890)
Co-authored-by: Kumar Aditya <59607654+kumaraditya303@users.noreply.github.com> (cherry picked from commit 19ca114) Co-authored-by: Koki Saito <49419225+saito828koki@users.noreply.github.com>
1 parent 72d445a commit c2d3f73

File tree

3 files changed

+11
-2
lines changed

3 files changed

+11
-2
lines changed

Lib/multiprocessing/resource_tracker.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -161,10 +161,10 @@ def unregister(self, name, rtype):
161161
def _send(self, cmd, name, rtype):
162162
self.ensure_running()
163163
msg = '{0}:{1}:{2}\n'.format(cmd, name, rtype).encode('ascii')
164-
if len(name) > 512:
164+
if len(msg) > 512:
165165
# posix guarantees that writes to a pipe of less than PIPE_BUF
166166
# bytes are atomic, and that PIPE_BUF >= 512
167-
raise ValueError('name too long')
167+
raise ValueError('msg too long')
168168
nbytes = os.write(self._fd, msg)
169169
assert nbytes == len(msg), "nbytes {0:n} but len(msg) {1:n}".format(
170170
nbytes, len(msg))

Lib/test/_test_multiprocessing.py

+8
Original file line numberDiff line numberDiff line change
@@ -5377,6 +5377,14 @@ def test_resource_tracker_reused(self):
53775377

53785378
self.assertTrue(is_resource_tracker_reused)
53795379

5380+
def test_too_long_name_resource(self):
5381+
# gh-96819: Resource names that will make the length of a write to a pipe
5382+
# greater than PIPE_BUF are not allowed
5383+
rtype = "shared_memory"
5384+
too_long_name_resource = "a" * (512 - len(rtype))
5385+
with self.assertRaises(ValueError):
5386+
resource_tracker.register(too_long_name_resource, rtype)
5387+
53805388

53815389
class TestSimpleQueue(unittest.TestCase):
53825390

Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fixed check in :mod:`multiprocessing.resource_tracker` that guarantees that the length of a write to a pipe is not greater than ``PIPE_BUF``.

0 commit comments

Comments
 (0)