Skip to content

Commit

Permalink
fix: avoid checking for the reaper out of its mutex (testcontainers#1692
Browse files Browse the repository at this point in the history
)
  • Loading branch information
mdelapenya authored Sep 27, 2023
1 parent 51cde33 commit e7e26eb
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 32 deletions.
31 changes: 5 additions & 26 deletions docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -877,18 +877,13 @@ func (p *DockerProvider) CreateContainer(ctx context.Context, req ContainerReque
opt(&reaperOpts)
}

sessionID := testcontainerssession.SessionID()
if reaperInstance != nil {
sessionID = reaperInstance.SessionID
}

tcConfig := p.Config().Config

var termSignal chan bool
// the reaper does not need to start a reaper for itself
isReaperContainer := strings.EqualFold(req.Image, reaperImage(reaperOpts.ImageName))
if !tcConfig.RyukDisabled && !isReaperContainer {
r, err := reuseOrCreateReaper(context.WithValue(ctx, testcontainersdocker.DockerHostContextKey, p.host), sessionID, p, req.ReaperOptions...)
r, err := reuseOrCreateReaper(context.WithValue(ctx, testcontainersdocker.DockerHostContextKey, p.host), testcontainerssession.SessionID(), p, req.ReaperOptions...)
if err != nil {
return nil, fmt.Errorf("%w: creating reaper failed", err)
}
Expand Down Expand Up @@ -972,7 +967,7 @@ func (p *DockerProvider) CreateContainer(ctx context.Context, req ContainerReque

if !isReaperContainer {
// add the labels that the reaper will use to terminate the container to the request
for k, v := range testcontainersdocker.DefaultLabels(sessionID) {
for k, v := range testcontainersdocker.DefaultLabels(testcontainerssession.SessionID()) {
req.Labels[k] = v
}
}
Expand Down Expand Up @@ -1077,7 +1072,7 @@ func (p *DockerProvider) CreateContainer(ctx context.Context, req ContainerReque
WaitingFor: req.WaitingFor,
Image: tag,
imageWasBuilt: req.ShouldBuildImage(),
sessionID: sessionID,
sessionID: testcontainerssession.SessionID(),
provider: p,
terminationSignal: termSignal,
stopProducer: nil,
Expand Down Expand Up @@ -1125,9 +1120,6 @@ func (p *DockerProvider) ReuseOrCreateContainer(ctx context.Context, req Contain
}

sessionID := testcontainerssession.SessionID()
if reaperInstance != nil {
sessionID = reaperInstance.SessionID
}

tcConfig := p.Config().Config

Expand Down Expand Up @@ -1298,9 +1290,6 @@ func (p *DockerProvider) CreateNetwork(ctx context.Context, req NetworkRequest)
}

sessionID := testcontainerssession.SessionID()
if reaperInstance != nil {
sessionID = reaperInstance.SessionID
}

var termSignal chan bool
if !tcConfig.RyukDisabled {
Expand Down Expand Up @@ -1406,17 +1395,12 @@ func (p *DockerProvider) getDefaultNetwork(ctx context.Context, cli client.APICl
}
}

sessionID := testcontainerssession.SessionID()
if reaperInstance != nil {
sessionID = reaperInstance.SessionID
}

// Create a bridge network for the container communications
if !reaperNetworkExists {
_, err = cli.NetworkCreate(ctx, reaperNetwork, types.NetworkCreate{
Driver: Bridge,
Attachable: true,
Labels: testcontainersdocker.DefaultLabels(sessionID),
Labels: testcontainersdocker.DefaultLabels(testcontainerssession.SessionID()),
})

if err != nil {
Expand Down Expand Up @@ -1447,12 +1431,7 @@ func containerFromDockerResponse(ctx context.Context, response types.Container)
}
container.provider = provider

sessionID := testcontainerssession.SessionID()
if reaperInstance != nil {
sessionID = reaperInstance.SessionID
}

container.sessionID = sessionID
container.sessionID = testcontainerssession.SessionID()
container.consumers = []LogConsumer{}
container.stopProducer = nil
container.isRunning = response.State == "running"
Expand Down
6 changes: 0 additions & 6 deletions reaper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,6 @@ func TestContainerStartsWithoutTheReaper(t *testing.T) {
terminateContainerOnEnd(t, ctx, container)

sessionID := testcontainerssession.SessionID()
if reaperInstance != nil {
sessionID = reaperInstance.SessionID
}

reaperContainer, err := lookUpReaperContainer(ctx, sessionID)
if err != nil {
Expand Down Expand Up @@ -172,9 +169,6 @@ func TestContainerStartsWithTheReaper(t *testing.T) {
terminateContainerOnEnd(t, ctx, c)

sessionID := testcontainerssession.SessionID()
if reaperInstance != nil {
sessionID = reaperInstance.SessionID
}

reaperContainer, err := lookUpReaperContainer(ctx, sessionID)
if err != nil {
Expand Down

0 comments on commit e7e26eb

Please sign in to comment.