-
Notifications
You must be signed in to change notification settings - Fork 4
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
367: Fix deferred rollback on error. #391
Conversation
It is worth adding that I ran this code overnight in the fixturenet along with some intentionally broken logic that generates duplicate ChainEvent every few blocks. It ultimately generated about 3,000 errors, but stayed up and statediffing. |
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.
Only variables passed to deferred functions as parameters are evaluated at the time that the defer
is called. If instead it is not passed by parameter but is a variable in the scope of the function calling the defer, then the variable is evaluated when the deferred function is executed not when defer
is called.
https://go.dev/play/p/sYbwGE_2ay-
So I think the thing that is actually fixing the bug here is that we stop overshadowing the err
variable in a number of places, such as on https://github.com/cerc-io/go-ethereum/pull/391/files#diff-9aaf9fbe34f5adee1f41264c3457450e39afd9df2faeda20e49f5b25bcfa3f23R87 and https://github.com/cerc-io/go-ethereum/pull/391/files#diff-b51e10084bd55533c672872e492d79f8733e78febfca935f57123796253538daR850. And I think this would fix the bug without needing to pass a pointer argument to the deffered function (leave it without args) which would look a lot cleaner imo.
I agree it is overcomplicated in either case, open to simplifying it by not catching panics and letting some error messages get overshadowed and unlogged.
Corrected. My initial understanding was as you mentioned (per https://go.dev/ref/spec#Defer_statements), and I just switched to |
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.
LGTM! Looks like this bug is also present in ipld-eth-server where we use a similar defer, will copy this fix over there.
Related to #367
The problem is that the values of the variables in the closure will be evaluated at the moment of the
defer
statement, not when the function is executed. In these cases, that guarantees thaterr
will be nil, because we would have returned already otherwise.The fix is to pass in a pointer to the error instead.
With that said, I do wonder if we need such complicated logic here. It would be a lot less complicated and cleaner to do something like:
However, that doesn't deal with the panic case nor the additional logging of the code we have, so I didn't change it. Personally, I would lean toward simplicity in error handling unless we are certain we need the complicated code though, precisely to avoid issues like this one.