python / python/cpython

ShareableList corrupts multi-byte UTF-8 strings and bytes with trailing nulls

Open
#145,261 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

stdlib topic-multiprocessing type-bug
Dominant language
Python
Stars
77.2k
Forks
35.9k
PR merge metrics
PR metrics pending

Description

Bug report

Bug description:

Issue

ShareableList allocates string slots based on character count instead of UTF-8 byte count, causing corruption for multi-byte characters. Additionally, rstrip(b'\x00') strips legitimate trailing null bytes from bytes values.

Note: The trailing null bytes issue was originally reported in #106939 (July 2023) and documented as a known issue with a workaround. This fix attempts to resolve both that long-standing issue and the newly discovered UTF-8 corruption bug.

Reproducer

from multiprocessing.shared_memory import ShareableList

# String corruption
sl = ShareableList(['0\U00010000\U00010000'])
print(sl[0])  # UnicodeDecodeError
sl.shm.close(); sl.shm.unlink()

# Bytes corruption
sl = ShareableList([b'\x00'])
print(repr(sl[0]))  # b'' instead of b'\x00'
sl.shm.close(); sl.shm.unlink()
CPython versions tested on:

CPython main branch

Operating systems tested on:

No response

Linked PRs
  • gh-145266

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at the multiprocessing.shared_memory.ShareableList entry point and run the two reproducer snippets from the issue. Done means multi-byte strings remain decodable and bytes values preserve trailing null bytes; review linked PR gh-145266 before starting because work is already underway.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
operating-systems
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.