Skip to content

fix: resolve variable shadowing causing allServices count to always report 0 in swarm mode#736

Merged
m90 merged 1 commit intooffen:mainfrom
SmallFriendlyKiwi:fix/allservices-variable-shadowing
Feb 26, 2026
Merged

fix: resolve variable shadowing causing allServices count to always report 0 in swarm mode#736
m90 merged 1 commit intooffen:mainfrom
SmallFriendlyKiwi:fix/allservices-variable-shadowing

Conversation

@SmallFriendlyKiwi
Copy link
Contributor

@SmallFriendlyKiwi SmallFriendlyKiwi commented Feb 26, 2026


Fix variable shadowing bug causing incorrect service count in log message

This is my first ever pull request - this is all new to me so I hope everything is in order!

I am no Go expert and used an LLM to help find the bug so I am not sure this is the preffered way to resolve.

I rebuilt the Docker image locally, pushed it to my local container registry, and tested it against my Swarm stack. Complete logs are included below to show that logging now correctly shows the number of active services.

Problem

In stopContainersAndServices() within the swarm handling block, the allServices variable declared in the outer scope is shadowed by a new inner-scope variable due to the use of := instead of =:

var allServices []swarm.Service
...
if isDockerSwarm {
    result, err := s.cli.ServiceList(...)
    allServices := result.Items  // shadows outer variable, outer remains nil

This means the outer allServices is never populated, so when it is referenced in the log message:

fmt.Sprintf(
    "Scaling down %d out of %d active service(s) as they were labeled %s or %s.",
    len(servicesToScaleDown),
    len(allServices),  // always 0
    ...
)

The count always reports 0, producing misleading log output such as:

Scaling down 1 out of 0 active service(s) as they were labeled ...

Note: s.stats.Services.All is also affected and will always be 0.

Fix

Change := to = when assigning result.Items to allServices:

result, err := s.cli.ServiceList(context.Background(), client.ServiceListOptions{Status: true})
allServices = result.Items  // assign to outer variable

Real-world reproduction

Before fix — service count incorrectly reports 0:

msg="Scaling down 1 out of 0 active service(s) as they were labeled ..."

After fix — service count correctly reports all 21 services in the swarm:

msg="Scaling down 1 out of 21 active service(s) as they were labeled ..."
Full log output

Before:

time=2026-02-26T14:10:59.231+13:00 level=INFO msg="Stopping 0 out of 2 running container(s) as they were labeled docker-volume-backup.stop-during-backup=swarm.minimaker.space_gitea or docker-volume-backup.stop-during-backup-no-restart=true."
time=2026-02-26T14:10:59.232+13:00 level=INFO msg="Scaling down 1 out of 0 active service(s) as they were labeled docker-volume-backup.stop-during-backup=swarm.minimaker.space_gitea or docker-volume-backup.stop-during-backup-no-restart=true."
time=2026-02-26T14:11:27.120+13:00 level=INFO msg="Created backup of `/backup` at `/tmp/backup-2026-02-26T14-10-59.tar.gz`."
time=2026-02-26T14:11:34.846+13:00 level=INFO msg="Restarted 0 out of 0 stopped container(s)."
time=2026-02-26T14:11:34.846+13:00 level=INFO msg="Scaled 1 out of 1 scaled down service(s) back up."

After:

time=2026-02-26T14:04:16.403+13:00 level=INFO msg="Stopping 0 out of 2 running container(s) as they were labeled docker-volume-backup.stop-during-backup=swarm.minimaker.space_gitea or docker-volume-backup.stop-during-backup-no-restart=true."
time=2026-02-26T14:04:16.404+13:00 level=INFO msg="Scaling down 1 out of 21 active service(s) as they were labeled docker-volume-backup.stop-during-backup=swarm.minimaker.space_gitea or docker-volume-backup.stop-during-backup-no-restart=true."
time=2026-02-26T14:04:50.515+13:00 level=INFO msg="Created backup of `/backup` at `/tmp/backup-2026-02-26T14-04-16.tar.gz`."
time=2026-02-26T14:04:58.014+13:00 level=INFO msg="Restarted 0 out of 0 stopped container(s)."
time=2026-02-26T14:04:58.015+13:00 level=INFO msg="Scaled 1 out of 1 scaled down service(s) back up."

@m90 m90 merged commit 7efcae4 into offen:main Feb 26, 2026
3 checks passed
@m90
Copy link
Member

m90 commented Feb 26, 2026

🎣 good catch, not sure how that regression slipped through. Thanks for fixing.

Also +1 on being transparent on how this was created, I don't mind at all as long as it's known to all parties 👍

@m90
Copy link
Member

m90 commented Feb 26, 2026

This is now released in v2.47.2

@SmallFriendlyKiwi SmallFriendlyKiwi deleted the fix/allservices-variable-shadowing branch February 27, 2026 00:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants