rapidfsub
Why require_atomic? defaults to true?
Just one use of “manage_relationship” makes action not able to be atomic.
I think the default value of require_atomic? should be “false”.
Why is it “true”?
Marked As Solved
zachdaniel
require_atomic? is a safety guard. It’s like marking code as safe or unsafe in another language. It has little to no value if it defaults to false. It is expected that you will have to turn it off relatively often. That is still important though, as when you see that error you should consider if there is anything to be done for it.
This configuration:
config :ash, :require_atomic_by_default?, false, was made only to assist with the upgrade and will be removed in the future. I would not suggest using it at all.
Also Liked
zachdaniel
Writing atomic code is harder, but here is an example of a case where it will pay off significantly:
update :add_ten_to_score do
change fn changeset, _ ->
Ash.Changeset.change_attribute(changeset, :score, changeset.data.score + 10)
end
end
The action above raises an error about it not being atomic. In this case, the effect here is that if you ran this same update concurrently on the same record, it’s possible to have inconsistent results.
i.e with a record that has a score of 0
Action 1: record is read, current score: 0
Action 2: record is read, current score: 0
Action 1: new score is determined, new score 10
Action 2: new score is determined, new score 10
Action 1: data is written, new score 10
Action 2: data is written, new score 10
So now even though the action was called twice, and you’d expect a new score of 20, you actually have a new score of 10.
By using an action like this:
update :add_ten_to_score do
change atomic_update(:score, expr(score + 10))
# or use the built in
change increment(:score, amount: 10)
end
you now have a fully atomic update that does not have the same problems as described above
I’ve made some tweaks to the atomic update docs to potentially help with clarity. Update Actions — ash v3.29.3
rapidfsub
I think you are not an ash framework user.
You don’t get what “not that simple” means.
Just one use of manage_relationship makes action not able to be atomic.
And manage_relationship is a basic feature in ash,
which is for creating two related resources in one action.
Last Post!
rapidfsub
I am also a race condition worrying guy.
But I think many folks would write a spark dsl for “automatically setting require_atomic? false”,
because require_atomic? true is rare case for most apps. ![]()
Popular in Questions
Other popular topics
Latest Ash Threads
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #elixirconf-us
- #advent-of-code
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #hex
- #security









