Skip to content

Make the pure-Python version bump atomic on GIL builds - #1610

Merged
asvetlov merged 2 commits into
aio-libs:masterfrom
asvetlov:pure-version-counter
Sep 27, 2026
Merged

asvetlov merged 2 commits into
aio-libs:masterfrom
asvetlov:pure-version-counter

Conversation

@asvetlov

Copy link
Copy Markdown
Member

What do these changes do?

On GIL builds the pure-Python _incr_version() read, incremented and stored the shared version counter across several bytecodes, so two threads mutating different multidicts at once could be handed the same version. The counter is now an itertools.count, and the GIL build takes next() from it, which is a single C call. The free-threaded build keeps _VERSION_LOCK around the same next(), since itertools.count is not documented as thread-safe there.

test_pure_python_version_thread_safety no longer carries _gil_build_race_skip. It lowers the switch interval to 1e-4 s while its threads run, which fails reliably on PyPy before the fix. CPython 3.10 and 3.14 GIL builds never showed the race, since they only switch threads at calls and backward jumps and the old increment had neither in the window.

The C extension is unaffected; its counter was made atomic in #1446.

Are there changes in behavior for the user?

No, apart from pure-Python versions no longer colliding across threads.

Is it a substantial burden for the maintainers to support this?

No; the change is three lines and makes the increment simpler.

Related issue number

Related to #1605, whose pure-Python GIL-build skip in test_versions_unique_across_threads can be removed once both land.

Checklist

  • I think the code is well written
  • Unit tests for the changes exist
  • Documentation reflects the changes (N/A, no documented behaviour changed)
  • If you provide code modification, please add yourself to CONTRIBUTORS.txt (N/A)
  • Add a new news fragment into the CHANGES/ folder
  • make doc-spelling passes and any new technical words are added to docs/spelling_wordlist.txt

Drafted with Claude Code (Claude Opus 5.5); reviewed by @asvetlov.

Agent run details (optional, for reviewers)

Reproduction, PyPy 3.10.16, test_pure_python_version_thread_safety under pytest-cov: 4/4 failed before the fix, 4/4 passed after, about 4 s per run. Standalone, 16 threads x 2000 __setitem__ at a 1e-4 switch interval gave 59 to 231 duplicate versions per run before, 0 after.

Full suite, each leg in its own venv and tree copy:

  • CPython 3.14.7, C extension: 2792 passed, 170 skipped
  • CPython 3.14.7t, C extension: 2778 passed, 184 skipped
  • CPython 3.10.17, C extension: 2792 passed, 170 skipped
  • CPython 3.14.7, MULTIDICT_NO_EXTENSIONS=1 ... --no-c-extensions: 1321 passed, 16 skipped
  • PyPy 3.10.16, pure Python: 1297 passed, 40 skipped (test_circular_imports needed multidict installed into the venv, then passed)

Lint: pre-commit (ruff, mypy) and make doc-spelling pass.

Instructions per operation, benchmarks/callgrind_driver.py --impl multidict_py --include-multidict-only, CPython 3.14.7 GIL build (no wall-clock run):

operation before after change
add 22743.5 20870.0 -8.24%
add_istr 24220.7 22349.9 -7.72%
popitem 11920.9 10987.7 -7.83%
ctor_items 2476497.0 2287618.6 -7.63%
pop 15420.9 14519.4 -5.85%
setdefault_new 34690.1 32819.2 -5.39%
delitem 19617.6 18723.2 -4.56%
setitem_replace 26166.9 25215.0 -3.64%
setitem_insert 27042.9 26106.1 -3.46%

Read-only operations moved by less than 0.3%.

The GIL-build _incr_version() read, incremented and stored the shared
counter across several bytecodes, so two threads could both hand out
the same version. Draw versions from itertools.count instead, whose
next() is a single C call. Free-threaded builds keep the lock.
@pyba-chronographer pyba-chronographer Bot added the bot:chronographer:provided There is a change note present in this PR label Sep 26, 2026
@asvetlov
asvetlov marked this pull request as ready for review September 26, 2026 21:04
@asvetlov
asvetlov requested a review from webknjaz as a code owner September 26, 2026 21:04
@greptile-apps

