Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AsynchronousOperation thread safety issue #355

Closed
lilyball opened this issue Aug 21, 2018 · 4 comments
Closed

AsynchronousOperation thread safety issue #355

lilyball opened this issue Aug 21, 2018 · 4 comments
Assignees
Labels
question Issues that have a question which should be addressed threading-sadness

Comments

@lilyball
Copy link

AsynchronousOperation is used in contexts where it may be completed from arbitrary background threads, but its state property is not thread-safe. In practice this is likely to work ok, because OperationQueue will receive and react to KVO notifications from the thread that mutated the state property, but there's always a risk where if OperationQueue checks e.g. isFinished on the operation at the same time that the state property is mutated on a background thread then it will violate the threading model and may read a state value that was never written.

Unfortunately Swift doesn't have access to atomic values, so I'm not quite sure what the best solution here is. If Apollo was a hybrid library I'd say this class should simply be written in Obj-C and use C11 atomics. There are a number of options for pure Swift but they're all going to be much slower than proper atomics. Possibly the best one is to use a pthread_mutex_t that's explicitly configured with the PTHREAD_MUTEX_POLICY_FIRSTFIT_NP policy (as this is an order of magnitude faster than the default "fair" lock). You could also just use objc_sync_enter(self) and objc_sync_exit(self); these use a recursive mutex, so locking/unlocking will be slower, but the system will reuse the mutexes when the associated objects deallocate which means they may be a good choice for large numbers of short-lived objects.

@designatednerd
Copy link
Contributor

@lilyball Have you seen this cause issues in practice? I can see where the theoretical issue lies, but I'm curious as to whether it's actively causing issues or is more of a "This may someday blow up in our faces and we should be aware of that"

@designatednerd designatednerd added the question Issues that have a question which should be addressed label Jul 15, 2019
@lilyball
Copy link
Author

This may someday blow up in our faces and we should be aware of that.

Or more accurately, this will likely be a source of very infrequent bad behavior or crashes, which is hard to diagnose and debug, but may possibly show up in high-usage apps.

@designatednerd
Copy link
Contributor

Yeah, fair point. There's definitely a lot of stuff where we need to get some much better practices in here, I'm just trying to figure out where to apply the duct tape first 😄

@designatednerd
Copy link
Contributor

"Fixing" this by removing the class altogether in the updated networking stack being put together in #1341. Gonna close this one out, sorry we were never able to untangle it!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Issues that have a question which should be addressed threading-sadness
Projects
None yet
Development

No branches or pull requests

3 participants