testcontainers-go icon indicating copy to clipboard operation
testcontainers-go copied to clipboard

Docker connection not closed

Open Eun opened this issue 4 years ago • 6 comments

Describe the bug The ProviderType.GetProvider() function creates a docker client, but never closes it, which leads to a leaking connection.

Similar to https://github.com/testcontainers/testcontainers-go/issues/319

To Reproduce Use following test:

package main

import (
	"context"
	"testing"
	"time"

	"github.com/testcontainers/testcontainers-go"
	"github.com/testcontainers/testcontainers-go/wait"
	"go.uber.org/goleak"
)

func TestMain(m *testing.M) {
	goleak.VerifyTestMain(m)
}

func TestFoo(t *testing.T) {
	ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
	defer cancel()
	req := testcontainers.ContainerRequest{
		Image:        "postgres:12",
		ExposedPorts: []string{"5432/tcp"},
		AutoRemove:   true,
		Env: map[string]string{
			"POSTGRES_USER":     "postgres",
			"POSTGRES_PASSWORD": "postgres",
			"POSTGRES_DB":       "postgres",
		},
		WaitingFor: wait.ForListeningPort("5432/tcp"),
	}
	container, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{
		ContainerRequest: req,
		Started:          true,
	})
	if err != nil {
		panic(err)
	}
	if err := container.Terminate(ctx); err != nil {
		panic(err)
	}
}

Expected behavior A successful test

docker info output of the command:

$ docker info
Client:
 Context:    default
 Debug Mode: false

Server:
 Containers: 18
  Running: 1
  Paused: 0
  Stopped: 17
 Images: 72
 Server Version: 20.10.2
 Storage Driver: zfs
  Zpool: rpool
  Zpool Health: ONLINE
  Parent Dataset: rpool/var/lib/docker
  Space Used By Parent: 10480205824
  Space Available: 145392234496
  Parent Quota: no
  Compression: lz4
 Logging Driver: json-file
 Cgroup Driver: cgroupfs
 Cgroup Version: 1
 Plugins:
  Volume: local
  Network: bridge host ipvlan macvlan null overlay
  Log: awslogs fluentd gcplogs gelf journald json-file local logentries splunk syslog
 Swarm: inactive
 Runtimes: io.containerd.runc.v2 io.containerd.runtime.v1.linux runc
 Default Runtime: runc
 Init Binary: docker-init
 containerd version: 
 runc version: 
 init version: 
 Security Options:
  apparmor
  seccomp
   Profile: default
 Kernel Version: 5.8.0-50-generic
 Operating System: Ubuntu 20.04.2 LTS
 OSType: linux
 Architecture: x86_64
 CPUs: 8
 Total Memory: 15.53GiB
 Name: tobias-talon-T480s
 ID: UMBP:HB2G:5S3P:JMX3:VZ46:ATMB:64RC:4WXO:YJ6L:M7UK:DWGB:JJ25
 Docker Root Dir: /var/lib/docker
 Debug Mode: false
 Registry: https://index.docker.io/v1/
 Labels:
 Experimental: false
 Insecure Registries:
  127.0.0.0/8
 Live Restore Enabled: false

Additional context It might be necessary to adjust GenericProvider to include a Close() function that closes the used provider.

Eun avatar May 11 '21 08:05 Eun

Hi @Eun, thanks for reporting this issue. Would you like to send a PR with this implementation? We'll be fast to review it.

If not, I'll find time to implement it and ask for your review when I have bandwidth to do it.

Thanks in advance!!

mdelapenya avatar May 11 '21 09:05 mdelapenya

Hi @Eun I'm not able to reproduce this bug yet, using the same repro code shared here.

Could you please double check if it's still happening on your side? 🙏

mdelapenya avatar Sep 15 '22 18:09 mdelapenya

OTOH let me rephrase it myself: do you think all the usages of the provider.client should automatically Close the connection? 🤔

mdelapenya avatar Sep 15 '22 18:09 mdelapenya

I cannot reproduce either! Could be related to new versions of goleak or go or whatever.

However I can remember the case:

In docker.go a new docker.Client instance gets created in func NewDockerProvider() (*DockerProvider, error).

However, this instance will never be closed.
It should be closed calling client.Close I think the only solution is to add a Close function to the GenericProvider interface and call it after being done.

Eun avatar Sep 16 '22 07:09 Eun

Would you mind elaborating a PR? Otherwise, we can prioritize it for our upcoming iterations

Cheers!

mdelapenya avatar Sep 16 '22 08:09 mdelapenya

I am kinda busy at this moment, so wont make it happen.

Eun avatar Sep 18 '22 11:09 Eun

See also related https://github.com/testcontainers/testcontainers-go/issues/1357

AlexanderYastrebov avatar Jul 10 '23 17:07 AlexanderYastrebov