-
Notifications
You must be signed in to change notification settings - Fork 121
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: prevent stringer deadlock #116 #144
Merged
Merged
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Hey @nbgraham thanks for looking into this and providing the fix! This looks good to me. |
JacobOaks
approved these changes
Feb 6, 2024
r-hang
pushed a commit
that referenced
this pull request
Jul 10, 2024
related #144 Whether or not it is a good idea to generate an ISGOMOCK method for fmt.Stringer deadlock
JacobOaks
added a commit
to JacobOaks/mock
that referenced
this pull request
Sep 18, 2024
A deadlock related to controller calling Stringer on mocks themselves was revealed in uber-go#116. uber-go#144 solved this deadlock by adding a generated `ISGOMOCK()` method to all generated mocks, and then checking for it before calling `.String()` on arguments. This reveals an exported method on each generated mock that: * Bloats the generated code * Can be taken dependency on in strange ways via Hyrum's Law * Technically opens up another route for naming collision. This PR attempts to clean up this type of check by instead generating an unexported field in generated mock structs instead, and checks for it using reflect. This hides this implementation detail of gomock/mockgen better, and produces less generated bloat. This PR then regenerated all generated code for tests/example via `go generate`. Note that, importantly, the regression test added in uber-go#144 still passes with this PR. Ref: uber-go#193
JacobOaks
added a commit
to JacobOaks/mock
that referenced
this pull request
Sep 18, 2024
A deadlock related to controller calling Stringer on mocks themselves was revealed in uber-go#116. uber-go#144 solved this deadlock by adding a generated `ISGOMOCK()` method to all generated mocks, and then checking for it before calling `.String()` on arguments. This reveals an exported method on each generated mock that: * Bloats the generated code * Can be taken dependency on in strange ways via Hyrum's Law * Technically opens up another route for naming collision. This PR attempts to clean up this type of check by instead generating an unexported field in generated mock structs instead, and checks for it using reflect. This hides this implementation detail of gomock/mockgen better, and produces less generated bloat. Note that, importantly, the regression test added in uber-go#144 still passes with this PR. Ref: uber-go#193
JacobOaks
added a commit
that referenced
this pull request
Sep 18, 2024
A deadlock related to controller calling Stringer on mocks themselves was revealed in #116. #144 solved this deadlock by adding a generated `ISGOMOCK()` method to all generated mocks, and then checking for it before calling `.String()` on arguments. This reveals an exported method on each generated mock that: * Bloats the generated code * Can be taken dependency on in strange ways via Hyrum's Law * Technically opens up another route for naming collision. This PR attempts to clean up this type of check by instead generating an unexported field in generated mock structs instead, and checks for it using reflect. This hides this implementation detail of gomock/mockgen better, and produces less generated bloat. Note that, importantly, the regression test added in #144 still passes with this PR. Ref: #193
apricote
pushed a commit
to hetznercloud/fleeting-plugin-hetzner
that referenced
this pull request
Oct 18, 2024
…eting-plugin-hetzner!135) This MR contains the following updates: | Package | Type | Update | Change | |---|---|---|---| | [go.uber.org/mock](https://github.com/uber/mock) | require | minor | `v0.4.0` -> `v0.5.0` | --- ### Release Notes <details> <summary>uber/mock (go.uber.org/mock)</summary> ### [`v0.5.0`](https://github.com/uber-go/mock/releases/tag/v0.5.0) [Compare Source](uber-go/mock@v0.4.0...v0.5.0) #### 0.5.0 (15 Oct 2024) ##### Added - [#​153][]: Add `--write_command_comment` flag to specify whether to include `Generated by this command` comment. - [#​191][]: Add `--build_constraint` flag to add `//go:build` directives to generated mocks - [#​214][]: Add gob mode to support custom package loading techniques in place of --exec_only ##### Changed - [#​181][]: Made mockgen faster by changing flags passed to `go list`. - [#​183][]: Made `Cond` matcher generic. - [#​204][]: Removed `ISGOMOCK()` from generated mocks. - [#​207][]: Deprecated reflect mode and replaced it with the new package mode. ##### Fixed - [#​144][]: Fix a deadlock that can happen when mocking an interface that matches `fmt.Stringer`. - [#​168][]: Fix an issue where the "generated by" comment was being included in the package comment of generated mocks. [#​144]: uber-go/mock#144 [#​153]: uber-go/mock#153 [#​168]: uber-go/mock#168 [#​181]: uber-go/mock#181 [#​183]: uber-go/mock#183 [#​191]: uber-go/mock#191 [#​204]: uber-go/mock#204 [#​207]: uber-go/mock#207 [#​214]: uber-go/mock#214 Thanks to [@​tulzke](https://github.com/tulzke) [@​JacobOaks](https://github.com/JacobOaks) [@​ARR4N](https://github.com/ARR4N) [@​sashamelentyev](https://github.com/sashamelentyev) [@​sywhang](https://github.com/sywhang) [@​fasmat](https://github.com/fasmat) [@​eyasy1217](https://github.com/eyasy1217) [@​ghouscht](https://github.com/ghouscht) [@​tie](https://github.com/tie) [@​Neo2308](https://github.com/Neo2308) [@​carson-brill](https://github.com/carson-brill) [@​alexandear](https://github.com/alexandear) [@​sodul](https://github.com/sodul) [@​nbgraham](https://github.com/nbgraham) for their contributions this release. </details> --- ### Configuration 📅 **Schedule**: Branch creation - At any time (no schedule defined), Automerge - At any time (no schedule defined). 🚦 **Automerge**: Disabled by config. Please merge this manually once you are satisfied. ♻ **Rebasing**: Whenever MR is behind base branch, or you tick the rebase/retry checkbox. 🔕 **Ignore**: Close this MR and you won't be reminded about this update again. --- - [ ] <!-- rebase-check -->If you want to rebase/retry this MR, check this box --- This MR has been generated by [Renovate Bot](https://github.com/renovatebot/renovate). <!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzOC4xMjcuMCIsInVwZGF0ZWRJblZlciI6IjM4LjEyNy4wIiwidGFyZ2V0QnJhbmNoIjoibWFpbiIsImxhYmVscyI6W119-->
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fixes #116
Problem
If your mock an interface that matches the fmt.Stringer interface:
Then your unit tests can deadlock if you provide that mock as an expected argument to a call that is not matched (i.e. fails the test).
Because, when printing the error message for the call that was not matched, it calls
String()
on all arguments that support it, including the mock.But each call to a mock is protected with a mutex, and the previous call (that was not matched) has not yet exited.
Solution
The solution has two parts