Skip to content

repr of threading.RLock is erronous with the _thread module #134322

Closed
@YvesDup

Description

@YvesDup

Bug report

Bug description:

import threading
 
r = threading.RLock()
 print(f"{r = }")

output is:

r = <unlocked _thread.RLock object owner=0 count=1 at 0x105a98720>

The threading.RLock.__repr__ seems to me erronous because when a threading.RLock is just created, its count attribute should be 0.
This error occurs only with the _thread module.

I can submit a PR.

CPython versions tested on:

CPython main branch

Operating systems tested on:

macOS

Linked PRs

Activity

picnixz

picnixz commented on May 20, 2025

@picnixz
Member

Yes, it's because of:

size_t count = self->lock.level + 1;

which doesn't check whether the lock is unlocked or not.

YvesDup

YvesDup commented on May 20, 2025

@YvesDup
ContributorAuthor

Yes, we have to check the lock state as in the rlock_recursion_count function from the _threadmodule.

vstinner

vstinner commented on May 20, 2025

@vstinner
Member

It's a regression introduced in Python 3.14 by commit 67f6e08. cc @kumaraditya303

commit 67f6e08147bc005e460d82fcce85bf5d56009cf5
Author: Kumar Aditya <kumaraditya@python.org>
Date:   Mon Oct 14 14:06:31 2024 +0530

    gh-125139: use `_PyRecursiveMutex` in `_thread.RLock` (#125144)
vstinner

vstinner commented on May 20, 2025

@vstinner
Member

Python 3.13 is not affected:

$ python3.13
Python 3.13.3+ (heads/mp_interrupt13:4d84782ac85, May 12 2025, 11:14:48) [GCC 15.0.1 20250329 (Red Hat 15.0.1-0)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import threading
>>> r=threading.RLock()
>>> r
<unlocked _thread.RLock object owner=0 count=0 at 0x7fb80bfa0d10>
added a commit that references this issue on May 22, 2025

gh-134322: Fix `repr(threading.RLock)` (#134389)

fade146
added a commit that references this issue on May 22, 2025

pythongh-134322: Fix `repr(threading.RLock)` (pythonGH-134389)

added a commit that references this issue on May 22, 2025

[3.14] gh-134322: Fix `repr(threading.RLock)` (GH-134389) (#134528)

068d570
added a commit that references this issue on May 26, 2025

pythongh-134322: Fix `repr(threading.RLock)` (python#134389)

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

    extension-modulesC modules in the Modules dirtype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      `repr` of `threading.RLock` is erronous with the `_thread` module · Issue #134322 · python/cpython

      Follow Lee on X/Twitter - Father, Husband, Serial builder creating AI, crypto, games & web tools. We are friends :) AI Will Come To Life!

      Check out: eBank.nz (Art Generator) | Netwrck.com (AI Tools) | Text-Generator.io (AI API) | BitBank.nz (Crypto AI) | ReadingTime (Kids Reading) | RewordGame | BigMultiplayerChess | WebFiddle | How.nz | Helix AI Assistant