Skip to content

onCancel callback of the withTaskCancellationHandler() is called more than once. #80161

Closed
@ser-0xff

Description

@ser-0xff

Description

The documentation doesn't clearly state that the onCancel can be called more than once.
We observed sometimes it happens and not clear is it bug or not.
Minimized reproducing test is available on github

Reproduction

git clone https://github.com/ordo-one/external-reproducers
cd external-reproducers/swift/task-cancellation
swift Sources/Main.swift 

Sometimes test fails:

task-cancellation % swift Sources/Main.swift 
Main/Main.swift:39: Precondition failed: cancelled more than once, iteration: 234607
Stack dump without symbol names (ensure you have llvm-symbolizer in your PATH or set the environment var `LLVM_SYMBOLIZER_PATH` to point to it):
0  swift-frontend             0x000000010a1c6a9c llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) + 56
1  swift-frontend             0x000000010a1c4cf0 llvm::sys::RunSignalHandlers() + 112
2  swift-frontend             0x000000010a1c7068 SignalHandler(int) + 292
3  libsystem_platform.dylib   0x000000018b436de4 _sigtramp + 56
4  libswiftCore.dylib         0x000000019c1d90a4 $ss17_assertionFailure__4file4line5flagss5NeverOs12StaticStringV_SSAHSus6UInt32VtF + 268
5  libswiftCore.dylib         0x00000001108709ac $ss17_assertionFailure__4file4line5flagss5NeverOs12StaticStringV_SSAHSus6UInt32VtF + 18446744071367653908
6  libswift_Concurrency.dylib 0x000000026bcd427c swift::runJobInEstablishedExecutorContext(swift::Job*) + 252
7  libswift_Concurrency.dylib 0x000000026bcd57a4 swift_job_runImpl(swift::Job*, swift::SerialExecutorRef) + 144
8  libdispatch.dylib          0x000000018b25f4cc _dispatch_root_queue_drain + 392
9  libdispatch.dylib          0x000000018b25fcd8 _dispatch_worker_thread2 + 156
10 libsystem_pthread.dylib    0x000000018b3fc39c _pthread_wqthread + 228
11 libsystem_pthread.dylib    0x000000018b3fb0f0 start_wqthread + 8
zsh: trace trap  swift Sources/Main.swift

Expected behavior

precondition check at line 39 always success

Environment

task-cancellation % swiftc -version 
swift-driver version: 1.115.1 Apple Swift version 6.0.3 (swiftlang-6.0.3.1.10 clang-1600.0.30.1)
Target: arm64-apple-macosx15.0

Additional information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

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

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions