-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Update to thread warnings. #773
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
Conversation
stephencelis
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice!
|
Now I have to do something like: just to avoid this warning. That doesn't make any sense to me, should this really include |
Unfortunately we think it must. As brought up in #772, when an effect's completion runs on the wrong thread, there is the possibility for there to be a runtime crash, so one must be diligent when working with background threads in general, even during fire-and-forget. |
|
Even when I am not mutating anything or doing any logic after (e.g. |
|
I find the new behavior surprising as well. The description of the original PR #772 said:
So at present would a More broadly, I feel like having to worry about scheduling back into the system breaks the semantics of 'fire and forget' (at least how I understood it). Related, if a fireAndForget is cancellable is it really fireAndForget? |
|
I know it sounds counter-intuitive, but even If you look at the implementation of swift-composable-architecture/Sources/ComposableArchitecture/Store.swift Lines 390 to 394 in 86bcd08
Note that this work has nothing to do with cancelling effects via the So, even if you previously were not seeing crashes in your application's due to data races, the fact is that the possibility was always there. Effects completing on different threads will eventually cause a problem, even if they are "fire-and-forget", and this is why we'd like to warn loudly if we detect it. Having said all of that, it does sound like we have room for improvement in the docs. Perhaps we should detail all of this information in the docs for |
|
Right on, thanks @mbrandonw. Agreed this is totally a mental model and docs issue. I'd suggest including some info in the error message from this change as well. I'll update my app shortly and see if anything else comes up as I transition to this new understanding. |
|
I’m still curious if TCA can do this automatically behind the scenes, since it’s apparently needed for pretty much every case. |
|
Unfortunately there is no way to do that really. The simplest way to do that would be to chain on a As documented on the store we take a strong stance on not baking any threading/locking into the library at all. We feel the simpler approach is to make users of the effects be explicit with how they behave. It's unfortunate that this change has caused disruptions, but we feel it's for good reason! In the future we'll try to do a better job of communicating this change to the community with better documentation, blog posts, etc. |
|
@mbrandonw quick note while I'm updating my app - I added a Without diving into the internals of TestStore, I'm wondering if there's some more structure we could add to this whole thing such that the error could be "the effect [id]" instead of "an effect" Would combining the need to receive and name each effect provide some clarity all around? For example: Similar to to how cancellation works now, but now for all effects. I wonder if this could simplify the mental model overall, since the rule would be to always identify and schedule any effects that receive/complete on a different thread. |
|
Thanks for the update @rcarver, it's good for us to know what the knock-on effects are of this change. One thing that comes to mind with your situation is whether or not it is appropriate for you to use an immediate scheduler. Then you wouldn't need to worry about the completion. Are you using a test scheduler specifically because time is involved? If the only asynchrony in your effects is just simple And as for identifying effects. It may not be clear at first, but the line the test failure points to is the action that kicked off the effect which is still running. So hopefully that should narrow down which effects could be causing this because you only have to look up that action in your reducer. Adding an explicit identifier to the effect is interesting, and something we've actually explored a ton to help out with debugging. However, we don't feel we have yet solved the ergonomics around it. You end up having to thread the information through many layers, and it's quite easy to lose the information (basically every time you erase the effect). We want to spend more time thinking about it, but it's a hard problem. |
|
thanks @mbrandonw I'll try the immediate scheduler, that should simplify at least a few cases. I did know about the error line which is great - hopefully tracking down the rest of these failures will be easy enough. |
A few small updates from #772