Fix data race for build container

A data race was introduced when we do read from
`watchForRunningBuildContainer` and writes in `requestBuildContainer`
concurrently. Introduce a mutex to do atomic updates and, locked reads.
parent b00023e5
......@@ -3,6 +3,7 @@ package docker
import (
"bytes"
"errors"
"sync"
"github.com/docker/docker/api/types"
"gitlab.com/gitlab-org/gitlab-runner/common"
......@@ -12,6 +13,14 @@ import (
type commandExecutor struct {
executor
buildContainer *types.ContainerJSON
sync.Mutex
}
func (s *commandExecutor) getBuildContainer() *types.ContainerJSON {
s.Lock()
defer s.Unlock()
return s.buildContainer
}
func (s *commandExecutor) Prepare(options common.ExecutorPrepareOptions) error {
......@@ -57,6 +66,8 @@ func (s *commandExecutor) requestNewPredefinedContainer() (*types.ContainerJSON,
}
func (s *commandExecutor) requestBuildContainer() (*types.ContainerJSON, error) {
s.Lock()
defer s.Unlock()
if s.buildContainer == nil {
var err error
......
......@@ -27,12 +27,13 @@ func (buildContainerTerminalTimeout) Error() string {
func (s *commandExecutor) watchForRunningBuildContainer(deadline time.Time) (string, error) {
for time.Since(deadline) < 0 {
if s.buildContainer == nil {
buildContainer := s.getBuildContainer()
if buildContainer == nil {
time.Sleep(time.Second)
continue
}
containerID := s.buildContainer.ID
containerID := buildContainer.ID
container, err := s.client.ContainerInspect(s.Context, containerID)
if err != nil {
return "", err
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment