Agreed. For now, I don't think going the AsyncAlgorithms route buys me anything.
I now have a working version with my home-made watchdog sub-task.
My original intent was to wrap this into a (blocking) non-async method, but I couldn't figure out how to do that since waiting for the result of the (async) sub-task needs an await and that's not allowed in a synchronous method.
The second problem was one that only showed up once I tried to wrap this into a (now async) helper method is the reference to the deviceEnumerationTask (line 4 in my post above) from within that task (so the watchdog can cancel that task). If it's a global variable, the task closure doesn't need to capture it, and everything is fine. But if it's a local variable, the closure wants to capture it, and refuses to do so, since it's not yet initialized (the closure is on the right-hand side of the assignment that initializes it).
This looks like
let outer = Task {
let inner = Task {
outer.cancel()
}
}
One approach for that that I couldn't get to work was the attempt to avoid the reference to the variable storing the outer task altogether, and instead get it from within the task, but since I want the parent of the inner task, and not the inner task itself, that didn't work.
TaskGroup was another idea, since there the watchdog could cancel itself (so wouldn't need a reference to outer), leading (hopefully) to cancelling the outer task / group, but then I'm missing the ability to pause and restart the watchdog, which I've so far done by cancelling its task and replacing it with a new one to restart.
So in the end I made outer an optional (so it is initialized during the capture), and that works but makes things kind of ugly since now it's mutable and no longer a let (so I had to put everything in an actor), and of course makes an optional that really cannot be nil but needs to be used.
Topic:
App & System Services
SubTopic:
Core OS
Tags: