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

367: Fix deferred rollback on error. #391

Merged
merged 2 commits into from
Jun 1, 2023
Merged

Conversation

telackey
Copy link

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 that err 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:

	base, err := tx.db.Begin(ctx)
	if err != nil {
		return err
	}
	defer base.Rollback()

        // do stuff...
        return base.Commit()

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.

@telackey
Copy link
Author

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.

Copy link
Collaborator

@i-norden i-norden left a 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.

@telackey
Copy link
Author

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.

@telackey telackey requested a review from i-norden May 31, 2023 21:01
Copy link
Collaborator

@i-norden i-norden left a 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.

@telackey telackey merged commit bac14cc into v1.11.6-statediff-v5 Jun 1, 2023
5 checks passed
@telackey telackey deleted the telackey/367v5 branch June 1, 2023 00:36
telackey added a commit that referenced this pull request Jun 1, 2023
telackey added a commit that referenced this pull request Jun 8, 2023
* Backport PR #391 to v4.

* Backport PR #392 to v4.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants