Skip to content

[BUG] goroutine leak in init() of v2/cmd/formatter ? #10157

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

Closed
tigerinus opened this issue Jan 9, 2023 · 2 comments · Fixed by #10192 or #10202
Closed

[BUG] goroutine leak in init() of v2/cmd/formatter ? #10157

tigerinus opened this issue Jan 9, 2023 · 2 comments · Fixed by #10192 or #10202
Labels

Comments

@tigerinus
Copy link
Contributor

tigerinus commented Jan 9, 2023

Description

Trying to build something that depends on github.com/docker/compose/v2, then ran into this issue, because all of our tests checks for goroutine leaks at the end of running.

Steps To Reproduce

Run following test

package main

import (
	"testing"

	_ "github.com/docker/compose/v2/cmd/formatter"
	"go.uber.org/goleak"
)

func TestLeak(t *testing.T) {
	defer goleak.VerifyNone(t)
}

Output:

$ go test
--- FAIL: TestLeak (0.45s)
    main_test.go:12: found unexpected goroutines:
        [Goroutine 19 in state chan send, with github.com/docker/compose/v2/cmd/formatter.init.0.func1 on top of the stack:
        goroutine 19 [chan send]:
        github.com/docker/compose/v2/cmd/formatter.init.0.func1()
                /home/wxh/go/pkg/mod/github.com/docker/compose/v2@v2.14.2/cmd/formatter/colors.go:120 +0x1de
        created by github.com/docker/compose/v2/cmd/formatter.init.0
                /home/wxh/go/pkg/mod/github.com/docker/compose/v2@v2.14.2/cmd/formatter/colors.go:104 +0x8b
        ]
FAIL
exit status 1
FAIL    tt      0.451s

This is because there is a non-terminating goroutine in the init():

	go func() {
		i := 0
		rainbow := []colorFunc{
			colors["cyan"],
			colors["yellow"],
			colors["green"],
			colors["magenta"],
			colors["blue"],
			colors["intense_cyan"],
			colors["intense_yellow"],
			colors["intense_green"],
			colors["intense_magenta"],
			colors["intense_blue"],
		}

		for {
			loop <- rainbow[i]
			i = (i + 1) % len(rainbow)
		}
	}()

Workaround

Not a best practice to bypass questionable func, but...

	defer goleak.VerifyNone(t, goleak.IgnoreTopFunction("github.com/docker/compose/v2/cmd/formatter.init.0.func1"))

Compose Version

https://github.com/docker/compose/blob/v2.14.2/cmd/formatter/colors.go

Docker Environment

No response

Anything else?

No response

@tigerinus tigerinus changed the title [BUG] goroutine leak in init() of v2/cmd/formatter [BUG] goroutine leak in init() of v2/cmd/formatter ? Jan 9, 2023
@ndeloof
Copy link
Contributor

ndeloof commented Jan 10, 2023

indeed. If you have any suggestion for an alternative implementation, you're welcome to propose a pull-request :)

@AhmedGrati
Copy link
Contributor

@ndeloof Can you please re-open the issue, to open a new PR?

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