I was working through some of my code, and building with
swift build -Xswiftc -strict-concurrency=complete
and I found that my use of mach_task_self_
is not safe in Swift 6:
reference to var 'mach_task_self_' is not concurrency-safe because it involves shared mutable state; this is an error in Swift 6
It seems a solve would be to use the @preconcurrency
attribute in my Darwin import:
@preconcurrency import Darwin
While this works, I find it a bit odd, as the documentation seems to indicate this attribute is meant to be a stopgap measure. To have to apply it to a core module feels strange to me.
Perhaps I shouldn't expect low-level system API's to conform to Swift's new concurrency model. But I also feel like mach_task_self_
would be used a lot, to the point where everyone just having @preconcurrency
behind their import makes its intended use as a stopgap measure make less sense.
It seems there is a functional implementation of mach_task_self_
in the XNU kernel syscalls. But it doesn't appear to actually be usable immediately from Swift.
To note, this "involves share mutable state" bit also happens with bootstrap_port
. And trying to use
task_get_special_port(
mach_task_self_, TASK_BOOTSTRAP_PORT, ...)
just moves the issue back to mach_task_self_
.
Are there any plans on allowing these Mach API's (and similar) be used in Swift 6 and beyond that go beyond simply guidance to add the @preconcurrency
attribute?