Skip to content

Conversation

@rani-pinchuk
Copy link
Contributor

@rani-pinchuk rani-pinchuk commented Sep 3, 2025

Shared memory names containing colons were not parsed correctly as the code of resource_tracker assumed that these names contain no colons.

@encukou

Copy link
Member

@encukou encukou left a comment

Choose a reason for hiding this comment

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

Hello! Sorry for the delay in reviewing.
The fix looks good for colons, but a comment on the issue also mentioned newlines in filenames. Do you want to tackle that, or leave it to another PR?

@rani-pinchuk
Copy link
Contributor Author

rani-pinchuk commented Nov 5, 2025 via email

Copy link
Member

@encukou encukou left a comment

Choose a reason for hiding this comment

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

Thank you!
This approach feels ad-hoc; did you consider a standard encoding like JSON lines or pickle?
(Python's JSON encoder won't output newlines unless you ask for indentation; pickle.load reads one object from a file and stops.)

But I can merge this if you don't want to explore those options.

cmd, name, rtype = line.strip().decode('ascii').split(':')
cmd, enc_name, rtype = line.rstrip(b'\n').decode('ascii').split(':', 2)
if rtype == "shared_memory":
name = base64.urlsafe_b64decode(enc_name.encode('ascii')).decode('utf-8', 'surrogateescape')
Copy link
Member

Choose a reason for hiding this comment

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

Decoding is OK with strings.

Suggested change
name = base64.urlsafe_b64decode(enc_name.encode('ascii')).decode('utf-8', 'surrogateescape')
name = base64.urlsafe_b64decode(enc_name).decode('utf-8', 'surrogateescape')

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for your comment. I like your suggestion to consider using JSON to encode the message.

Also, in the original implementation there was a check that the message is not longer than 512 bytes, since writes shorter than PIPE_BUF (512 bytes on POSIX) are guaranteed to be atomic. But there was no check on the name length.

Now I check that the name is at most 255 bytes long, which is the value of NAME_MAX on Linux (including the leading slash that POSIX requires in shared memory and semaphore names).

I still encode the name using Base64, because json.dumps(..., ensure_ascii=True) would otherwise expand each non-ASCII byte into a 6-character escape like \uDC80. Using Base64 ensures that a 255-byte name becomes at most 340 bytes long, so the total JSON message always remains well below 512 bytes.

As a result, the previous runtime check for the message length is now replaced by an assert.

What do you think?

Copy link
Member

@encukou encukou left a comment

Choose a reason for hiding this comment

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

Hm, good catch about still needing base64 for atomicity.
Hopefully, humans won't need to inspect the stream.

Comment on lines 314 to 316
line = raw.rstrip(b'\n')
try:
obj = json.loads(line.decode('ascii'))
Copy link
Member

Choose a reason for hiding this comment

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

Stripping and decoding shouldn't be needed; json.loads can handle bytes and trailing newlines.

if not isinstance(cmd, str) or not isinstance(rtype, str) or not isinstance(b64, str):
raise ValueError("malformed resource_tracker fields: %r" % (obj,))

enc = b64.encode('ascii')
Copy link
Member

Choose a reason for hiding this comment

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

urlsafe_b64decode can handle bytes as well.

Comment on lines 320 to 322
cmd = obj.get("cmd")
rtype = obj.get("rtype")
b64 = obj.get("base64_name")
Copy link
Member

Choose a reason for hiding this comment

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

You don't need get with constant arguments:

Suggested change
cmd = obj.get("cmd")
rtype = obj.get("rtype")
b64 = obj.get("base64_name")
cmd = obj["cmd"]
rtype = obj["rtype"]
b64 = obj["base64_name"]

Copy link
Member

Choose a reason for hiding this comment

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

Alternately, make some fields optional -- then the PROBE message can be shorter:

Suggested change
cmd = obj.get("cmd")
rtype = obj.get("rtype")
b64 = obj.get("base64_name")
cmd = obj["cmd"]
rtype = obj["rtype"]
b64 = obj.get("base64_name", "")

@encukou encukou added the 🔨 test-with-buildbots Test PR w/ buildbots; report in status section label Nov 11, 2025
@bedevere-bot
Copy link

🤖 New build scheduled with the buildbot fleet by @encukou for commit ea6416e 🤖

Results will be shown at:

https://buildbot.python.org/all/#/grid?branch=refs%2Fpull%2F138473%2Fmerge

If you want to schedule another build, you need to add the 🔨 test-with-buildbots label again.

@bedevere-bot bedevere-bot removed the 🔨 test-with-buildbots Test PR w/ buildbots; report in status section label Nov 11, 2025
@encukou encukou merged commit c6f3dd6 into python:main Nov 12, 2025
120 of 121 checks passed
@encukou
Copy link
Member

encukou commented Nov 12, 2025

Looks good! Thank you for the fix!

@encukou encukou added needs backport to 3.13 bugs and security fixes needs backport to 3.14 bugs and security fixes labels Nov 13, 2025
@miss-islington-app
Copy link

Thanks @rani-pinchuk for the PR, and @encukou for merging it 🌮🎉.. I'm working now to backport this PR to: 3.13.
🐍🍒⛏🤖

@miss-islington-app
Copy link

Thanks @rani-pinchuk for the PR, and @encukou for merging it 🌮🎉.. I'm working now to backport this PR to: 3.14.
🐍🍒⛏🤖

@miss-islington-app

This comment was marked as resolved.

@miss-islington-app

This comment was marked as resolved.

@StanFromIreland StanFromIreland removed needs backport to 3.13 bugs and security fixes needs backport to 3.14 bugs and security fixes labels Nov 24, 2025
@StanFromIreland StanFromIreland added needs backport to 3.13 bugs and security fixes needs backport to 3.14 bugs and security fixes labels Nov 24, 2025
@miss-islington-app

This comment was marked as resolved.

@miss-islington-app

This comment was marked as resolved.

@miss-islington-app

This comment was marked as resolved.

@miss-islington-app

This comment was marked as resolved.

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)

Copy link
Member

Choose a reason for hiding this comment

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

Suggested change

This should have been a double line gap. I'll fix it in the backports.

StanFromIreland pushed a commit to StanFromIreland/cpython that referenced this pull request Nov 24, 2025
…itrary shared memory names (pythonGH-138473)

(cherry picked from commit c6f3dd6)

Co-authored-by: Rani Pinchuk <33353578+rani-pinchuk@users.noreply.github.com>
@bedevere-app
Copy link

bedevere-app bot commented Nov 24, 2025

GH-141922 is a backport of this pull request to the 3.14 branch.

@bedevere-app bedevere-app bot removed the needs backport to 3.14 bugs and security fixes label Nov 24, 2025
encukou pushed a commit that referenced this pull request Nov 27, 2025
… shared memory names (GH-138473) (GH-141922)

Co-authored-by: Rani Pinchuk <33353578+rani-pinchuk@users.noreply.github.com>
miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Nov 27, 2025
…itrary shared memory names (pythonGH-138473) (pythonGH-141922)

(cherry picked from commit 64d6bde38ffb97a6f7c7fee82ba98de81911f1b9)

Co-authored-by: Stan Ulbrych <89152624+StanFromIreland@users.noreply.github.com>
Co-authored-by: Rani Pinchuk <33353578+rani-pinchuk@users.noreply.github.com>
@bedevere-app
Copy link

bedevere-app bot commented Nov 27, 2025

GH-142014 is a backport of this pull request to the 3.13 branch.

@bedevere-app bedevere-app bot removed the needs backport to 3.13 bugs and security fixes label Nov 27, 2025
encukou pushed a commit that referenced this pull request Nov 28, 2025
… shared memory names (GH-138473) (GH-142014)

(cherry picked from commit 64d6bde)

Co-authored-by: Stan Ulbrych <89152624+StanFromIreland@users.noreply.github.com>
Co-authored-by: Rani Pinchuk <33353578+rani-pinchuk@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants