Skip to content

fix(http2): close the client connection when a write fails - #6391

Open
binaryfire wants to merge 1 commit into
swoole:masterfrom
binaryfire:fix/http2-client-failed-write-close
Open

binaryfire wants to merge 1 commit into
swoole:masterfrom
binaryfire:fix/http2-client-failed-write-close

Conversation

@binaryfire

Copy link
Copy Markdown
Member

When a write fails, Coroutine\Http2\Client records the error but keeps the connection. A timeout or cancel can leave part of a frame on the wire. Later sends can then be read as its missing bytes, which corrupts the connection and leaves requests unanswered. The client still reports connected and later send() calls return stream ids, so callers wait out their read timeout. MultiplexClient's next request() times out the same way. The HTTP/1 client already closes on every failed write.

Fix

A failed write in send(), including the queue flush, now closes the connection and keeps the write's error. close() resumes a coroutine waiting in recv(), and that would otherwise replace the error with ECANCELED and then "Coroutine socket close wait".

Closing from a failed write needs three more changes:

  • recv() finishes a close() that had to wait for its read, as Socket::close() expects. Before, the socket stayed open and connect() failed with EISCONN until close() was called again. This also applies when the user calls close() while another coroutine is in recv().
  • recv() and ping() hold the socket, as send(), write(), goaway() and close() already do. A coroutine woken by another coroutine's close() can now finish that close and release the socket while the outer close() is still running.
  • GOAWAY and an invalid SETTINGS_MAX_FRAME_SIZE set their error after close(). A writer resumed by that close records its own error, which replaced the server's GOAWAY code and debug data.

Also, the encoded form body is freed when the write fails, and connect() no longer calls close() a second time after a failed send, which replaced the error with EBADF.

Hypervel impact

Hypervel's Engine\Http\V2\Client wrapper passes send failures through and keeps using the native client, so after a failed send it still reports the connection as open and later requests on it go unanswered. Hypervel's regression test sends a 1 MB POST to a listener that never reads and checks that the client is disconnected after the write times out.

Tests

close_on_send_failure.phpt uses a peer that never reads:

  • a failed write closes the connection and keeps ETIMEDOUT, and the next send() fails with no connection;
  • a coroutine in recv() is woken, the socket is released, and the same client can connect again;
  • a GOAWAY while a write waits closes the connection and keeps the GOAWAY code and debug data.

It fails on master. Without the recv() socket hold, the GOAWAY case segfaults.

Also run: the HTTP/2 client and server suites, swoole/library's MultiplexClientTest, the new test under Valgrind (which also checks that the form body is freed), and a ZTS build with thread support.

When send_all() failed, Coroutine\Http2\Client::send() recorded the error
but kept the connection. A timeout or cancel can leave part of a frame on
the wire, and the server then reads every later frame as the rest of it,
so nothing else on the connection is answered. The client still reported
`connected`, and later send() calls returned stream ids.
MultiplexClient's next request timed out the same way.

A failed write now closes the connection and keeps the write's error.
close() resumes a coroutine waiting in recv(), and that coroutine would
otherwise replace the error with ECANCELED and then CLOSE_WAIT.

The close needs three more changes to be complete and safe:

- recv() finishes a close() that had to wait for its read. Before, the
  socket stayed open and connect() failed with EISCONN until close() was
  called again.
- recv() and ping() hold the socket, as send(), write(), goaway() and
  close() already do. A writer woken by another coroutine's close() now
  finishes that close, which frees the socket while the outer close() is
  still running.
- GOAWAY and an invalid SETTINGS_MAX_FRAME_SIZE now set their error after
  close(). A writer resumed by that close records its own error, which
  replaced the server's GOAWAY code and debug data.

Also free the encoded form body when a send fails, and drop connect()'s
second close() after a failed send, which replaced the error with EBADF.
@binaryfire
binaryfire force-pushed the fix/http2-client-failed-write-close branch from 1392d49 to 3c41e9c Compare November 12, 2026 04:13
@codecov

codecov Bot commented Nov 12, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 84.08%. Comparing base (9cd45e0) to head (3c41e9c).

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #6391   +/-   ##
=======================================
  Coverage   84.08%   84.08%           
=======================================
  Files         115      115           
  Lines       20237    20237           
  Branches     3637     3638    +1     
=======================================
+ Hits        17016    17017    +1     
+ Misses       3221     3220    -1     

☔ 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.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant