Skip to content

Fix test_data_file race condition on Python 3.14 free-threaded#12170

Merged
Dreamsorcerer merged 4 commits intoaio-libs:masterfrom
rodrigobnogueira:fix-test-data-file-3.14t
Feb 28, 2026
Merged

Fix test_data_file race condition on Python 3.14 free-threaded#12170
Dreamsorcerer merged 4 commits intoaio-libs:masterfrom
rodrigobnogueira:fix-test-data-file-3.14t

Conversation

@rodrigobnogueira
Copy link
Member

@rodrigobnogueira rodrigobnogueira commented Feb 28, 2026

What do these changes do?

This PR fixes a flaky test (test_data_file) that fails on Python 3.14 free-threaded (3.14t) builds.

The test asserts asyncio.isfuture(req._writer) immediately after await req._send(conn). However, on Python 3.12+, asyncio.Task accepts eager_start=True. Because the 2-byte file payload in this test is exceptionally small, the eagerly-started task completes synchronously during _send().

When task.done() handles this, req._writer is left as None, causing the assertion to fail (e.g. assert False + where False = asyncio.isfuture(None)).

This PR applies the exact same mock strategy used by the sibling test test_data_stream. We mock the _write_bytes method to execute await asyncio.sleep(0), guaranteeing the task yields back to the event loop. This ensures req._writer holds a pending Future when the assertion fires.

The fixed test has failed in PRs #12089 and #12153, for example.

Are there changes in behavior for the user?

No user-facing changes. This strictly addresses test flakiness in the CI environment.

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

No. This follows the exact same pattern already used in the same test file.

Related issue number

N/A.

Checklist

  • I think the code is well written
  • Unit tests for the changes exist
  • Documentation reflects the changes
  • If you provide code modification, please add yourself to CONTRIBUTORS.txt
  • Add a new news fragment into the CHANGES/ folder

@psf-chronographer psf-chronographer bot added the bot:chronographer:provided There is a change note present in this PR label Feb 28, 2026
@codecov
Copy link

codecov bot commented Feb 28, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 98.78%. Comparing base (c1981ef) to head (eace48d).
⚠️ Report is 2 commits behind head on master.
✅ All tests successful. No failed tests found.

Additional details and impacted files
@@           Coverage Diff           @@
##           master   #12170   +/-   ##
=======================================
  Coverage   98.77%   98.78%           
=======================================
  Files         128      128           
  Lines       45321    45326    +5     
  Branches     2405     2405           
=======================================
+ Hits        44768    44776    +8     
+ Misses        393      390    -3     
  Partials      160      160           
Flag Coverage Δ
CI-GHA 98.64% <100.00%> (+<0.01%) ⬆️
OS-Linux 98.38% <100.00%> (+<0.01%) ⬆️
OS-Windows 96.75% <100.00%> (+<0.01%) ⬆️
OS-macOS 97.63% <100.00%> (+<0.01%) ⬆️
Py-3.10.11 97.18% <100.00%> (+<0.01%) ⬆️
Py-3.10.19 97.67% <100.00%> (+<0.01%) ⬆️
Py-3.11.14 97.86% <100.00%> (+<0.01%) ⬆️
Py-3.11.9 97.39% <100.00%> (+<0.01%) ⬆️
Py-3.12.10 97.48% <100.00%> (-0.01%) ⬇️
Py-3.12.12 97.95% <100.00%> (+<0.01%) ⬆️
Py-3.13.12 98.20% <100.00%> (+<0.01%) ⬆️
Py-3.14.3 98.16% <100.00%> (+<0.01%) ⬆️
Py-3.14.3t 97.26% <100.00%> (+<0.01%) ⬆️
Py-pypy3.11.13-7.3.20 97.29% <100.00%> (+<0.01%) ⬆️
VM-macos 97.63% <100.00%> (+<0.01%) ⬆️
VM-ubuntu 98.38% <100.00%> (+<0.01%) ⬆️
VM-windows 96.75% <100.00%> (+<0.01%) ⬆️

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

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

@codspeed-hq
Copy link

codspeed-hq bot commented Feb 28, 2026

Merging this PR will not alter performance

✅ 59 untouched benchmarks


Comparing rodrigobnogueira:fix-test-data-file-3.14t (eace48d) with master (c1981ef)

Open in CodSpeed

@Dreamsorcerer Dreamsorcerer added backport-3.13 Trigger automatic backporting to the 3.13 release branch by Patchback robot backport-3.14 Trigger automatic backporting to the 3.14 release branch by Patchback robot labels Feb 28, 2026
@Dreamsorcerer Dreamsorcerer merged commit 2d03bc4 into aio-libs:master Feb 28, 2026
45 of 46 checks passed
@patchback
Copy link
Contributor

patchback bot commented Feb 28, 2026

Backport to 3.13: 💔 cherry-picking failed — conflicts found

❌ Failed to cleanly apply 2d03bc4 on top of patchback/backports/3.13/2d03bc45722f4153c2a993f084ecac04023d54c4/pr-12170

Backporting merged PR #12170 into master

  1. Ensure you have a local repo clone of your fork. Unless you cloned it
    from the upstream, this would be your origin remote.
  2. Make sure you have an upstream repo added as a remote too. In these
    instructions you'll refer to it by the name upstream. If you don't
    have it, here's how you can add it:
    $ git remote add upstream https://github.com/aio-libs/aiohttp.git
  3. Ensure you have the latest copy of upstream and prepare a branch
    that will hold the backported code:
    $ git fetch upstream
    $ git checkout -b patchback/backports/3.13/2d03bc45722f4153c2a993f084ecac04023d54c4/pr-12170 upstream/3.13
  4. Now, cherry-pick PR Fix test_data_file race condition on Python 3.14 free-threaded #12170 contents into that branch:
    $ git cherry-pick -x 2d03bc45722f4153c2a993f084ecac04023d54c4
    If it'll yell at you with something like fatal: Commit 2d03bc45722f4153c2a993f084ecac04023d54c4 is a merge but no -m option was given., add -m 1 as follows instead:
    $ git cherry-pick -m1 -x 2d03bc45722f4153c2a993f084ecac04023d54c4
  5. At this point, you'll probably encounter some merge conflicts. You must
    resolve them in to preserve the patch from PR Fix test_data_file race condition on Python 3.14 free-threaded #12170 as close to the
    original as possible.
  6. Push this branch to your fork on GitHub:
    $ git push origin patchback/backports/3.13/2d03bc45722f4153c2a993f084ecac04023d54c4/pr-12170
  7. Create a PR, ensure that the CI is green. If it's not — update it so that
    the tests and any other checks pass. This is it!
    Now relax and wait for the maintainers to process your pull request
    when they have some cycles to do reviews. Don't worry — they'll tell you if
    any improvements are necessary when the time comes!

🤖 @patchback
I'm built with octomachinery and
my source is open — https://github.com/sanitizers/patchback-github-app.

@patchback
Copy link
Contributor

patchback bot commented Feb 28, 2026

Backport to 3.14: 💔 cherry-picking failed — conflicts found

❌ Failed to cleanly apply 2d03bc4 on top of patchback/backports/3.14/2d03bc45722f4153c2a993f084ecac04023d54c4/pr-12170

Backporting merged PR #12170 into master

  1. Ensure you have a local repo clone of your fork. Unless you cloned it
    from the upstream, this would be your origin remote.
  2. Make sure you have an upstream repo added as a remote too. In these
    instructions you'll refer to it by the name upstream. If you don't
    have it, here's how you can add it:
    $ git remote add upstream https://github.com/aio-libs/aiohttp.git
  3. Ensure you have the latest copy of upstream and prepare a branch
    that will hold the backported code:
    $ git fetch upstream
    $ git checkout -b patchback/backports/3.14/2d03bc45722f4153c2a993f084ecac04023d54c4/pr-12170 upstream/3.14
  4. Now, cherry-pick PR Fix test_data_file race condition on Python 3.14 free-threaded #12170 contents into that branch:
    $ git cherry-pick -x 2d03bc45722f4153c2a993f084ecac04023d54c4
    If it'll yell at you with something like fatal: Commit 2d03bc45722f4153c2a993f084ecac04023d54c4 is a merge but no -m option was given., add -m 1 as follows instead:
    $ git cherry-pick -m1 -x 2d03bc45722f4153c2a993f084ecac04023d54c4
  5. At this point, you'll probably encounter some merge conflicts. You must
    resolve them in to preserve the patch from PR Fix test_data_file race condition on Python 3.14 free-threaded #12170 as close to the
    original as possible.
  6. Push this branch to your fork on GitHub:
    $ git push origin patchback/backports/3.14/2d03bc45722f4153c2a993f084ecac04023d54c4/pr-12170
  7. Create a PR, ensure that the CI is green. If it's not — update it so that
    the tests and any other checks pass. This is it!
    Now relax and wait for the maintainers to process your pull request
    when they have some cycles to do reviews. Don't worry — they'll tell you if
    any improvements are necessary when the time comes!

🤖 @patchback
I'm built with octomachinery and
my source is open — https://github.com/sanitizers/patchback-github-app.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backport-3.13 Trigger automatic backporting to the 3.13 release branch by Patchback robot backport-3.14 Trigger automatic backporting to the 3.14 release branch by Patchback robot 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.

2 participants