Skip to content

add atomic global counter system#1323

Open
Vizonex wants to merge 17 commits intoaio-libs:masterfrom
Vizonex:global-state
Open

add atomic global counter system#1323
Vizonex wants to merge 17 commits intoaio-libs:masterfrom
Vizonex:global-state

Conversation

@Vizonex
Copy link
Copy Markdown
Member

@Vizonex Vizonex commented Apr 21, 2026

What do these changes do?

These changes are based off @devdanzin's second fuzzer for freethreading mode which noted using a global atomic counter system instead of a normal uint64_t value I did however make one small nitpick/change from that original design to just recast (_Atomic(uint64_t)*)&state->global_version which was not in the original suggestion mainly due to my code editor misbehaving on me when I would go to set it into the actual structure.

Are there changes in behavior for the user?

Just bug fixes.

Related issue number

#1321

Checklist

  • I think the code is well written
  • Unit tests for the changes exist
  • Documentation reflects the changes

@Vizonex Vizonex requested a review from asvetlov as a code owner April 21, 2026 23:34
Comment thread tests/isolated/multidict_global_counter.py Fixed
@Vizonex Vizonex requested a review from webknjaz as a code owner April 21, 2026 23:36
@psf-chronographer psf-chronographer Bot added the bot:chronographer:provided There is a change note present in this PR label Apr 21, 2026
@codspeed-hq
Copy link
Copy Markdown

codspeed-hq Bot commented Apr 22, 2026

Merging this PR will not alter performance

✅ 245 untouched benchmarks


Comparing Vizonex:global-state (8bb2038) with master (637ffa9)

Open in CodSpeed

Comment thread multidict/_multilib/state.h Outdated
Comment thread multidict/_multilib/state.h Outdated
Comment thread multidict/_multilib/state.h Outdated
Comment thread setup.py Outdated
Comment thread setup.py Outdated
Comment thread setup.py Outdated
Copy link
Copy Markdown
Member

@bdraco bdraco left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @Vizonex

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR updates multidict’s global version counter to be atomic (supporting CPython free-threading) and adds an isolated regression test to detect lost increments under concurrent mutation.

Changes:

  • Make mod_state.global_version an atomic and update NEXT_VERSION() to use atomic_fetch_add_explicit(..., memory_order_relaxed).
  • Add an isolated free-threading test that asserts the version delta matches the number of concurrent writes.
  • Adjust build configuration to add C11 atomics flags for MSVC via a custom build_ext.

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
multidict/_multilib/state.h Switches the module-level global counter to an atomic and updates increment logic.
tests/isolated/multidict_global_counter.py New isolated regression test for concurrent version increments (free-threaded builds).
tests/test_leaks.py Registers the new isolated script in the isolated-script runner list.
setup.py Introduces custom build_ext to apply platform-specific C compile flags (incl. MSVC C11 atomics).
CHANGES/1328.bugfix.rst Adds a changelog entry for the atomic counter bugfix.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread setup.py
Comment on lines +33 to +36
for flag in BASE_CFLAGS:
# XXX: MSVC Doesn't have a /O3 flag only O2 is possible...
ext.extra_compile_args.append("/O2" if flag == "O3" else f"/{flag}")
else:
Copy link

Copilot AI Apr 22, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MSVC handling currently prefixes BASE_CFLAGS values with / (e.g. O0 -> /O0, g3 -> /g3). These aren't valid MSVC flags (/Od and /Zi//Z7 are the usual equivalents), so Windows builds with MULTIDICT_DEBUG_BUILD=1 will fail. Consider defining a separate MSVC-specific debug/release flag list instead of reusing BASE_CFLAGS verbatim.

Copilot uses AI. Check for mistakes.
Comment thread tests/isolated/multidict_global_counter.py Outdated
Comment thread setup.py
Comment thread tests/isolated/multidict_global_counter.py Dismissed
@bdraco
Copy link
Copy Markdown
Member

bdraco commented Apr 22, 2026

Looks like the tests still need some work

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.

4 participants