Skip to content

rework weakref#6916

Merged
youknowone merged 2 commits into
RustPython:mainfrom
youknowone:weakref
Feb 1, 2026
Merged

rework weakref#6916
youknowone merged 2 commits into
RustPython:mainfrom
youknowone:weakref

Conversation

@youknowone

@youknowone youknowone commented Jan 31, 2026

Copy link
Copy Markdown
Member

Summary by CodeRabbit

  • Refactor

    • Reorganized weak reference handling to use an optimized locking mechanism for improved concurrency management.
    • Adjusted internal method visibility to enhance code structure and maintainability.
  • Chores

    • Updated spell-check dictionary with new vocabulary entries.
    • Updated documentation comments for consistency.

✏️ Tip: You can customize this high-level summary in your review settings.

@coderabbitai

coderabbitai Bot commented Jan 31, 2026

Copy link
Copy Markdown
Contributor

Warning

Rate limit exceeded

@youknowone has exceeded the limit for the number of commits that can be reviewed per hour. Please wait 10 minutes and 19 seconds before requesting another review.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

📝 Walkthrough

Walkthrough

This pull request refactors the weakref locking mechanism in the VM from per-object mutex guards to a stripe-based locking approach, inlines the weakref list representation, and adds a direct referent pointer to PyWeak. Additionally, four pointer accessor methods are exposed as public, and a dictionary entry is added for spelling.

Changes

Cohort / File(s) Summary
Spell Check Dictionary
.cspell.dict/cpython.txt
Added "weakrefobject" entry to CPython dictionary for improved lexical coverage.
API Visibility
crates/common/src/linked_list.rs
Elevated visibility of four Pointers<T> accessor methods (get_prev, get_next, set_prev, set_next) from private to pub const fn.
Weakref Locking Refactor
crates/vm/src/object/core.rs
Introduced stripe-based locking mechanism (weakref_lock) replacing per-object mutex; inlined WeakRefList with atomic head/generic cache pointers; added wr_object field to PyWeak for direct referent access; reworked upgrade, is_dead, and drop_inner operations; updated get_weak_references and weak_count signatures to pass owning object context; added unsafe unlink helper for list manipulation.
Documentation Update
crates/vm/src/object/traverse_object.rs
Updated comments describing weak list tracing rationale in PyInner without altering traversal behavior.

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~50 minutes

Poem

🐰 With stripes and locks, the weakrefs dance,
No mutex chains—a pointers' prance!
Cache generic, reference tight,
Dead detection burning bright.

🚥 Pre-merge checks | ✅ 1 | ❌ 2
❌ Failed checks (1 warning, 1 inconclusive)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 71.88% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
Title check ❓ Inconclusive The title 'rework weakref' is vague and generic, using non-descriptive language that doesn't convey meaningful details about the substantial architectural changes in the changeset. Provide a more specific title that highlights the key change, such as 'Replace weakref per-object mutexes with stripe-based locking' or 'Refactor weakref locking mechanism to use stripe locks'.
✅ Passed checks (1 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@github-actions

Copy link
Copy Markdown
Contributor

Code has been automatically formatted

The code in this PR has been formatted using:

  • cargo fmt --all
    Please pull the latest changes before pushing again:
git pull origin weakref

@youknowone youknowone marked this pull request as ready for review February 1, 2026 01:42
Remove heap-allocated WeakListInner (OncePtr<PyMutex<WeakListInner>>).
WeakRefList now holds two inline atomic pointers (head, generic).
PyWeak.parent replaced with wr_object pointing directly to referent.
Add weakref_lock module with AtomicU8 spinlock array for thread safety.
Rewrite upgrade/clear/drop_inner/count/get_weak_references with stripe lock.
Make Pointers methods public in linked_list.rs.
Remove expectedFailure from test_subclass_refs_dont_replace_standard_refs.
@github-actions

github-actions Bot commented Feb 1, 2026

Copy link
Copy Markdown
Contributor

📦 Library Dependencies

The following Lib/ modules were modified. Here are their dependencies:

[x] lib: cpython/Lib/weakref.py
[x] lib: cpython/Lib/_weakrefset.py
[x] test: cpython/Lib/test/test_weakref.py (TODO: 22)

dependencies:

  • weakref

dependent tests: (147 tests)

  • weakref: test_array test_ast test_asyncio test_code test_concurrent_futures test_context test_contextlib test_copy test_deque test_descr test_dict test_exceptions test_file test_fileio test_functools test_generators test_genericalias test_importlib test_inspect test_io test_ipaddress test_itertools test_logging test_memoryview test_mmap test_ordered_dict test_pickle test_picklebuffer test_queue test_re test_scope test_set test_slice test_socket test_sqlite3 test_ssl test_struct test_tempfile test_thread test_threading test_threading_local test_types test_typing test_unittest test_uuid test_weakref test_weakset test_xml_etree
    • asyncio: test_asyncio test_builtin test_contextlib_async test_os test_sys_settrace test_unittest
    • concurrent.futures.process: test_concurrent_futures
    • copy: test_bytes test_codecs test_collections test_coroutines test_csv test_defaultdict test_dictviews test_email test_enum test_fractions test_http_cookies test_opcache test_optparse test_platform test_plistlib test_posix test_site test_statistics test_sysconfig test_tomllib test_xml_dom_minicompat test_zlib
      • dataclasses: test__colorize test_patma test_pprint test_regrtest test_zoneinfo
      • email.generator: test_email
      • http.cookiejar: test_http_cookiejar
      • http.server: test_httpservers test_robotparser test_urllib2_localnet
      • mailbox: test_mailbox
      • smtplib: test_smtplib test_smtpnet
      • tarfile: test_shutil test_tarfile
    • inspect: test_abc test_argparse test_asyncgen test_decimal test_grammar test_ntpath test_operator test_posixpath test_signal test_traceback test_yield_from test_zipimport
      • bdb: test_bdb
      • importlib.metadata: test_importlib
      • rlcompleter: test_rlcompleter
      • trace: test_trace
      • xmlrpc.server: test_docxmlrpc test_xmlrpc
    • multiprocessing: test_concurrent_futures test_fcntl test_multiprocessing_main_handling
    • symtable: test_symtable
    • tempfile: test_bz2 test_cmd_line test_compile test_compileall test_dis test_doctest test_ensurepip test_faulthandler test_filecmp test_fileinput test_importlib test_linecache test_pathlib test_pkg test_pkgutil test_py_compile test_runpy test_selectors test_string_literals test_subprocess test_support test_tabnanny test_threadedtempfile test_urllib test_urllib2 test_urllib_response test_venv test_zipapp test_zipfile test_zipfile64 test_zstd
      • urllib.request: test_urllib2net test_urllibnet

Legend:

  • [+] path exists in CPython
  • [x] up-to-date, [ ] outdated

@youknowone youknowone merged commit 60bec8a into RustPython:main Feb 1, 2026
14 checks passed
@youknowone youknowone deleted the weakref branch February 1, 2026 03:12
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.

1 participant