Skip to content
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

fix: avoid checking for the reaper out of its mutex #1692

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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