Skip to content

fix(utilities): a concurrent first import on free-threaded 3.14t still keeps a layer raw for 'dictionary changed size during iteration' (#3243) - #3245

Merged
JarryShaw merged 1 commit into
developfrom
fix/3243-free-threaded-first-import-race
Nov 30, 2026
Merged

JarryShaw merged 1 commit into
developfrom
fix/3243-free-threaded-first-import-race

Conversation

@JarryShaw

Copy link
Copy Markdown
Owner

Please follow the guide below

  • Put an x into every box [ ] that applies to your pull request (like this: [x])

  • Use the Preview tab to see how your pull request will look

  • Searched for similar pull requests

  • Followed the coding style (make pylint, make mypy, make isort)

  • make test passes, and a test case covers the change

  • Described the user-visible change below and left its changelog entry to the maintainer, keeping docs/source/changelog/ and CHANGELOG.md out of the diff, if the change is user-visible: N/A — centralised

What is the purpose of your pull request?

Tick the commit type your subject line carries.

  • fix — corrects a defect

  • feat — adds a feature

  • perf — changes performance, not behaviour

  • refactor — changes neither behaviour nor performance

  • test — tests only

  • docs — documentation only

  • ci — workflows or build tooling

  • release — bumps the version or rolls up a distribution

  • chore — anything else

  • Also tick if it regenerates constant or registry tables, members keeping their numeric values (the const label)


Description of your pull request and other information

Reproduced on a local free-threaded 3.14.10 (1 of 200 helper trials under load). The traceback runs from TCP's next layer through HTTPv1.read and _freeze to the first OrderedMultiInfo(...), then Info.__new__ → info_final → dir(obj). A class left unfinalised at definition is finalised by its first construction, which walks dir() over the MRO and then writes the class's namespace. Two threads building it at once each walk while the other writes. Schema.__new__ does the same.

  • Info.__new__ and Schema.__new__ now finalise under import_lock and re-read the state once they hold it. Every first import and nested-type store (fix(utilities): a concurrent first import can raise 'dictionary changed size during iteration' on PyPy #2870) already holds that lock, so no namespace is written while another thread walks it, and a class is finalised once. A finalised class takes no lock. No module finalises a class on import (none of 932), so the lock cannot wait inside an import.
  • tests/corekit/test_first_construction_threads_3243_unit.py pauses one thread's walk while a second first construction runs, for Info and Schema. Both fail on an origin/develop export with RuntimeError('dictionary changed size during iteration'), on 3.14 and 3.14t, and pass with the fix.
  • Stress on 3.14t, 16 threads racing fresh classes' first construction with no patching: develop raises in 158 of 900 trials, the fix in 0 of 900. The 2771, 2870 and 3243 thread modules pass 20 runs of 20 on 3.14t.

tests/corekit, tests/protocols/schema, tests/utilities, tests/project (bar test_conda_dev_release_gate_2458, which fails the same on the develop export), tests/protocols/*.py, tests/*.py and the Sphinx-gated modules pass. pylint, mypy and isort raise nothing new.

Closes #3243

@github-actions github-actions Bot added this to the PyPCAPKit v1.10 milestone Nov 30, 2026
@JarryShaw JarryShaw moved this to In review in PyPCAPKit Nov 30, 2026
@JarryShaw JarryShaw added bug Issues reporting a defect (set by the bug report template; a default, not an assessment) review: running A cross-review is in flight against the current head - no verdict yet labels Nov 30, 2026
@read-the-docs-community

read-the-docs-community Bot commented Nov 30, 2026 •

Copy link
Copy Markdown

…l keeps a layer raw for 'dictionary changed size during iteration' (#3243)

- infoclass, schema: a class left unfinalised at definition, such as
  OrderedMultiInfo, is finalised by its first construction, which walks
  dir() over the MRO and then writes the class's namespace. Two threads
  constructing it at once each walk while the other writes, which raises
  on free-threaded CPython; Info.__new__ and Schema.__new__ now finalise
  under import_lock and re-read the state once they hold it.
- importlib: import_lock's docs name the new holder.
- tests: a thread's walk paused part way through while a second first
  construction runs, for Info and for Schema.

tests/corekit, tests/protocols/schema, tests/utilities, tests/project,
tests/protocols/*.py and tests/*.py pass, bar a conda gate test that
fails the same on develop.
@JarryShaw
JarryShaw force-pushed the fix/3243-free-threaded-first-import-race branch from ef2b3f3 to cb3ba7b Compare November 30, 2026 06:27
@JarryShaw

Copy link
Copy Markdown
Owner Author

Good to go at cb3ba7be9, from a Sonnet cross-review and my check of the nit fix.

  • The race is real. On a develop export, both new tests fail on 3.14 and 3.14t with RuntimeError('dictionary changed size during iteration'). The reviewer's 8-thread stress on 3.14t without the GIL gave 13 Info and 14 Schema errors in 2000 rounds on develop, and 0 on this PR.
  • The fix is complete. No writer of a walked namespace is left outside import_lock, and __finalised__ is written last, so the lock-free fast path is safe. Importing all 933 modules finalises no class, so there is no path to a deadlock.
  • The cost is small. It falls only on bare Info() and Schema() (+250–420 ns), which pcapkit never builds. Finalised classes are within noise. The worker kept that cost rather than lose coverage of the early returns.
  • Tests pass: tests/corekit, schema and utilities (1086), the top-level and root suites (1008) and tests/project (2435). The 2771 test passed 20 of 20 runs on 3.14t without the GIL.
  • The nit fix: import_lock's doc comment is now 8 lines, and the Info/Schema.__new__ paragraphs state what holds rather than history. Prose only, and still one commit on 07f7bc8ff.

@JarryShaw JarryShaw added review: good-to-go Cross-review at the current head says ready; CI state is separate and removed review: running A cross-review is in flight against the current head - no verdict yet labels Nov 30, 2026
@github-actions

Copy link
Copy Markdown
Contributor

Coverage: 94.98% (unit tier, Python 3.15, cb3ba7be9, Unit Tests run success)

Package Statements Missed Branches Partial Cover
pcapkit (top level) 116 4 20 2 95.59%
pcapkit/const 22386 224 3186 191 98.28%
pcapkit/corekit 3162 83 1060 40 96.85%
pcapkit/dumpkit 423 3 148 5 98.60%
pcapkit/foundation 4350 24 1364 26 99.12%
pcapkit/interface 112 4 40 3 95.39%
pcapkit/protocols 28995 154 7682 128 99.22%
pcapkit/toolkit 1273 35 470 9 96.67%
pcapkit/utilities 639 7 176 5 98.53%
pcapkit/vendor 6575 2396 1478 258 60.24%

Per-file detail: the coverage-html artifact of this run.

@JarryShaw
JarryShaw merged commit 29780fe into develop Nov 30, 2026
73 checks passed
@github-project-automation github-project-automation Bot moved this from In review to Done in PyPCAPKit Nov 30, 2026
@JarryShaw
JarryShaw deleted the fix/3243-free-threaded-first-import-race branch November 30, 2026 14:04
@JarryShaw JarryShaw removed the review: good-to-go Cross-review at the current head says ready; CI state is separate label Nov 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Issues reporting a defect (set by the bug report template; a default, not an assessment)

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

fix(utilities): a concurrent first import on free-threaded 3.14t still keeps a layer raw for 'dictionary changed size during iteration'

1 participant