greptile-apps Bot commented Sep 26, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerConfidence Score: 5/5

[Medium risk] Fixes a race condition in the pure-Python multidict implementation.

The outstanding test-quality concern is non-blocking.

Reviews (2) · Last reviewed commit: "Say which interpreters the version race ..."

Comment thread tests/test_multidict.py Resolved
@greptile-apps

This comment has been minimized.

@codspeed

codspeed Bot commented Sep 26, 2026 •

Copy link
Copy Markdown

Merging this PR will improve performance by 10.3%

⚡ 19 improved benchmarks
✅ 294 untouched benchmarks

Performance Changes

Benchmark BASE HEAD Efficiency
⚡ test_multidict_add_str[cs-py] 576.1 ms 515.9 ms +11.67%
⚡ test_create_multidict_with_items[cs-py] 1.9 ms 1.7 ms +11.51%
⚡ test_cimultidict_add_istr[py] 582 ms 524.2 ms +11.04%
⚡ test_create_cimultidict_with_items_istr[py] 1.9 ms 1.7 ms +11.03%
⚡ test_multidict_add_str[ci-py] 610.6 ms 550.1 ms +10.98%
⚡ test_create_multidict_with_dict[cs-py] 2 ms 1.8 ms +10.72%
⚡ test_cimultidict_add_str_mixed_case[py] 621.6 ms 561.7 ms +10.66%
⚡ test_create_multidict_with_items[ci-py] 2 ms 1.8 ms +10.65%
⚡ test_create_cimultidict_with_dict_istr[py] 2 ms 1.8 ms +10.58%
⚡ test_create_multidict_with_dict[ci-py] 2.1 ms 1.9 ms +10.47%
⚡ test_create_multidict_with_many_items[ci-py] 108.4 ms 98.3 ms +10.29%
⚡ test_create_multidict_with_items_with_kwargs[cs-py] 4.1 ms 3.8 ms +9.81%
⚡ test_create_multidict_with_many_items[cs-py] 99.6 ms 90.7 ms +9.78%
⚡ test_create_multidict_with_items_with_kwargs[ci-py] 4.5 ms 4.1 ms +9.68%
⚡ test_create_cimultidict_with_items_istr_with_kwargs[py] 4.4 ms 4 ms +9.63%
⚡ test_multidict_insert_str[ci-py] 712.7 ms 650.2 ms +9.62%
⚡ test_multidict_extend_str[ci-py] 478.9 ms 437.8 ms +9.38%
⚡ test_multidict_extend_str[cs-py] 437.9 ms 400.9 ms +9.24%
⚡ test_cimultidict_extend_istr[py] 444.7 ms 407.6 ms +9.09%

Tip

Curious why performance improved? Comment @codspeedbot explain why performance improved on this PR, or directly use the CodSpeed MCP with your agent.


Comparing asvetlov:pure-version-counter (a1c2130) with master (0a4d2e0)

Open in CodSpeed

@codecov

codecov Bot commented Sep 26, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (8e5c14c) to head (a1c2130).
⚠️ Report is 3 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff            @@
##            master     #1610   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           43        43           
  Lines         7940      7987   +47     
  Branches       663       669    +6     
=========================================
+ Hits          7940      7987   +47     
Flag Coverage Δ
CI-GHA 100.00% <100.00%> (ø)
pytest 100.00% <100.00%> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

CPython GIL builds only switch threads at calls and backward jumps, so
they never interleave inside the old increment; only PyPy does.
@asvetlov
asvetlov merged commit 200e9d4 into aio-libs:master Sep 27, 2026
62 checks passed
@asvetlov
asvetlov deleted the pure-version-counter branch September 27, 2026 10:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bot:chronographer:provided There is a change note present in this PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant