Swift concurrency and NSData - all threads stuck in mach_msg2_trap

I run the following code in an actor:

func aaa() async throws -> Data {
    async let result = Task(
            operation: {
                ... decompressing data through try (data as NSData).decompressed(using: .lzfse) as Data
            }
        ).result
    switch await result {
    case .success(let value): return value
    case .failure(let error): throw error
}

I do it this way because I do not want to block the actor by decompression, and there is no state change in the actor afterwards. I would say that the actor plays no significant role here. Important is that many (14) concurrent tasks run in parallel, however NOT on the same data. It runs fine for a while (dozens/hundreds of data decompressed), and then the following happens:

  • Activity Monitor (macOS GUI tool) shows almost none User CPU time, and approx. 75% System CPU time. The rest is idle. (When it runs fine, User CPU time is 95+%)
  • When I pause the run in Xcode (in release config it behaves the same), all threads are in mach_msg2_trap
#0	0x0000000180ac21f4 in mach_msg2_trap ()
#1	0x0000000180ad4b24 in mach_msg2_internal ()
#2	0x0000000180ac52fc in vm_copy ()
#3	0x0000000180916b78 in szone_realloc ()
#4	0x000000018093cfb0 in _malloc_zone_realloc ()
#5	0x000000018093d7e8 in _realloc ()
#6	0x0000000180bb8a10 in __CFSafelyReallocate ()
#7	0x0000000181d00e30 in _NSMutableDataGrowBytes ()
#8	0x0000000181ce2630 in -[NSConcreteMutableData appendBytes:length:] ()
#9	0x00000001823c30d8 in -[_NSDataCompressor processBytes:size:flags:] ()
#10	0x00000001823c32c4 in -[NSData(NSDataCompression) _produceDataWithCompressionOperation:algorithm:handler:] ()
#11	0x00000001823c3598 in -[NSData(NSDataCompression) _decompressedDataUsingCompressionAlgorithm:error:] ()

It looks like something is wrong with safe reallocation, however if this have been a bug, then all macOS is stuck.

Any idea, please?

Accepted Reply

It is reproducible, at least on my machine.

https://github.com/hibernat/concurrency-with-data-bug

I tested that on macOS 14.4, Xcode 15.3 (15E204a)

  • Resolved. Threads are blocked by filesystem operations.

Add a Comment

Replies

It is reproducible, at least on my machine.

https://github.com/hibernat/concurrency-with-data-bug

I tested that on macOS 14.4, Xcode 15.3 (15E204a)

  • Resolved. Threads are blocked by filesystem operations.

Add a Comment