Skip to content

[bot] Fix flaky coverage of the closed check in the client WebSocket heartbeat - #14433

Draft
Dreamsorcerer wants to merge 2 commits into
masterfrom
bot-flaky-cov-client-ws-heartbeat-after-close
Draft

Dreamsorcerer wants to merge 2 commits into
masterfrom
bot-flaky-cov-client-ws-heartbeat-after-close

Conversation

@Dreamsorcerer

Copy link
Copy Markdown
Member

This is from an agent via my MCP server, a human will verify it soon.


What do these changes do?

if self._closed: return in ClientWebSocketResponse._handle_ping_pong_exception (aiohttp/client_ws.py:217-218) is only covered on some nightly runs, so Codecov flips by 2 lines on commits that can't change coverage (e.g. 2091405, 0ae5b96, b2350c3, b60c612). Per-flag data shows only the Py-pypy-3.12 job ever hits it.

After a clean close(), the server's CLOSE frame, like any received data, re-arms the heartbeat (_on_data_received → _reset_heartbeat doesn't check _closed). test_close_websocket_while_ping_inflight and test_heartbeat_does_not_timeout_while_receiving_large_frame end straight after such a close with heartbeat=0.1, so the stale heartbeat only fires, and its failed PING reaches the guard, if fixture teardown takes over 100 ms. On PyPy the pure-Python coverage tracer makes pytest-cov's per-test context switch slow enough for that on some runs; on CPython it never happens.

This adds test_heartbeat_after_close, which waits for that heartbeat (and its PONG timeout) after a close and checks the close code and exception are left alone, so the guard is covered on every run.

The send_frame assertion relies on the re-arm, which is the client-side counterpart of #14125. If that is fixed for the client too, this test should be revisited with it.

Are there changes in behavior for the user?

No, test-only.

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

No.

Related issue number

N/A (related: #14125)

Checklist

  • I think the code is well written
  • Unit tests for the changes exist
  • Documentation reflects the changes (N/A)
  • If you provide code modifications, please add yourself to CONTRIBUTORS.txt (N/A)
  • Add a new news fragment into the CHANGES/ folder
Local verification
  • Original flake reproduced on PyPy 3.12 (both client WS test files, new test deselected): line 218 hit in 5/10 runs, always from test_close_websocket_while_ping_inflight|teardown. On CPython 3.11/3.13 it was missed in every run.
  • New test with coverage, fresh data file per run, on CPython 3.11–3.15, 3.15t and PyPy 3.12 (30–50 runs each, also under CPU load): passed, with lines 217–218 and the 217→218 arc covered every time.
  • tests/test_client_ws.py tests/test_client_ws_functional.py: 105 passed on CPython 3.11–3.13; 103 passed, 2 skipped (isal) on 3.14, 3.15, 3.15t and PyPy.
  • With the guard removed, the new test fails (assert 1006 == 1000).
  • black, isort, flake8, mypy: clean.

@pyba-chronographer pyba-chronographer Bot added the bot:chronographer:provided There is a change note present in this PR label Nov 22, 2026
@codecov

codecov Bot commented Nov 22, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.12%. Comparing base (b60c612) to head (98a61ed).
✅ All tests successful. No failed tests found.

Additional details and impacted files
@@           Coverage Diff           @@
##           master   #14433   +/-   ##
=======================================
  Coverage   99.12%   99.12%           
=======================================
  Files         135      135           
  Lines       53661    53678   +17     
  Branches     2737     2737           
=======================================
+ Hits        53191    53210   +19     
+ Misses        356      355    -1     
+ Partials      114      113    -1     
Flag Coverage Δ
Autobahn 21.65% <5.88%> (-0.01%) ⬇️
CI-GHA 99.05% <100.00%> (+<0.01%) ⬆️
OS-Linux 98.73% <100.00%> (+<0.01%) ⬆️
OS-Windows 97.49% <100.00%> (+<0.01%) ⬆️
OS-macOS 98.35% <100.00%> (+<0.01%) ⬆️
Py-3.11 98.30% <100.00%> (+<0.01%) ⬆️
Py-3.12 98.38% <100.00%> (-0.01%) ⬇️
Py-3.13 98.38% <100.00%> (+0.01%) ⬆️
Py-3.14 98.44% <100.00%> (+0.01%) ⬆️
Py-3.15 98.43% <100.00%> (-0.01%) ⬇️
Py-3.15t 98.28% <100.00%> (+<0.01%) ⬆️
Py-pypy-3.12 96.95% <100.00%> (-0.01%) ⬇️
VM-macos-latest 98.35% <100.00%> (+<0.01%) ⬆️
VM-ubuntu-latest 98.73% <100.00%> (+<0.01%) ⬆️
VM-windows-latest 97.49% <100.00%> (+<0.01%) ⬆️
cython-coverage 83.94% <ø> (ø)

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.

@codspeed

codspeed Bot commented Nov 22, 2026

Copy link
Copy Markdown

Merging this PR will not alter performance

1 warning

⚠️ 49 benchmarks are dominated by system calls

System calls cannot be simulated consistently and are left out of the measure. The reported value understates the real cost. Switch to the Walltime instrument to measure them.

Learn more about system calls

See affected benchmarks

✅ 102 untouched benchmarks
⏩ 83 skipped benchmarks1


Comparing bot-flaky-cov-client-ws-heartbeat-after-close (98a61ed) with master (b60c612)

Open in CodSpeed

Footnotes

  1. 83 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports. ↩

This branch has not been deployed

No deployments
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