367: Fix deferred rollback on error. #391
Merged
telackey
merged 2 commits from 2023-06-01 00:36:37 +00:00
telackey/367v5 into v1.11.6-statediff-v5
Dismiss Review
Are you sure you want to dismiss this review?
Labels
Clear labels
bug
critical
duplicate
enhancement
epic
help wanted
in progress
invalid
low priority
question
rebase
v1
v5
wontfix
Copied from Github
Kind/Breaking
Kind/Bug
Kind/Documentation
Kind/Enhancement
Kind/Feature
Kind/Security
Kind/Testing
An issue or PR manually copied from GitHub.
Breaking change that won't be backward compatible
Something is not working
Documentation changes
Improve existing functionality
New functionality
This is security issue
Issue or pull request related to testing
Priority
Critical
The priority is critical
Priority
High
The priority is high
Priority
Low
The priority is low
Priority
Medium
The priority is medium
Reviewed
Confirmed
Issue has been confirmed
Reviewed
Duplicate
This issue or pull request already exists
Reviewed
Invalid
Invalid issue
Reviewed
Won't Fix
This issue won't be fixed
Status
Abandoned
Somebody has started to work on this but abandoned work
Status
Blocked
Something is blocking this issue or pull request
Status
Need More Info
Feedback is required to reproduce issue or to continue work
No labels
Milestone
No items
No Milestone
Projects
Clear projects
No projects
No Assignees
Notifications
Due Date
No due date set.
Dependencies
No dependencies set.
Reference: cerc-io/go-ethereum#391
Reference in New Issue
Block a user
Blocking a user prevents them from interacting with repositories, such as opening or commenting on pull requests or issues. Learn more about blocking a user.
Related to https://github.com/cerc-io/go-ethereum/issues/367
The problem is that the values of the variables in the closure will be evaluated at the moment of the
deferstatement, not when the function is executed. In these cases, that guarantees thaterrwill 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.
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.
Only variables passed to deferred functions as parameters are evaluated at the time that the
deferis 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 whendeferis 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
errvariable 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
=so the variables weren't masked, but stepping through the code I kept seeing the issue until switching to the pointer so concluded that perhaps my understanding was wrong. I think it must have been a build issue though, and I was using an old container, as I fixed it this time, and stepped through the code again, and all is working as expected.LGTM! Looks like this bug is also present in ipld-eth-server where we use a similar defer, will copy this fix over there.