Swift thread safety?
While looking at race(), I got curious why thenUnleashZalgo() doesn't protect mutation of Handlers with dispatch_barrier_sync. It appears that any use of thenUnleashZalgo() has the potential to corrupt the handlers array.
If one queue is chaining the promise at the same time the main queue is calling thenUnleashZalgo() then the potential for data races or corruption is present.
Worse, if a background queue is fulfilling it may either observe the handlers array prior to mutation causing the handler to never be called, or it may observe the closure's presence in the array before the closure itself has been fully constructed (regardless of what order those statements happen in the Swift standard library, the compiler and the CPU are free to re-order the writes and publish the change to the array before construction of the closure's environment is captured and fully constructed)
Am I missing something obvious here?
edit: removed mistaken view of how the normal chaining works; I see how it uses the barrier inside init to protect everything. I think thenUnleashZalgo is the only actual question.
Yes this was an omission. Thanks for pointing it out.
Is there a good way to test this sort of thing? We'd need to get into the mechanism and cause the kinds of race conditions that would cause corruption.
Pushing 1.5.3