7 ms·
Avoiding race conditions in Swift
- eridius 8y agoThis code has a significant and common flaw: If the token is cached, it invokes the handler synchronously. You should never design a function with a completion handler that sometimes invokes it synchronously and sometimes asynchronously. That can cause all sorts of problems, the simplest of which is a caller that expects it to be asynchronous and ends up performing work out-of-order because the handler was invoked synchronously. The final form of the code in this article accidentally fixes this, by moving the work it does onto a background queue, but this just introduces a separate flaw: it performs the callback from its internal private queue. This isn't necessarily a problem, except in that it will block any other work from running on that queue unnecessarily (as it's a serial queue), but in more complicated code, having external code running on your private queue can indeed cause problems. --- There's also another issue, which is that if the AccessTokenService gets deinited before the loader calls its callback, the callback is dropped on the floor. In general, you should not design a function that takes a completion callback with conditions where the completion callback is never fired. There's 3 reasonable behaviors that could be implemented here: 1. Have the callback keep the AccessTokenService alive. It's pretty common for networking operations to keep themselves alive while executing, so this is probably the approach I'd take. 2. Have the AccessTokenService fire all of its pending completion handlers with an error in deinit. The error should ideally represent the notion that the operation was cancelled. 3. Change the AccessTokenLoader completion handler to look more like the initial version, where it still calls the callback even if self is nil. With the pending completion handlers approach, this requires moving the pending completion handlers into a reference-type wrapper and strongly referencing that from the AccessTokenLoader callback. In this case, that's pretty much the same as approach #1, so you should just go ahead and use approach #1, but in a more complex class this approach might have some utility.
- anothergoogler 8y agoYou have it backwards, it's the caller's bug if they assume order in an async process.
- SamReidHughes 8y agoNo no no no no. He's right, and this is a hard-learned lesson that many practitioners have discovered independently. Both the caller and callee face huge problems of reentrant function calls when you mix up the behavior. Generally the right choice is to make every callback invoked asynchronously, unless you need to do something else.
- solarexplorer 8y agoExactly. UITableView is a good example of how _not_ to do things.
- dep_b 8y agoYeah the mixup of MVC layers is terrible and anything beyond a hard reload data has many edge cases.
- alexashka 8y agoCan you explain what problems someone faces if a callback is executed immediately?
- Matthias247 8y agoOne usual issue on the callee side is that the code inside the callback might call again into the same component (here: AccessTokenLoader::retrieveToken). When this call is done, state inside the component is modified before the callback returns. When the callback returns the component is now no longer in the state that it was, which might not be expected. In C++, it could even happen that the object was deleted within the callback. Then accessing any local field after the callback returns leads to a segfault. One kind of remedy is to only call callbacks at the end of the the logic, and don't touch anything anymore after the callback returns. That works in some cases - however scheduling the callback might be easier to reason about. Then there are also issues on the caller side, if the caller doesn't expect that the callback is sometimes made synchronously and sometimes asynchronously. If it's assumed that the callback happens asynchronously and some state is modified in it, the code that runs after invoking the initial function might work on wrong assumptions (wasn't aware that state could have been modified).
- charleslmunger 8y agoAbsolutely this. See https://google.github.io/guava/releases/23.0/api/docs/src-html/com/google/common/util/concurrent/ListenableFuture.html#line.141 https://google.github.io/guava/releases/23.0/api/docs/src-ht... For a discussion on some of the dangers of invoking completion listeners synchronously.
- tumdum_ 8y agoThis page is completely unusable without js, yet all it is, is short static document :(
- raphlinus 8y agoThis might be contentious, but I'm going to say it. If your program is multithreaded in a nontrivial way, Swift is not the language for you, at least yet. First, this article understates the danger significantly. It's not just that your token loader might be called twice, if you call this from two different threads you're fully in undefined behavior. Swift is "safe-ish" in that it's mostly safe in single-threaded contexts, but with data races that breaks down. It's even more confusing because some things are properly atomic (reference counts, manipulation of value types like arrays), but some aren't (access to fields in classes). Second, the language is missing basic features that in modern programming would help tame this beast - the most important of which is a wrapper for internally mutable state that is protected by a mutex. Grand Central Dispatch can help with some of this. Third, there are unexpected performance losses. For example, the closure for running a block on a sync "queue" is heap allocated (because the type of such a closure is the same for sync and async, and of course the latter has to be heap allocated). This will probably be fixed, but I think is symptomatic. Fourth, there's no official concurrency model. Again, it's confusing because there are some things that are obviously intended to run with concurrency. In fairness, Rust doesn't have a formal concurrency model yet either, but it is straightforward to apply reasoning from C++, and also reasonable to expect guarantees if the primitives such as Mutex, channels, etc., are used properly. Fifth (and related), there are no atomics in the language or the standard library. Apparently OSAtomic is deprecated, and they now recommend the use of C atomics, but you're in the territory about having to reason about the polyglot combination of Swift and C. Likely all this will improve, but in the meantime it's important to realize how immature Swift's concurrency is.
- saagarjha 8y ago> It's not just that your token loader might be called twice, if you call this from two different threads you're fully in undefined behavior. This is addressed in the article, and a resolution is provided. > It's even more confusing because some things are properly atomic (reference counts, manipulation of value types like arrays) Modifying an array is not atomic, AFAIK. You cannot safely do this concurrently.
- raphlinus 8y ago
- dep_b 8y agoNice article. It came to a few conclusions I came before: buffering token requests in a queue once the first request has been kicked off and using named threads for things that need to happen off main but in-order. I can follow some of the critiques here but implementing both solutions really makes things a lot better than doing without.
- asdfasgasdgasdg 8y agoThis seems craaaazy complicated. In C++, I'm only familiar with the abseil libraries for thread safety, but the way to make sure multiple threads don't access shared state is: absl::MutexLock l(&mutex_); // Critical section. The idea that you have to write a "waiting queue" every time you need to guard shared state in Swift is mind boggling. Surely not!
- jchb 8y agoThe waiting queue mechanism in the article is related to the specific requirements of that auth network request rather than Swift in general. The queues provided by Apples GCD are an abstraction above threads and mutexes. The equivalent to your critical section example is: let dispatchQueue = DispatchQueue(label: "com.mycompany.myComponent") // Initialize queue, just as you would with the mutex, label will show in debugger dispatchQueue.sync { // Critical section } There are several wins here. One is that gcd controls thread creation as needed. But the bigger win is when you have a longer running critical section and mustn't block the calling UI (main) queue. Then you can simply do an async dispatch on the queue and callback to the UI queue on completion. GCD will also introduce the necessary memory barriers for passing data between the queues.
- coldcode 8y agoI find the lack of language level locking a benefit in Swift, using GCD is really easy now that the syntax is much more polished. I have yet to run into a race condition. When I worked (years ago) on a 3D game engine in C++ we actually removed most of the multi-threading because it was too hard to reason what it was doing and avoid threading errors and race conditions.
- sovande 8y agoSwift has easy access to pthread mutex and you can do that. serialQueue.async { // Critical section } is GCD's way of doing the same with the added benefit that the calling code does not have to wait. Problems in the parent code could be solved by using a mutex instead of GCD in this case. The code _has_ to wait for the access token to be loaded and not blocking does not make sense here unless you also introduce some kind of Promises.
- jordansmithnz 8y agoA while ago I built an app data framework that used similar logic (although a bit more abstracted) to coalesce every GET type request. It made me wonder why network libraries don’t do this for the actual requests by default (or perhaps they do?). I’m sure there are some cases where simultaneous requests are intentional, but it seems like this would be infrequent.
- skohan 8y agoI guess the argument against would be that it adds complexity for a use-case which is probably infrequent. How often does the average HTTP client request the same resource multiple times concurrently? In what subset of those cases does it actually matter if requests are redundant? Why waste your user's clock-cycles on an edge case?