Skip to content

fix event-based connection shutdown and add a test for it - #22304

Closed
GrahamCampbell wants to merge 5 commits into
curl:masterfrom
GrahamCampbell:event-shutdown-fix
Closed

GrahamCampbell wants to merge 5 commits into
curl:masterfrom
GrahamCampbell:event-shutdown-fix

Conversation

@GrahamCampbell

Copy link
Copy Markdown
Contributor

This builds on @icing's fix from #22284, with @pszemus's analysis in #22282, and adds a follow-up commit with two small additional things I noticed. The pollset predicate now reads the connection's own shutdown state rather than going through data->conn, and the EXPIRE_SHUTDOWN arming in cshutdn_perform() works again: next_expire_ms started at 0 and could never be set, so a peer that never sends a close_notify would still park its connection forever, timeout or not.

I also wanted this covered by a test, which turned out to be fun: the curl tool cannot show the bug at all, since it runs the multi's admin handle eagerly enough to rescue parked connections. So the new cli_ev_download client drives transfers through a strict curl_multi_socket_action() loop like a real event application would, and test_19_08 checks that shutdown sockets stay with the socket callback until done. It fails on master without the fix and passes with it.

Copilot AI left a comment

Copy link
Copy Markdown

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 fixes a regression in libcurl’s event/socket-action API where connections in graceful shutdown could stop being progressed (leading to leaked fds/memory), and adds a focused event-driven client + HTTP test to prevent regressions.

Changes:

  • Ensure connection filter pollset adjustment is performed when a connection has entered shutdown, so shutdown sockets remain registered with the app’s socket callback.
  • Fix shutdown timer arming logic so EXPIRE_SHUTDOWN is scheduled correctly.
  • Add a new event-driven libtest client (cli_ev_download) and a new pytest case (test_19_08) to validate shutdown sockets stay supervised until shutdown completes.

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
tests/libtest/Makefile.inc Adds the new event-driven test client to the libtest build.
tests/libtest/cli_ev_download.c New strict socket-action loop client to reproduce/verify shutdown socket supervision.
tests/http/test_19_shutdown.py Adds a new pytest that asserts shutdown sockets are watched and fully drained.
lib/cshutdn.c Fixes next shutdown-expire scheduling so shutdown timeouts can fire.
lib/cfilters.c Makes pollset adjustment consult shutdown state so shutdown sockets stay registered.

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

Comment thread tests/libtest/cli_ev_download.c Resolved
@testclutch

Copy link
Copy Markdown

Analysis of PR #22304 at 7b701945:

Test ../../tests/http/test_32_ftps_vsftpd.py::TestFtpsVsFTPD::test_32_06_shutdown_download failed, which has NOT been flaky recently, so there could be a real issue in this PR. Note that this test has failed in 2 different CI jobs (the link just goes to one of them).

Generated by Testclutch

@jay

jay commented Jul 12, 2026

Copy link
Copy Markdown
Member

@icing about your busy loop fix and this modification is the intention to adjust both if the one socket is connected but the other isn't, or one is shutting down but the other isn't, because I mean for example why is not applied per filter if only that filter meets requirements, like for example what about this:

  for(i = 0; (i < 2) && !result && conn; ++i) {
    if(ps->n && conn->cfilter[i] && 
       (!Curl_conn_is_connected(conn, i) ||
        conn_shutdown_started(conn, i))) {
      result = Curl_conn_cf_adjust_pollset(conn->cfilter[i], data, ps);
    }
  }

because otherwise both can adjusted depending on the check of one? maybe I misunderstand

@GrahamCampbell
GrahamCampbell marked this pull request as ready for review July 13, 2026 10:22

@icing icing left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Nice work, adding the test client and case!

Comment thread lib/cfilters.c Resolved
Comment thread lib/cfilters.c Resolved
icing and others added 4 commits July 15, 2026 21:27
In curl 8.21 the progress handling of shutdowns became brokwn as the
connections sockets were no longer added to the pollset.

Fix-by: pszemus

refs curl#22282

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 12 out of 12 changed files in this pull request and generated 1 comment.

Comment thread tests/libtest/cli_ev_download.c Resolved

@icing icing left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks!

@jay jay closed this in 820c014 Jul 17, 2026
@jay

jay commented Jul 17, 2026

Copy link
Copy Markdown
Member

Thanks

vszakats added a commit that referenced this pull request Nov 10, 2026
Seen in test 2330 torture with c-ares not enabled, or threaded-resolved
not disabled.

Fixing:
```
test 2330 [FTP server response timeout with the socket action API]
 76 functions found, but only fail 25 (32.89%)
** MEMORY FAILURE
Leak detected: memory still allocated: 3056 bytes
At 5c4ca478c9e8, there is 3056 bytes.
 allocated by /home/runner/work/curl/curl/lib/url.c:457
LIMIT /home/runner/work/curl/curl/lib/curlx/strdup.c:87 malloc reached memlimit
 2330: torture FAILED: function number 14 in test.
 invoke with "-t14" to repeat this single case.
```
Linux: https://github.com/curl/curl/actions/runs/42739521997/job/129576860241?pr=23869#step:45:3215
macOS: https://github.com/curl/curl/actions/runs/42739522174/job/129576860210?pr=23869#step:17:1457

Bug: #23246 (comment)
Bug: #23869 (comment)
Follow-up to b32baab #23246
Follow-up to 820c014 #22304
Cherry-picked from #23869

Closes #23872
dkarpov1970 pushed a commit to dkarpov1970/curl that referenced this pull request Nov 18, 2026
- Adjust pollset during connection shutdown.

- Separate the FIRSTSOCKET and SECONDSOCKET check so that one being in
  connect or shutdown no longer adds poll events for the other.

- Fix shutdown state evaluation (as detailed below).

- Add a unit test for Curl_conn_adjust_pollset.

- Add a client for event testing.

- Test that shutdown sockets stay with the socket callback until done.

The pollset predicate now reads the connection's own shutdown state
rather than going through data->conn, and the EXPIRE_SHUTDOWN arming in
cshutdn_perform() works again: next_expire_ms started at 0 and could
never be set, so a peer that never sends a close_notify would still park
its connection forever, timeout or not.

Reported-by: [email protected]

Fixes curl#22282
Closes curl#22304

Backport notes:

- unit2606.c: the release's struct Curl_cftype still has do_close, so
  the test filter sets Curl_cf_def_close.

- Added test2606 and unit2606.c to the release's test lists.

- Dropped the new cli_ev_download.c client and its pytest; the fix
  does not need them.

(cherry picked from commit 820c014)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Development

Successfully merging this pull request may close these issues.

5 participants