Skip to content

Commit cfbe931

Browse files
vcalasanshHarness
authored andcommitted
fix: [PIPE-29145]: Stop leaking file descriptors pointing to docker socket (#396)
* 67f5a3 Remove unused variable * 7521f2 Stop leaking file descriptors pointing to docker socket
1 parent b6591d8 commit cfbe931

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed

engine/docker/docker.go

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,15 @@ const (
5555
running = "running"
5656
)
5757

58+
var (
59+
globalDockerClient *client.Client
60+
globalDockerClientLock sync.Mutex
61+
)
62+
5863
// Opts configures the Docker engine.
5964
type Opts struct {
60-
HidePull bool
65+
HidePull bool
66+
ReuseDockerClient bool
6167
}
6268

6369
// Docker implements a Docker pipeline engine.
@@ -88,7 +94,19 @@ func New(client client.APIClient, opts Opts) *Docker {
8894

8995
// NewEnv returns a new Engine from the environment.
9096
func NewEnv(opts Opts) (*Docker, error) {
91-
cli, err := client.NewClientWithOpts(client.FromEnv)
97+
var cli client.APIClient
98+
var err error
99+
if opts.ReuseDockerClient {
100+
globalDockerClientLock.Lock()
101+
defer globalDockerClientLock.Unlock()
102+
// Try to (re)initialize if not set
103+
if globalDockerClient == nil {
104+
globalDockerClient, err = client.NewClientWithOpts(client.FromEnv)
105+
}
106+
cli = globalDockerClient
107+
} else {
108+
cli, err = client.NewClientWithOpts(client.FromEnv)
109+
}
92110
if err != nil {
93111
return nil, err
94112
}

pipeline/runtime/step_executor_stateless.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010

1111
"github.com/harness/lite-engine/api"
1212
"github.com/harness/lite-engine/engine"
13+
"github.com/harness/lite-engine/engine/docker"
1314
"github.com/harness/lite-engine/engine/spec"
1415
"github.com/harness/lite-engine/errors"
1516
"github.com/harness/lite-engine/logstream"
@@ -61,7 +62,7 @@ func (e *StepExecutorStateless) executeStep( //nolint:gocritic
6162
) (*runtime.State, map[string]string,
6263
map[string]string, []byte, []*api.OutputV2, *types.TelemetryData, string, error) {
6364
runFunc := func(ctx context.Context, step *spec.Step, output io.Writer, isDrone bool, isHosted bool) (*runtime.State, error) {
64-
return engine.RunStep(ctx, engine.Opts{}, step, output, cfg, isDrone, isHosted)
65+
return engine.RunStep(ctx, engine.Opts{Opts: docker.Opts{ReuseDockerClient: true}}, step, output, cfg, isDrone, isHosted)
6566
}
6667
// Temporary: this should be removed once we have a better way of handling test intelligence.
6768
tiConfig := getTiCfg(&r.TIConfig, &r.MtlsConfig)

0 commit comments

Comments
 (0)