Skip to content

Conversation

jaypipes
Copy link
Collaborator

When calling a function directly from a defer statement, like so:

var err error
exit := someFuncReturningAFunctor()
defer exit(err)

The exit function is passed the err variable by value, which means
that the value of err will always be nil above.

In order to have the deferred exit function use the value of the err
variable on exit of the function, you need to wrap the exit call
inside a temporary function, like so:

var err error
exit := someFuncReturningAFunctor()
defer func() {
        exit(err)
}()

See here for more information about this odd behaviour:

https://stackoverflow.com/questions/42703707/when-defer-func-evaluates-its-parameters

The alternative solution here was to change exit's call signature to
accept a *error instead of an error, however we have code out there
in the controllers that would break if we changed exit's function
signature.

By submitting this pull request, I confirm that my contribution is made
under the terms of the Apache 2.0 license.

jaypipes added 2 commits June 15, 2022 14:37
Getting a failure when trying to use mockery to build mocks:

```
internal error: package "context" without types was imported from ...
```

Updated to modern mockery v2.13.1 and regenerated our mocks to get tests
passing.

Signed-off-by: Jay Pipes <[email protected]>
When calling a function directly from a `defer` statement, like so:

```go
var err error
exit := someFuncReturningAFunctor()
defer exit(err)
```

The `exit` function is passed the `err` variable by value, which means
that the value of `err` will always be nil above.

In order to have the deferred `exit` function use the value of the `err`
variable *on exit of the function*, you need to wrap the `exit` call
inside a temporary function, like so:

```go
var err error
exit := someFuncReturningAFunctor()
defer func() {
        exit(err)
}()
```

See here for more information about this odd behaviour:

https://stackoverflow.com/questions/42703707/when-defer-func-evaluates-its-parameters

The alternative solution here was to change `exit`'s call signature to
accept a `*error` instead of an `error`, however we have code out there
in the controllers that would break if we changed `exit`'s function
signature.

Signed-off-by: Jay Pipes <[email protected]>
@ack-bot ack-bot requested review from a-hilaly and vijtrip2 June 15, 2022 18:44
@jaypipes jaypipes requested a review from RedbackThomson June 15, 2022 18:45
@vijtrip2
Copy link
Contributor

/lgtm

@ack-bot ack-bot added the lgtm Indicates that a PR is ready to be merged. label Jun 15, 2022
@ack-bot
Copy link
Collaborator

ack-bot commented Jun 15, 2022

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: jaypipes, vijtrip2

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@ack-bot ack-bot merged commit 71e60cc into aws-controllers-k8s:main Jun 15, 2022
jaypipes added a commit to jaypipes/ack-code-generator that referenced this pull request Jun 15, 2022
Updates the generated code that defers a call to `exit(err)` for the
resource logger to wrap the defer in a temporary functor that then calls
`exit(err)`.

See aws-controllers-k8s/runtime#93 for
explanation.

Signed-off-by: Jay Pipes <[email protected]>
ack-bot pushed a commit to aws-controllers-k8s/code-generator that referenced this pull request Jun 15, 2022
Updates the generated code that defers a call to `exit(err)` for the
resource logger to wrap the defer in a temporary functor that then calls
`exit(err)`.

See aws-controllers-k8s/runtime#93 for
explanation.

Signed-off-by: Jay Pipes <[email protected]>

By submitting this pull request, I confirm that my contribution is made
under the terms of the Apache 2.0 license.
RedbackThomson pushed a commit to RedbackThomson/ack-code-generator that referenced this pull request Jun 21, 2022
Updates the generated code that defers a call to `exit(err)` for the
resource logger to wrap the defer in a temporary functor that then calls
`exit(err)`.

See aws-controllers-k8s/runtime#93 for
explanation.

Signed-off-by: Jay Pipes <[email protected]>

By submitting this pull request, I confirm that my contribution is made
under the terms of the Apache 2.0 license.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved lgtm Indicates that a PR is ready to be merged.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants