Skip to content

Discarding task group tasks are alive and running after successfully ending their scope #71809

Open
@Joannis

Description

@Joannis

Description

When debugging Hummingbird 2 in instruments, I've found that running a benchmark against a test app causes tasks to stay alive. This is paired with a slight bump in memory usage as well, which isn't being released. Upon inspection, the alive task tracker in the "Swift Tasks" instruments sees a continuous growth in both running and alive tasks.

In the attached graph, you'll see ~five tests in the time profiler. In reality, I ran nine of these tests total. Each test invoked wrk as such: wrk -c 128 -d 15s -t 8 http://localhost:8080. This is likely a glitch in the time profiler instrument, because WRK is in fact producing and receiving data. In addition, the alive and running tasks do continue to grow.

image

Initially, I thought that the tasks were kept alive by NIO or Hummingbird. In reality, the socket closes and I've added a marker at the start and end of each task to measure how many tasks were still running as far as Hummingbird is concerned. The counter went up to 128 and back to 0 as expected. This leads me to conclude that there's an edge case we're running into with DiscardingTaskGroup.

The line(s) in question are these: https://github.com/hummingbird-project/hummingbird/blob/2.x.x/Sources/HummingbirdCore/Server/Server.swift#L118-L120
This calls into HTTP1: https://github.com/hummingbird-project/hummingbird/blob/2.x.x/Sources/HummingbirdCore/Server/HTTP/HTTP1Channel.swift#L65
Finally calling into a generic HTTP handle function here: https://github.com/hummingbird-project/hummingbird/blob/2.x.x/Sources/HummingbirdCore/Server/HTTP/HTTPChannelHandler.swift#L43

Reproduction

Hummingbird 2 has a PerformanceTest application that reproduces this issue. Run that executable in Xcode Instruments with the concurrency tools. Then, after it's started, run wrk -c 128 -d 15s -t 8 http://localhost:8080 from your terminal.

Expected behavior

The amount of alive and running tasks decreases once TCP connections are closed.

Environment

Swift 5.9 as bundled with Xcode 15.2 (swiftc -version does not happen to be targeting the Xcode provided version)

Additional information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugA deviation from expected or documented behavior. Also: expected but undesirable behavior.concurrency runtimeFeature: umbrella label for concurrency runtime features

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions