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

Refactor defer-close with error handling #378

Closed
wants to merge 7 commits into from
Closed

Conversation

facuMH
Copy link

@facuMH facuMH commented Dec 16, 2024

This PR contributes to https://github.com/Fantom-foundation/sonic-admin/issues/90

This PR adds real life use cases of both functions provided the caution package to properly handler errors in case of defer and close.

This PR fixes some of the unhandled errors reported by the external audit. Most (if not all) of them are from either defer close patterns or similar cleanups or unhandled close errors.

Test list to verify we don't have double closes (or other errors) and panic now because error is no longer ignored:

  • run make test
  • Norma test
    • demonet/static.yml
    • demonet/dynamic.yml
  • testing suggestions ?

@facuMH facuMH self-assigned this Dec 16, 2024
@facuMH facuMH requested a review from LuisPH3 December 16, 2024 15:14
Copy link

@LuisPH3 LuisPH3 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I saw some details that need handing;

Additionally, I would like to avoid using caution.CloseAndReportError for scenarios without defer, it does nonstandard error handling, and it may be a little convoluted to read. We can talk about a replacement.

@facuMH facuMH requested a review from LuisPH3 December 18, 2024 11:02
@facuMH facuMH mentioned this pull request Dec 18, 2024
5 tasks
LuisPH3
LuisPH3 previously approved these changes Dec 18, 2024
Copy link
Collaborator

@HerbertJordan HerbertJordan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do not really like that most of this refactoring is introducing named return values for non-trivial functions. It makes functions harder to read, and without an IDE highlighting it is difficult to verify that a return value name is not shadowed in a nested scope.

But I do understand that this seems to be a requirement imposed by the caution package, and alternatives might be even more cumbersome.

However, I would like to ask to reconsider the naming and placement of the function IfErrorAddContext as addressed in the comments. Please have a look.

HerbertJordan
HerbertJordan previously approved these changes Jan 15, 2025
@facuMH facuMH force-pushed the facundo/use-caution branch from 469c960 to 1f78e22 Compare January 16, 2025 11:20

return nil
// Close will resets terminal mode.
caution.CloseAndReportError(&err, prompt.Stdin, "failed to reset terminal input")
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this case is not defered, close can be called using normal procedures.

@@ -117,5 +118,7 @@ func (tt *index) scanPatternVariant(pos uint8, variant common.Hash, start uint64
break
}
}
onMatched(nil)
if _, err := onMatched(nil); err != nil {
log.Warn("searchParallel", "err", err)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

since the first time i saw this line, i dont really know what to do with it.
The search algorithm will look for something not returning an error and producing errors may be the intended use.

The context we add to warn is not good.

  • Write a complete sentence in warn: "error produced during parallel search"
  • Add a comment explaining the issue, so that the problem is understood in case this line turns too verbose (which could affect performance of some hot region)

@HerbertJordan @kjezek do you know what this code is about?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code is the implementation of the log-filter feature. You can send RPC queries to the service asking for log messages listing a given set of topics. This function is searching for those and does a call-back on onMatched for each element and, as it looks like, a final one passing a nil to indicate that all matches have been reported and that there are no more.

The only time this function is actually used is in line 94 above, where onMatched returns an error if, for instance, the execution context is canceled. Another option is that the consumer of the match failed to consume it, which seems to be ignored, as shown in line 116.

I guess the best option here, matching the action in line 116, is to simply ignore the error.

@@ -81,9 +82,9 @@ func makeEngine(chaindataDir string, cfg Configs) (*abft.Lachesis, *vecmt.Index,
}
defer func() {
if err != nil {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

//	}
func ExecuteAndReportError(err *error, f func() error, message string) {
	fErr := f()
	if fErr != nil {
		*err = errors.Join(*err, fmt.Errorf("%s: %w", message, fErr))
	}
}

this if is superfluous.

@@ -117,5 +118,7 @@ func (tt *index) scanPatternVariant(pos uint8, variant common.Hash, start uint64
break
}
}
onMatched(nil)
if _, err := onMatched(nil); err != nil {
log.Warn("searchParallel", "err", err)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code is the implementation of the log-filter feature. You can send RPC queries to the service asking for log messages listing a given set of topics. This function is searching for those and does a call-back on onMatched for each element and, as it looks like, a final one passing a nil to indicate that all matches have been reported and that there are no more.

The only time this function is actually used is in line 94 above, where onMatched returns an error if, for instance, the execution context is canceled. Another option is that the consumer of the match failed to consume it, which seems to be ignored, as shown in line 116.

I guess the best option here, matching the action in line 116, is to simply ignore the error.

@LuisPH3
Copy link

LuisPH3 commented Feb 3, 2025

Please, Add one deferred error missing : gossip/handler.go:700:19: defer msg.Discard()

@facuMH
Copy link
Author

facuMH commented Feb 6, 2025

moved to 0xsoniclabs/sonic#7

@facuMH facuMH closed this Feb 6, 2025
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.

3 participants