How to use withTaskCancellationHandler properly?

Data races have undefined behavior, so technically anything can happen. See e.g. this answer from @John_McCall: Race condition behaviors - #13 by John_McCall

In the code in question, one concurrency context assigns a class instance to an unsynchronized property (which is previously nil), and another concurrency context calls a method on that property (if not nil). I don't know if anything may actually go wrong with this in real life, but even if it doesn't right now, that may change once the code is compiled with a different compiler version or optimization level.