On an M2 MacBook Air that exact code deadlocks pretty much all the time.
On an M1 Studio Ultra it might help to change the condition in performWork to id % 10 == 0 to make it more likely.
I am in both cases on macOS 13.4.1 using Xcode 14.3.1.
I include the backtrace below. It is a bit hard to see, but eight threads (the number of cores on the MacBook Air) are waiting on the cooperative queue Swift is using for its tasks.
(lldb) bt all
* thread #1, queue = 'com.apple.main-thread', stop reason = signal SIGSTOP
* frame #0: 0x00000001a820bf14 libsystem_kernel.dylib`mach_msg2_trap + 8
frame #1: 0x00000001a821e240 libsystem_kernel.dylib`mach_msg2_internal + 80
frame #2: 0x00000001a8214b78 libsystem_kernel.dylib`mach_msg_overwrite + 604
frame #3: 0x00000001a820c290 libsystem_kernel.dylib`mach_msg + 24
frame #4: 0x00000001a832a7e4 CoreFoundation`__CFRunLoopServiceMachPort + 160
frame #5: 0x00000001a83290c4 CoreFoundation`__CFRunLoopRun + 1208
frame #6: 0x00000001a83284b8 CoreFoundation`CFRunLoopRunSpecific + 612
frame #7: 0x00000001007b4bdc XCTestCore`-[XCTWaiter waitForExpectations:timeout:enforceOrder:] + 704
frame #8: 0x00000001007b6c9c XCTestCore`+[XCTWaiter waitForExpectations:timeout:enforceOrder:] + 80
frame #9: 0x00000001007d9394 XCTestCore`__81+[XCTFailableInvocation invokeWithAsynchronousWait:lastObservedErrorIssue:block:]_block_invoke + 236
frame #10: 0x00000001007986d0 XCTestCore`__49+[XCTSwiftErrorObservation observeErrorsInBlock:]_block_invoke + 48
frame #11: 0x000000010261c0d8 libXCTestSwiftSupport.dylib`function signature specialization <Arg[1] = [Closure Propagated : reabstraction thunk helper from @callee_unowned @convention(block) (@unowned @callee_unowned @convention(block) () -> ()) -> () to @escaping @callee_guaranteed (@unowned @callee_guaranteed () -> ()) -> (), Argument Types : [@callee_unowned @convention(block) (@unowned @callee_unowned @convention(block) () -> ()) -> ()]> of closure #1 () -> () in static __C.XCTSwiftErrorObservation._observeErrors(in: (() -> ()) -> ()) -> () -> Swift.Optional<XCTest.XCTIssue> + 208
frame #12: 0x000000010261c1d4 libXCTestSwiftSupport.dylib`function signature specialization <Arg[5] = [Closure Propagated : reabstraction thunk helper from @callee_unowned @convention(block) (@unowned @callee_unowned @convention(block) () -> ()) -> () to @escaping @callee_guaranteed (@unowned @callee_guaranteed () -> ()) -> (), Argument Types : [@callee_unowned @convention(block) (@unowned @callee_unowned @convention(block) () -> ()) -> ()]> of function signature specialization <Arg[2] = [Closure Propagated : closure #1 () -> () in static (extension in XCTest):__C.XCTSwiftErrorObservation.(_observeErrors in _B0397D3B80CBC8D7FB9A5B33AB2A74B8)(in: (() -> ()) -> ()) -> () -> Swift.Optional<XCTest.XCTIssue>, Argument Types : [@callee_guaranteed (@unowned @callee_guaranteed () -> ()) -> ()]> of generic specialization <Swift.Optional<XCTest.LocalErrorTracker>, ()> of Swift.TaskLocal.withValue<τ_0_0>(_: τ_0_0, operation: () throws -> τ_1_0, file: Swift.String, line: Swift.UInt) throws -> τ_1_0 + 144
frame #13: 0x000000010261be80 libXCTestSwiftSupport.dylib`function signature specialization <Arg[0] = [Closure Propagated : reabstraction thunk helper from @callee_unowned @convention(block) (@unowned @callee_unowned @convention(block) () -> ()) -> () to @escaping @callee_guaranteed (@unowned @callee_guaranteed () -> ()) -> (), Argument Types : [@callee_unowned @convention(block) (@unowned @callee_unowned @convention(block) () -> ()) -> ()]> of static __C.XCTSwiftErrorObservation._observeErrors(in: (() -> ()) -> ()) -> () -> Swift.Optional<XCTest.XCTIssue> + 916
frame #14: 0x000000010261c2bc libXCTestSwiftSupport.dylib`@objc static __C.XCTSwiftErrorObservation._observeErrors(in: (() -> ()) -> ()) -> () -> Swift.Optional<XCTest.XCTIssue> + 52
frame #15: 0x00000001007985d8 XCTestCore`+[XCTSwiftErrorObservation observeErrorsInBlock:] + 204
frame #16: 0x00000001007d91cc XCTestCore`+[XCTFailableInvocation invokeWithAsynchronousWait:lastObservedErrorIssue:block:] + 228
frame #17: 0x00000001007d984c XCTestCore`+[XCTFailableInvocation invokeInvocation:withTestMethodConvention:lastObservedErrorIssue:] + 372
frame #18: 0x00000001007d9bc8 XCTestCore`+[XCTFailableInvocation invokeInvocation:lastObservedErrorIssue:] + 72
frame #19: 0x00000001007c7748 XCTestCore`__24-[XCTestCase invokeTest]_block_invoke_2 + 88
frame #20: 0x00000001007a5924 XCTestCore`-[XCTMemoryChecker _assertInvalidObjectsDeallocatedAfterScope:] + 84
frame #21: 0x00000001007d0984 XCTestCore`-[XCTestCase assertInvalidObjectsDeallocatedAfterScope:] + 92
frame #22: 0x00000001007c76c8 XCTestCore`__24-[XCTestCase invokeTest]_block_invoke.98 + 172
frame #23: 0x00000001007911e8 XCTestCore`-[XCTestCase(XCTIssueHandling) _caughtUnhandledDeveloperExceptionPermittingControlFlowInterruptions:caughtInterruptionException:whileExecutingBlock:] + 168
frame #24: 0x00000001007c724c XCTestCore`-[XCTestCase invokeTest] + 756
frame #25: 0x00000001007c889c XCTestCore`__26-[XCTestCase performTest:]_block_invoke.154 + 36
frame #26: 0x00000001007911e8 XCTestCore`-[XCTestCase(XCTIssueHandling) _caughtUnhandledDeveloperExceptionPermittingControlFlowInterruptions:caughtInterruptionException:whileExecutingBlock:] + 168
frame #27: 0x00000001007c83e8 XCTestCore`__26-[XCTestCase performTest:]_block_invoke.140 + 516
frame #28: 0x00000001007aee8c XCTestCore`+[XCTContext _runInChildOfContext:forTestCase:markAsReportingBase:block:] + 180
frame #29: 0x00000001007aeda0 XCTestCore`+[XCTContext runInContextForTestCase:markAsReportingBase:block:] + 144
frame #30: 0x00000001007c8040 XCTestCore`-[XCTestCase performTest:] + 308
frame #31: 0x000000010078002c XCTestCore`-[XCTest runTest] + 48
frame #32: 0x00000001007b1a18 XCTestCore`-[XCTestSuite runTestBasedOnRepetitionPolicy:testRun:] + 68
frame #33: 0x00000001007b18f8 XCTestCore`__27-[XCTestSuite performTest:]_block_invoke + 164
frame #34: 0x00000001007b13f8 XCTestCore`__59-[XCTestSuite _performProtectedSectionForTest:testSection:]_block_invoke + 48
frame #35: 0x00000001007aee8c XCTestCore`+[XCTContext _runInChildOfContext:forTestCase:markAsReportingBase:block:] + 180
frame #36: 0x00000001007aeda0 XCTestCore`+[XCTContext runInContextForTestCase:markAsReportingBase:block:] + 144
frame #37: 0x00000001007b1394 XCTestCore`-[XCTestSuite _performProtectedSectionForTest:testSection:] + 180
frame #38: 0x00000001007b1600 XCTestCore`-[XCTestSuite performTest:] + 216
frame #39: 0x000000010078002c XCTestCore`-[XCTest runTest] + 48
frame #40: 0x0000000100781cf0 XCTestCore`__89-[XCTTestRunSession executeTestsWithIdentifiers:skippingTestsWithIdentifiers:completion:]_block_invoke + 520
frame #41: 0x00000001007aee8c XCTestCore`+[XCTContext _runInChildOfContext:forTestCase:markAsReportingBase:block:] + 180
frame #42: 0x00000001007aeda0 XCTestCore`+[XCTContext runInContextForTestCase:markAsReportingBase:block:] + 144
frame #43: 0x0000000100781a44 XCTestCore`-[XCTTestRunSession executeTestsWithIdentifiers:skippingTestsWithIdentifiers:completion:] + 296
frame #44: 0x00000001007e6fe0 XCTestCore`__103-[XCTExecutionWorker executeTestIdentifiers:skippingTestIdentifiers:completionHandler:completionQueue:]_block_invoke_2 + 136
frame #45: 0x00000001007e6500 XCTestCore`-[XCTExecutionWorker runWithError:] + 108
frame #46: 0x00000001007ac0c8 XCTestCore`__25-[XCTestDriver _runTests]_block_invoke.272 + 56
frame #47: 0x000000010078a460 XCTestCore`-[XCTestObservationCenter _observeTestExecutionForBlock:] + 288
frame #48: 0x00000001007abd24 XCTestCore`-[XCTestDriver _runTests] + 1092
frame #49: 0x000000010078061c XCTestCore`_XCTestMain + 88
frame #50: 0x00000001000057c0 xctest`main + 156
frame #51: 0x00000001a7ef3f28 dyld`start + 2236
thread #2, queue = 'com.apple.root.user-initiated-qos.cooperative'
frame #0: 0x00000001a820dcd0 libsystem_kernel.dylib`__ulock_wait + 8
frame #1: 0x00000001a809cdf0 libdispatch.dylib`_dlock_wait + 56
frame #2: 0x00000001a809cba4 libdispatch.dylib`_dispatch_thread_event_wait_slow + 56
frame #3: 0x00000001a80abc68 libdispatch.dylib`__DISPATCH_WAIT_FOR_QUEUE__ + 368
frame #4: 0x00000001a80ab814 libdispatch.dylib`_dispatch_sync_f_slow + 148
frame #5: 0x0000000100c3c394 DispatchTests`Subsystem.read(id=3, self=0x0000600000204620) at DeadlockTest.swift:32:15
frame #6: 0x0000000100c3b074 DispatchTests`Subsystem.performWork(id=3, self=0x0000600000204620) at DeadlockTest.swift:21:16
frame #7: 0x0000000100c3affc DispatchTests`closure #1 in closure #1 in BarrierTests.test(subsystem=0x0000600000204620, index=3) at DeadlockTest.swift:8:43
frame #8: 0x0000000100c3d960 DispatchTests`partial apply for closure #1 in closure #1 in BarrierTests.test() at <compiler-generated>:0
frame #9: 0x0000000100c3d190 DispatchTests`thunk for @escaping @callee_guaranteed @Sendable @async () -> (@out A) at <compiler-generated>:0
frame #10: 0x0000000100c3da9c DispatchTests`partial apply for thunk for @escaping @callee_guaranteed @Sendable @async () -> (@out A) at <compiler-generated>:0
thread #3, queue = 'com.apple.root.user-initiated-qos.cooperative'
frame #0: 0x00000001a820dcd0 libsystem_kernel.dylib`__ulock_wait + 8
frame #1: 0x00000001a809cdf0 libdispatch.dylib`_dlock_wait + 56
frame #2: 0x00000001a809cba4 libdispatch.dylib`_dispatch_thread_event_wait_slow + 56
frame #3: 0x00000001a80abc68 libdispatch.dylib`__DISPATCH_WAIT_FOR_QUEUE__ + 368
frame #4: 0x00000001a80ab814 libdispatch.dylib`_dispatch_sync_f_slow + 148
frame #5: 0x0000000100c3c394 DispatchTests`Subsystem.read(id=6, self=0x0000600000204620) at DeadlockTest.swift:32:15
frame #6: 0x0000000100c3b074 DispatchTests`Subsystem.performWork(id=6, self=0x0000600000204620) at DeadlockTest.swift:21:16
frame #7: 0x0000000100c3affc DispatchTests`closure #1 in closure #1 in BarrierTests.test(subsystem=0x0000600000204620, index=6) at DeadlockTest.swift:8:43
frame #8: 0x0000000100c3d960 DispatchTests`partial apply for closure #1 in closure #1 in BarrierTests.test() at <compiler-generated>:0
frame #9: 0x0000000100c3d190 DispatchTests`thunk for @escaping @callee_guaranteed @Sendable @async () -> (@out A) at <compiler-generated>:0
frame #10: 0x0000000100c3da9c DispatchTests`partial apply for thunk for @escaping @callee_guaranteed @Sendable @async () -> (@out A) at <compiler-generated>:0
thread #4, queue = 'com.apple.root.user-initiated-qos.cooperative'
frame #0: 0x00000001a820dcd0 libsystem_kernel.dylib`__ulock_wait + 8
frame #1: 0x00000001a809cdf0 libdispatch.dylib`_dlock_wait + 56
frame #2: 0x00000001a809cba4 libdispatch.dylib`_dispatch_thread_event_wait_slow + 56
frame #3: 0x00000001a80abc68 libdispatch.dylib`__DISPATCH_WAIT_FOR_QUEUE__ + 368
frame #4: 0x00000001a80ab814 libdispatch.dylib`_dispatch_sync_f_slow + 148
frame #5: 0x0000000100c3c394 DispatchTests`Subsystem.read(id=9, self=0x0000600000204620) at DeadlockTest.swift:32:15
frame #6: 0x0000000100c3b074 DispatchTests`Subsystem.performWork(id=9, self=0x0000600000204620) at DeadlockTest.swift:21:16
frame #7: 0x0000000100c3affc DispatchTests`closure #1 in closure #1 in BarrierTests.test(subsystem=0x0000600000204620, index=9) at DeadlockTest.swift:8:43
frame #8: 0x0000000100c3d960 DispatchTests`partial apply for closure #1 in closure #1 in BarrierTests.test() at <compiler-generated>:0
frame #9: 0x0000000100c3d190 DispatchTests`thunk for @escaping @callee_guaranteed @Sendable @async () -> (@out A) at <compiler-generated>:0
frame #10: 0x0000000100c3da9c DispatchTests`partial apply for thunk for @escaping @callee_guaranteed @Sendable @async () -> (@out A) at <compiler-generated>:0
thread #5, queue = 'com.apple.root.user-initiated-qos.cooperative'
frame #0: 0x00000001a820dcd0 libsystem_kernel.dylib`__ulock_wait + 8
frame #1: 0x00000001a809cdf0 libdispatch.dylib`_dlock_wait + 56
frame #2: 0x00000001a809cba4 libdispatch.dylib`_dispatch_thread_event_wait_slow + 56
frame #3: 0x00000001a80abc68 libdispatch.dylib`__DISPATCH_WAIT_FOR_QUEUE__ + 368
frame #4: 0x00000001a80ab814 libdispatch.dylib`_dispatch_sync_f_slow + 148
frame #5: 0x0000000100c3c394 DispatchTests`Subsystem.read(id=11, self=0x0000600000204620) at DeadlockTest.swift:32:15
frame #6: 0x0000000100c3b074 DispatchTests`Subsystem.performWork(id=11, self=0x0000600000204620) at DeadlockTest.swift:21:16
frame #7: 0x0000000100c3affc DispatchTests`closure #1 in closure #1 in BarrierTests.test(subsystem=0x0000600000204620, index=11) at DeadlockTest.swift:8:43
frame #8: 0x0000000100c3d960 DispatchTests`partial apply for closure #1 in closure #1 in BarrierTests.test() at <compiler-generated>:0
frame #9: 0x0000000100c3d190 DispatchTests`thunk for @escaping @callee_guaranteed @Sendable @async () -> (@out A) at <compiler-generated>:0
frame #10: 0x0000000100c3da9c DispatchTests`partial apply for thunk for @escaping @callee_guaranteed @Sendable @async () -> (@out A) at <compiler-generated>:0
thread #6, queue = 'com.apple.root.user-initiated-qos.cooperative'
frame #0: 0x00000001a820dcd0 libsystem_kernel.dylib`__ulock_wait + 8
frame #1: 0x00000001a809cdf0 libdispatch.dylib`_dlock_wait + 56
frame #2: 0x00000001a809cba4 libdispatch.dylib`_dispatch_thread_event_wait_slow + 56
frame #3: 0x00000001a80abc68 libdispatch.dylib`__DISPATCH_WAIT_FOR_QUEUE__ + 368
frame #4: 0x00000001a80ab814 libdispatch.dylib`_dispatch_sync_f_slow + 148
frame #5: 0x0000000100c3c394 DispatchTests`Subsystem.read(id=10, self=0x0000600000204620) at DeadlockTest.swift:32:15
frame #6: 0x0000000100c3b074 DispatchTests`Subsystem.performWork(id=10, self=0x0000600000204620) at DeadlockTest.swift:21:16
frame #7: 0x0000000100c3affc DispatchTests`closure #1 in closure #1 in BarrierTests.test(subsystem=0x0000600000204620, index=10) at DeadlockTest.swift:8:43
frame #8: 0x0000000100c3d960 DispatchTests`partial apply for closure #1 in closure #1 in BarrierTests.test() at <compiler-generated>:0
frame #9: 0x0000000100c3d190 DispatchTests`thunk for @escaping @callee_guaranteed @Sendable @async () -> (@out A) at <compiler-generated>:0
frame #10: 0x0000000100c3da9c DispatchTests`partial apply for thunk for @escaping @callee_guaranteed @Sendable @async () -> (@out A) at <compiler-generated>:0
thread #7, queue = 'com.apple.root.user-initiated-qos.cooperative'
frame #0: 0x00000001a820dcd0 libsystem_kernel.dylib`__ulock_wait + 8
frame #1: 0x00000001a809cdf0 libdispatch.dylib`_dlock_wait + 56
frame #2: 0x00000001a809cba4 libdispatch.dylib`_dispatch_thread_event_wait_slow + 56
frame #3: 0x00000001a80abc68 libdispatch.dylib`__DISPATCH_WAIT_FOR_QUEUE__ + 368
frame #4: 0x00000001a80ab814 libdispatch.dylib`_dispatch_sync_f_slow + 148
frame #5: 0x0000000100c3c394 DispatchTests`Subsystem.read(id=4, self=0x0000600000204620) at DeadlockTest.swift:32:15
frame #6: 0x0000000100c3b074 DispatchTests`Subsystem.performWork(id=4, self=0x0000600000204620) at DeadlockTest.swift:21:16
frame #7: 0x0000000100c3affc DispatchTests`closure #1 in closure #1 in BarrierTests.test(subsystem=0x0000600000204620, index=4) at DeadlockTest.swift:8:43
frame #8: 0x0000000100c3d960 DispatchTests`partial apply for closure #1 in closure #1 in BarrierTests.test() at <compiler-generated>:0
frame #9: 0x0000000100c3d190 DispatchTests`thunk for @escaping @callee_guaranteed @Sendable @async () -> (@out A) at <compiler-generated>:0
frame #10: 0x0000000100c3da9c DispatchTests`partial apply for thunk for @escaping @callee_guaranteed @Sendable @async () -> (@out A) at <compiler-generated>:0
thread #8
frame #0: 0x00000001a820dbc8 libsystem_kernel.dylib`__workq_kernreturn + 8
thread #9
frame #0: 0x00000001a820dbc8 libsystem_kernel.dylib`__workq_kernreturn + 8
thread #10, queue = 'com.apple.root.user-initiated-qos.cooperative'
frame #0: 0x00000001a820dcd0 libsystem_kernel.dylib`__ulock_wait + 8
frame #1: 0x00000001a809cdf0 libdispatch.dylib`_dlock_wait + 56
frame #2: 0x00000001a809cba4 libdispatch.dylib`_dispatch_thread_event_wait_slow + 56
frame #3: 0x00000001a80abc68 libdispatch.dylib`__DISPATCH_WAIT_FOR_QUEUE__ + 368
frame #4: 0x00000001a80ab814 libdispatch.dylib`_dispatch_sync_f_slow + 148
frame #5: 0x0000000100c3c394 DispatchTests`Subsystem.read(id=8, self=0x0000600000204620) at DeadlockTest.swift:32:15
frame #6: 0x0000000100c3b074 DispatchTests`Subsystem.performWork(id=8, self=0x0000600000204620) at DeadlockTest.swift:21:16
frame #7: 0x0000000100c3affc DispatchTests`closure #1 in closure #1 in BarrierTests.test(subsystem=0x0000600000204620, index=8) at DeadlockTest.swift:8:43
frame #8: 0x0000000100c3d960 DispatchTests`partial apply for closure #1 in closure #1 in BarrierTests.test() at <compiler-generated>:0
frame #9: 0x0000000100c3d190 DispatchTests`thunk for @escaping @callee_guaranteed @Sendable @async () -> (@out A) at <compiler-generated>:0
frame #10: 0x0000000100c3da9c DispatchTests`partial apply for thunk for @escaping @callee_guaranteed @Sendable @async () -> (@out A) at <compiler-generated>:0
thread #11, queue = 'com.apple.root.user-initiated-qos.cooperative'
frame #0: 0x00000001a820dcd0 libsystem_kernel.dylib`__ulock_wait + 8
frame #1: 0x00000001a809cdf0 libdispatch.dylib`_dlock_wait + 56
frame #2: 0x00000001a809cba4 libdispatch.dylib`_dispatch_thread_event_wait_slow + 56
frame #3: 0x00000001a80abc68 libdispatch.dylib`__DISPATCH_WAIT_FOR_QUEUE__ + 368
frame #4: 0x00000001a80ab814 libdispatch.dylib`_dispatch_sync_f_slow + 148
frame #5: 0x0000000100c3c394 DispatchTests`Subsystem.read(id=12, self=0x0000600000204620) at DeadlockTest.swift:32:15
frame #6: 0x0000000100c3b074 DispatchTests`Subsystem.performWork(id=12, self=0x0000600000204620) at DeadlockTest.swift:21:16
frame #7: 0x0000000100c3affc DispatchTests`closure #1 in closure #1 in BarrierTests.test(subsystem=0x0000600000204620, index=12) at DeadlockTest.swift:8:43
frame #8: 0x0000000100c3d960 DispatchTests`partial apply for closure #1 in closure #1 in BarrierTests.test() at <compiler-generated>:0
frame #9: 0x0000000100c3d190 DispatchTests`thunk for @escaping @callee_guaranteed @Sendable @async () -> (@out A) at <compiler-generated>:0
frame #10: 0x0000000100c3da9c DispatchTests`partial apply for thunk for @escaping @callee_guaranteed @Sendable @async () -> (@out A) at <compiler-generated>:0