GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — str.maketrans on a mutating dict is not thread-safe · Issue #145142 · python/cpython

str.maketrans on a mutating dict is not thread-safe #145142

New issue
Copy link
New issue
Copy link
Closed
Closed
str.maketrans on a mutating dict is not thread-safe#145142
Copy link
Labels
easyinterpreter-core(Objects, Python, Grammar, and Parser dirs)(Objects, Python, Grammar, and Parser dirs)topic-free-threadingtype-crashA hard crash of the interpreter, possibly with a core dumpA hard crash of the interpreter, possibly with a core dump

Description

@eendebakpt
eendebakpt
opened on Feb 23, 2026
Issue body actions

Crash report

What happened?

A reproducer (in the format of Lib/tests/test_free_threading/xxx.py):

import unittest
from test.support import threading_helper

threading_helper.requires_working_threading(module=True)


class ItertoolsThreading(unittest.TestCase):

    @threading_helper.reap_threads
    def test_maketrans(self):
        number_of_iterations = 10
        number_of_attempts=100
        for _ in range(number_of_iterations):
            print(f'iteration {_}')
            d = {2000: 'a'}
            def work_iterator(dct):
                for idx in range(number_of_attempts):
                    str.maketrans(dct)
                    idx = 2000 + idx # outside range of small ints
                    dct[idx] = chr(idx%16)
                    dct.pop(idx, None)
            threading_helper.run_concurrently(work_iterator, nthreads=10, args=[d])

if __name__ == "__main__":
    unittest.main()
    print('done')

At first glance it seems that the iteration using PyDict_Next is not safe (but maybe there are more unsafe parts):

cpython/Objects/unicodeobject.c

Line 13158 in 24cc998

while (PyDict_Next(x, &i, &key, &value)) {

(found when reviewing #145129)

CPython versions tested on:

CPython main branch

Operating systems tested on:

Windows

Output from running 'python -VV' on the command line:

No response

Linked PRs

  • gh-145142: Make str.maketrans safe under free-threading #145157
  • [3.14] gh-145142: Make str.maketrans safe under free-threading (gh-145157) #145320

Activity

  1. eendebakpt
    added
    type-crashA hard crash of the interpreter, possibly with a core dumpA hard crash of the interpreter, possibly with a core dump
    interpreter-core(Objects, Python, Grammar, and Parser dirs)(Objects, Python, Grammar, and Parser dirs)
    topic-free-threading
    on Feb 23, 2026
  2. colesbury commented on Feb 23, 2026

    @colesbury
    colesbury
    on Feb 23, 2026
    Contributor
    More actions

    Yes, PyDict_Next isn't thread-safe if there are concurrent modifications of the dict. We should wrap the loop with a critical section on x here.

    Do you want to make the change?

  3. eendebakpt commented on Feb 23, 2026

    @eendebakpt
    eendebakpt
    on Feb 23, 2026
    ContributorAuthor
    More actions

    Do you want to make the change?

    I am adding the easy label for newcomers. If not picked up, I will make the change.

    Reacted by Sam Gross
  4. eendebakpt
    added
    easy
    on Feb 23, 2026
  5. VanshAgarwal24036 commented on Feb 23, 2026

    @VanshAgarwal24036
    VanshAgarwal24036
    on Feb 23, 2026
    Contributor
    More actions

    I am working on this issue.

  6. VanshAgarwal24036
    added 5 commits that reference this issue on Feb 23, 2026

    pythongh-145142: Make str.maketrans safe under free-threading

    3bedfff

    Merge branch 'main' into pythongh-145142-dict-next-critical-section

    d487864

    Merge remote-tracking branch 'upstream/main' into pythongh-145142-dic…

    ca3b943

    Merge branch 'main' into pythongh-145142-dict-next-critical-section

    206f6b2

    pythongh-145142: Make str.maketrans safe under free-threading

    0745e41
  7. colesbury
    added 2 commits that reference this issue on Feb 27, 2026

    Merge branch 'main' into pythongh-145142-dict-next-critical-section

    4994663

    gh-145142: Make str.maketrans safe under free-threading (gh-145157)

    a249795
  8. miss-islington
    added a commit that references this issue on Feb 27, 2026

    pythongh-145142: Make str.maketrans safe under free-threading (python…

    0efb6a2
  9. colesbury
    added a commit that references this issue on Feb 27, 2026

    [3.14] gh-145142: Make str.maketrans safe under free-threading (gh-14…

    d76c56e
  10. colesbury
    closed this as completedon Feb 27, 2026
  11. brijkapadia
    added a commit that references this issue on Feb 28, 2026

    pythongh-145142: Make str.maketrans safe under free-threading (python…

    bbcb932
  12. ljfp
    added a commit that references this issue on Apr 25, 2026

    pythongh-145142: Make str.maketrans safe under free-threading (python…

    1f6c471
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    easyinterpreter-core(Objects, Python, Grammar, and Parser dirs)(Objects, Python, Grammar, and Parser dirs)topic-free-threadingtype-crashA hard crash of the interpreter, possibly with a core dumpA hard crash of the interpreter, possibly with a core dump

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402503 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.