swift-server / swift-server/swift-http-server

Client closing the socket does not cause a CancellationError on the server

Open
#53 0 comments 0 reactions 0 assignees View on GitHub

@gjcairo is already working on this.

Since Sep 11, 2026.

  • #134 by @gjcairo — open
kind/feature
Dominant language
Swift
Stars
44
Forks
13
Avg merge
4d 3h
Merged PRs (30d)
8

Description

We have found that a server that is async-waiting after receiving a HTTP request does not see a CancellationError if the client were to close the connection.

This is trivially reproducible.

Server code:

let logger = Logger(label: "TestHTTPServer")
let server = NIOHTTPServer(logger: logger, configuration: .init(bindTarget: .hostAndPort(host: "127.0.0.1", port: 12345)))
try await server.serve { request, requestContext, requestBodyAndTrailers, responseSender in
    print("STALLING...")
    try await Task.sleep(for: .seconds(60 * 60))
}

Client terminal:

$ curl -v http://localhost:12345/stall
* Host localhost:12345 was resolved.
* IPv6: ::1
* IPv4: 127.0.0.1
*   Trying [::1]:12345...
* connect to ::1 port 12345 from ::1 port 57145 failed: Connection refused
*   Trying 127.0.0.1:12345...
* Connected to localhost (127.0.0.1) port 12345
> GET /stall HTTP/1.1
> Host: localhost:12345
> User-Agent: curl/8.7.1
> Accept: */*
> 
* Request completely sent off
^C

Server output:

STALLING...

The server hangs even though the client has closed the socket. Note how there is no exception or additional printing in the server output.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the NIOHTTPServer serve closure shown in the issue and reproduce the stalled request with the provided server and curl commands. Trace how client socket closure reaches the async request handler, then verify that closing the client causes the server-side wait to receive a CancellationError rather than continuing to hang.

Written by the indexing model from the issue text.

Assessment

Tech stack
swift
Domain
backend, networking
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.