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

closure variable modifications are losing in unittest #53697

Closed
ymcvalu opened this issue Jul 6, 2022 · 3 comments
Closed

closure variable modifications are losing in unittest #53697

ymcvalu opened this issue Jul 6, 2022 · 3 comments
Labels
FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided.
Milestone

Comments

@ymcvalu
Copy link

ymcvalu commented Jul 6, 2022

What version of Go are you using (go version)?

$ go version
go version go1.17.11 darwin/amd64

Does this issue reproduce with the latest release?

i can't reproduce on go1.18.3

What operating system and processor architecture are you using (go env)?

go env Output
$ go env

GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOINSECURE=""
GOOS="darwin"
GOPRIVATE=""
GOTMPDIR=""
GOVCS=""
GOVERSION="go1.17.11"
GCCGO="gccgo"
AR="ar"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"

What did you do?

func Recover(ctx context.Context, cbs ...func(ctx context.Context, err error)) {
	r := recover()
	if r == nil {
		return
	}

	err, is := r.(error)
	if !is {
		err = fmt.Errorf("panic with err: %v", r)
	} else {
		err = fmt.Errorf("panic with err: %w", err)
	}

	log.Stack(ctx, "goroutine panic", zap.Error(err))
	for _, cb := range cbs {
		cb(ctx, err)
	}
}

func LP() (err error) {
	ctx := context.Background()
	cb := func(ctx context.Context, pe error) {
		err = pe
		log.Info(ctx, "===> cb run", zap.Error(err))
	}
	defer Recover(ctx, cb)

	panic("test")
}
func main() {
	fmt.Println("===>", LP())
}

func TestLP(t *testing.T) {
	err := LP()
	t.Log("===>", err, err == nil)
}

What did you expect to see?

the LP func should returns a non-nil error

What did you see instead?

when i using go run main.go, the LP func returns a non-nil error,
but i get a nil error when using go test -v -count=1 -run=TestLP

@ymcvalu ymcvalu changed the title closure variable modifications are missing in unittest closure variable modifications are losing in unittest Jul 6, 2022
@ZekeLu
Copy link
Contributor

ZekeLu commented Jul 6, 2022

This issue has been fixed as a side effect of 0a0e3a3, which has been shipped in go1.18.

@heschi heschi added WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided. NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. labels Jul 6, 2022
@heschi heschi added this to the Backlog milestone Jul 6, 2022
@heschi
Copy link
Contributor

heschi commented Jul 6, 2022

Go 1.17 will be out of support when 1.19 ships in about a month, so it's unlikely we'd backport a fix to it now. I suggest you update to 1.18 or 1.19 when it comes out.

@gopherbot
Copy link
Contributor

Timed out in state WaitingForInfo. Closing.

(I am just a bot, though. Please speak up if this is a mistake or you have the requested information.)

@golang golang locked and limited conversation to collaborators Aug 6, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided.
Projects
None yet
Development

No branches or pull requests

4 participants