Skip to content

Commit

Permalink
gh-96819: multiprocessing.resource_tracker: check if length of pipe w…
Browse files Browse the repository at this point in the history
…rite <= 512 (#96890)

Co-authored-by: Kumar Aditya <[email protected]>
  • Loading branch information
saito828koki and kumaraditya303 authored Oct 3, 2022
1 parent 14d4f68 commit 19ca114
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 2 deletions.
4 changes: 2 additions & 2 deletions Lib/multiprocessing/resource_tracker.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,10 +161,10 @@ def unregister(self, name, rtype):
def _send(self, cmd, name, rtype):
self.ensure_running()
msg = '{0}:{1}:{2}\n'.format(cmd, name, rtype).encode('ascii')
if len(name) > 512:
if len(msg) > 512:
# posix guarantees that writes to a pipe of less than PIPE_BUF
# bytes are atomic, and that PIPE_BUF >= 512
raise ValueError('name too long')
raise ValueError('msg too long')
nbytes = os.write(self._fd, msg)
assert nbytes == len(msg), "nbytes {0:n} but len(msg) {1:n}".format(
nbytes, len(msg))
Expand Down
8 changes: 8 additions & 0 deletions Lib/test/_test_multiprocessing.py
Original file line number Diff line number Diff line change
Expand Up @@ -5432,6 +5432,14 @@ def test_resource_tracker_reused(self):

self.assertTrue(is_resource_tracker_reused)

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


class TestSimpleQueue(unittest.TestCase):

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +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 comments on commit 19ca114

Please sign in to comment.