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

fix incorrect use of loop variable #16872

Merged
merged 1 commit into from
Oct 4, 2022

Conversation

renatolabs
Copy link
Contributor

This fixes a couple of references to loop variables in parallel tests
and deferred functions. When running a parallel test (calling
t.Parallel()) combined with the table-driven pattern, it's necessary
to copy the test case loop variable, otherwise only the last test case
is exercised. This is documented in the testing package:

https://pkg.go.dev/testing#hdr-Subtests_and_Sub_benchmarks

defer statements that invoke a closure should also not reference a
loop variable directly as the referenced value will change in each
iteration of the loop.

Issues were automatically found with the loopvarcapture linter.

@renatolabs renatolabs requested a review from a team August 24, 2022 16:52
@hashicorp-cla
Copy link

hashicorp-cla commented Aug 24, 2022

CLA assistant check
All committers have signed the CLA.

@peteski22
Copy link

Hi @renatolabs, thanks for your submission! Please could I get you to add a change log entry (https://github.com/hashicorp/vault/blob/main/CONTRIBUTING.md#changelog-entries)?

@renatolabs
Copy link
Contributor Author

Hi @renatolabs, thanks for your submission! Please could I get you to add a change log entry (https://github.com/hashicorp/vault/blob/main/CONTRIBUTING.md#changelog-entries)?

Done, thank you for the reminder!

Copy link
Contributor

@maxcoulombe maxcoulombe left a comment

Choose a reason for hiding this comment

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

Hey @renatolabs, thanks for this contribution!

False positives in tests can be really sneaky, I'm glad this can be fixed before a problem gets introduced unnoticed.

Would you mind triggering the CI so it has a chance to pass before I can approve the PR?
You can either apply the suggested change to the changelog or push an empty commit to trigger the validation steps:
git commit --allow-empty -m "trigger ci" && git push origin X

@@ -0,0 +1,3 @@
```release-note:bug
Copy link
Contributor

Choose a reason for hiding this comment

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

Since the changes eliminate a redundant check and fixes tests hiding possible problems, but do not fix a logic error in the business code itself, may I suggest updating this to improvement instead of bug?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure, changed to improvement 👍

@maxcoulombe
Copy link
Contributor

Seems like the test failures are not flukes. I'm able to reproduce them when running the pull-request locally with make test. I'd be ok to approve the changes if we can get a green test run.

I tried the proposed changes on the tip of hashicorp/vault and it seems to pass. Would it be possible to rebase to a newer commit to see if it does the trick? Thanks again.

@renatolabs
Copy link
Contributor Author

Seems like the test failures are not flukes. I'm able to reproduce them when running the pull-request locally with make test. I'd be ok to approve the changes if we can get a green test run.

I tried the proposed changes on the tip of hashicorp/vault and it seems to pass. Would it be possible to rebase to a newer commit to see if it does the trick? Thanks again.

Rebased 👍

@ncabatoff
Copy link
Collaborator

Looks like there's a panic, which I think is introduced by this change:

[signal SIGSEGV: segmentation violation code=0x1 addr=0x28 pc=0x43f9231]

goroutine 24844 [running]:
github.com/hashicorp/vault/command.(*ServerCommand).Run.func2(0xc000095400?)
	/home/circleci/go/src/github.com/hashicorp/vault/command/server.go:1333 +0x31
github.com/hashicorp/vault/command.(*ServerCommand).Run(0xc00120b860, {0xc002ab21e0, 0x2, 0x2})
	/home/circleci/go/src/github.com/hashicorp/vault/command/server.go:1773 +0x41e5
github.com/hashicorp/vault/command.TestServer_ReloadListener.func1()
	/home/circleci/go/src/github.com/hashicorp/vault/command/server_test.go:140 +0x5f
created by github.com/hashicorp/vault/command.TestServer_ReloadListener
	/home/circleci/go/src/github.com/hashicorp/vault/command/server_test.go:139 +0x56c

@renatolabs
Copy link
Contributor Author

renatolabs commented Sep 30, 2022

I suppose this is related to this section

// There is always one nil seal. We need to skip it so we don't start an empty Finalize-Seal-Shamir
// section.
if seal == nil {
    continue
}

which exists in command/operator_diagnose.go but not in command/server.go. So in that sense, this commit exposed (rather than introduced) the issue.

But I'll let folks with more knowledge of the codebase to weigh in; let me know if you want me to any changes here!

@ncabatoff
Copy link
Collaborator

But I'll let folks with more knowledge of the codebase to weigh in; let me know if you want me to any changes here!

Well, I agree with your analysis: you've exposed a bug, not introduced one. And I agree it makes sense to check for nil seals in that loop so we don't panic.

This fixes a couple of references to loop variables in parallel tests
and deferred functions. When running a parallel test (calling
`t.Parallel()`) combined with the table-driven pattern, it's necessary
to copy the test case loop variable, otherwise only the last test case
is exercised. This is documented in the `testing` package:

https://pkg.go.dev/testing#hdr-Subtests_and_Sub_benchmarks

`defer` statements that invoke a closure should also not reference a
loop variable directly as the referenced value will change in each
iteration of the loop.

Issues were automatically found with the `loopvarcapture` linter.
@renatolabs
Copy link
Contributor Author

But I'll let folks with more knowledge of the codebase to weigh in; let me know if you want me to any changes here!

Well, I agree with your analysis: you've exposed a bug, not introduced one. And I agree it makes sense to check for nil seals in that loop so we don't panic.

Added a similar check to command/server.go 👍

@ncabatoff ncabatoff merged commit eb338de into hashicorp:main Oct 4, 2022
@ncabatoff
Copy link
Collaborator

Thanks @renatolabs !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants