Skip to content

fix: remove stale instance entry on recreate and guard stop against missing containers - #468

Merged
yada merged 2 commits into
microcks:masterfrom
Syedowais312:fix/stale-instance-cleanup
Oct 28, 2026
Merged

yada merged 2 commits into
microcks:masterfrom
Syedowais312:fix/stale-instance-cleanup

Conversation

@Syedowais312

@Syedowais312 Syedowais312 commented Jun 22, 2026 •

Copy link
Copy Markdown
Contributor

Problem

When a container is removed externally (e.g. docker system prune) and
microcks start recreates it, the old instance entry was never removed from
config causing duplicate instance records to accumulate on every recreate.

Two root causes:

  1. RemoveInstance matched by Name (not unique) instead of ContainerID (unique)
  2. The autoRemove path in stop.go was passing instance.Name to RemoveInstance,
    silently failing to remove the correct entry
  3. start.go was clearing instance.ContainerID = "" instead of calling
    RemoveInstance to actually remove the stale record from config

Changes

  • cmd/start.go: call localConfig.RemoveInstance(instance.ContainerID) to
    remove the stale record before recreating, instead of just clearing the local variable
  • cmd/stop.go: pass instance.ContainerID to RemoveInstance in the autoRemove path
  • pkg/config/localconfig.go: RemoveInstance now matches by ContainerID instead of Name

Testing

Ran docker rm <container> while config shows status: Running, then
microcks start --name <instance> config shows a single clean entry with
the new container ID, no duplicates.

Realted: #456

Fix: #483

@Vaishnav88sk

Copy link
Copy Markdown
Contributor

Matching by ContainerID instead of Name is the correct approach. Could you add a test case for the duplicate-entry scenario?

@Syedowais312

Copy link
Copy Markdown
Contributor Author

Matching by ContainerID instead of Name is the correct approach. Could you add a test case for the duplicate-entry scenario?

i have added appropriate test cases in pkg/config/localconfig_test.go

@Vaishnav88sk

Copy link
Copy Markdown
Contributor

Thanks for adding tests after the earlier review feedback.

One thing I want to call out: the PR title mentions “guard stop against missing containers”, but I don’t see an actual missing-container guard added in stop.go in this diff. So:

  • add the missing guard behavior as well.

@Syedowais312 Syedowais312 changed the title fix: remove stale instance entry on recreate and guard stop against m… fix: remove stale instance entry on recreate and guard stop against missing containers Jul 16, 2026
@Syedowais312
Syedowais312 force-pushed the fix/stale-instance-cleanup branch from a859375 to 700e6db Compare July 16, 2026 17:58
@Syedowais312
Syedowais312 force-pushed the fix/stale-instance-cleanup branch from a5e72de to cd4b467 Compare July 16, 2026 18:03
@Syedowais312

Copy link
Copy Markdown
Contributor Author

Thanks for adding tests after the earlier review feedback.

One thing I want to call out: the PR title mentions “guard stop against missing containers”, but I don’t see an actual missing-container guard added in stop.go in this diff. So:

sorry i missed it earlier.
i have added a checks to verify whether the container still exists before trying to stop it.

Comment thread pkg/config/localconfig_test.go

@Vaishnav88sk Vaishnav88sk left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please resolve that. After that LGTM. 👍🏻

Comment thread cmd/stop.go Outdated
@Caesarsage

Copy link
Copy Markdown
Collaborator

Thanks @Syedowais312

The reconcile logic on master resets the stale entry in place (Status/ContainerID cleared, then reused) rather than appending a new record — see the "Reconcile before trusting it" block in cmd/start.go.

Could you re-verify duplicates still accumulate on a current build? If not, this can be closed.

@Syedowais312

Copy link
Copy Markdown
Contributor Author

Thanks @Syedowais312

The reconcile logic on master resets the stale entry in place (Status/ContainerID cleared, then reused) rather than appending a new record — see the "Reconcile before trusting it" block in cmd/start.go.

Could you re-verify duplicates still accumulate on a current build? If not, this can be closed.

Hey @Caesarsage, I pulled the latest master and verified again. The bug still seems to exist.

The reason is around this line:

instance.Status = ""

Here we clear instance.Status and instance.ContainerID when the old container no longer exists. Because the status becomes empty, the switch falls through to the default case and creates a new container.

But the old instance entry is still present in the config with the old container ID. Later, UpsertInstance compares using ContainerID, so the recreated instance has a new ID and gets appended instead of replacing the old entry.

In this PR, I removed the stale instance before recreating it. I also changed RemoveInstance to remove by ContainerID, because that is the exact stale entry we are trying to clean up.

@Syedowais312

Copy link
Copy Markdown
Contributor Author

Hey, I've rebased the branch onto the latest master. Could you take a look when you get a chance?

Comment thread cmd/stop.go
Comment thread cmd/stop.go
Comment on lines +69 to +78
if !exists {
fmt.Printf("Container for instance %s no longer exists\n", instance.Name)
fmt.Printf("Run 'microcks start --name %s' to bring the container back\n", instance.Name)
return nil
} else {
if err := containerClient.StopContainer(instance.ContainerID); err != nil {
return errors.Wrap(errors.KindEnvironment, fmt.Errorf("failed to stop container: %w", err))
}
fmt.Println("")
log.Printf("Instance %s stopped successfully", instance.Name)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Almost there! Thanks for moving StopContainer to the else block.

However, the return nil and the 'Run microcks start to bring it back' print statement are still inside the if !exists block. Because of that return nil, the code still exits early and skips the // update configs section at the bottom.

Could you please remove the return nil (and optionally the second print statement) so that execution falls through to the config cleanup?

It should look exactly like this:

  		if !exists {
  			fmt.Printf("Container for instance %s no longer exists\n", instance.Name)
  		} else {
  			if err := containerClient.StopContainer(instance.ContainerID); err != nil {
  				return errors.Wrap(errors.KindEnvironment, fmt.Errorf("failed to stop container: %w", err))
  			}
  			fmt.Println("")
  			log.Printf("Instance %s stopped successfully", instance.Name)
  		}

  		// update configs

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the review! Removed the return nil ,but I kept the second print statement since it tells the user how to bring the container back, which seemed useful for UX? or would you prefer I remove it?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's keep it.

@Vaishnav88sk Vaishnav88sk left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@yada yada added kind/bug Something isn't working component/cli labels Oct 15, 2026
@yada

yada commented Oct 28, 2026

Copy link
Copy Markdown
Member

Verified the merge result (build, go vet, go test ./... pass) and ran it live with Docker (alpine image, --no-wait). Start, docker rm -f, then start again: master leaves 2 instance entries with the same name; this PR leaves 1. stop after an external docker rm fails on master with No such container (exit 14); with this PR it prints that the container no longer exists, suggests microcks start, and exits 0. RemoveInstance is only called from start and stop, so the switch to container ID is safe.

Disclosure: this comment was drafted with AI assistance, and the results were run locally.

@yada yada left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verified locally. Disclosure: AI-assisted verification.

@yada
yada merged commit 6ad14d9 into microcks:master Oct 28, 2026
4 checks passed

This branch was successfully deployed

1 active deployment
Build — 38819dbc Deployed Sep 30, 2026 by Syedowais312 via build-verify-package #758
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

component/cli kind/bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bug: stale instance entries accumulate in config when container is recreated

4 participants