Skip to content

Crash in swift_task_dealloc when multiple modules generate Task.sleep(for:) specialisations #86204

@vsarunas

Description

@vsarunas

Description

We noticed this when with upgrade from 6.1 to 6.2.0. Revisiting again with 6.2.3.
This does not happen on toolchains that are compiled with +assertions flags such as:

  • swift-6.2.3-RELEASE (not the one shipped with Xcode 26.2)
  • swift-6.3-DEVELOPMENT-SNAPSHOT-2025-12-14-a

The reproducer will crash with the following:

freed pointer was not the last allocation[1]

It looks to be a cross module interaction between Kafka and ServiceLifecycle having different Task.sleep(for:) specialisations.

Reproduction

Self contained archive: TaskSleepCrash.zip

Package.swift:

// swift-tools-version: 6.2
import PackageDescription

let package = Package(
    name: "TaskSleepCrash",
    platforms: [.macOS(.v15)],
    dependencies: [
        .package(url: "https://github.com/ordo-one/swift-kafka-client", exact: "1.0.0-ordoalpha.49"),
    ],
    targets: [
        .executableTarget(
            name: "TaskSleepCrash",
            dependencies: [
                .product(name: "Kafka", package: "swift-kafka-client"),
            ],
            path: "Sources"
        ),
    ]
)

Sources/main.swift:

import Foundation
// Not imported, just linked
// import Kafka

@main
struct Main {
    static func main() async throws {
        // CRASHES:
        try? await Task.sleep(for: .seconds(5))

        // WORKS:
        // let clock = ContinuousClock()
        // try? await clock.sleep(until: clock.now.advanced(by: .seconds(5)))

        // WORKS
        // try? await Task.sleep(nanoseconds: 5_000_000_000)
        print("After sleep")
    }
}

Run in release mode.

Stack dump

Thread 1 Crashed::  Dispatch queue: com.apple.root.default-qos.cooperative
0   libsystem_kernel.dylib        	       0x1874cb5b0 __pthread_kill + 8
1   libsystem_pthread.dylib       	       0x187505888 pthread_kill + 296
2   libsystem_c.dylib             	       0x18740a850 abort + 124
3   libswift_Concurrency.dylib    	       0x282def484 swift::swift_Concurrency_fatalErrorv(unsigned int, char const*, char*) + 32
4   libswift_Concurrency.dylib    	       0x282def4a4 swift::swift_Concurrency_fatalError(unsigned int, char const*, ...) + 32
5   libswift_Concurrency.dylib    	       0x282df4704 swift_task_dealloc + 132
6   TaskSleepCrash                	       0x100bf80b0 specialized static Task<>.sleep<A>(for:tolerance:clock:) + 24 [inlined]
7   TaskSleepCrash                	       0x100bf80b0 closure #1 in closure #1 in static Main.main() + 52 (main.swift:11)
8   TaskSleepCrash                	       0x100bf858d partial apply for closure #1 in closure #1 in static Main.main() + 1
9   TaskSleepCrash                	       0x100bf8631 specialized thunk for @escaping @isolated(any) @callee_guaranteed @async () -> (@out A) + 1
10  TaskSleepCrash                	       0x100bf8709 partial apply for specialized thunk for @escaping @isolated(any) @callee_guaranteed @async () -> (@out A) + 1
11  libswift_Concurrency.dylib    	       0x282df32fd completeTaskWithClosure(swift::AsyncContext*, swift::SwiftError*) + 1

Expected behavior

No crash

Environment

swift-driver version: 1.127.14.1 Apple Swift version 6.2.3 (swiftlang-6.2.3.3.21 clang-1700.6.3.2)
Target: arm64-apple-macosx26.0

Additional information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugA deviation from expected or documented behavior. Also: expected but undesirable behavior.crashBug: A crash, i.e., an abnormal termination of softwaretriage neededThis issue needs more specific labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions