Skip to content

[9.4](backport #50498) fix: Safeguard closing nil channels in azure metrics module#50512

Open
mergify[bot] wants to merge 2 commits into9.4from
mergify/bp/9.4/pr-50498
Open

[9.4](backport #50498) fix: Safeguard closing nil channels in azure metrics module#50512
mergify[bot] wants to merge 2 commits into9.4from
mergify/bp/9.4/pr-50498

Conversation

@mergify
Copy link
Copy Markdown
Contributor

@mergify mergify Bot commented May 6, 2026

In x-pack/metricbeat/module/azure, BatchClient.InitResources could panic
with panic: close of nil channel whenever every configured resource
entry returned zero resources from the Azure API (e.g. a
resource_query: "resourceType eq 'Microsoft.ContainerInstance/containerGroups'"
on a subscription that has no Container Instances).

WHAT:

  • Guard the goroutine that closes MetricDefinitionsChan and ErrorChan
    in InitResources so it only runs when those channels were actually
    created during the resource-config loop.
  • Add a unit test (does_not_panic_when_all_resource_configs_return_an_empty_list)
    covering the empty-list + nil-error path that reproduces the panic on
    the unfixed code.

WHY:

  • MetricDefinitionsChan / ErrorChan are created lazily inside the
    resource-config loop, only after the empty-list continue check. When
    every resourceConfig returned an empty list, both channels stayed
    nil (their initial value from NewBatchClient) but the closer
    goroutine launched unconditionally at the end of the function still
    ran, calling close(nil) and crashing the Metricbeat process.
  • Skipping the closer goroutine when the channels were never created.

Checklist

  • My code follows the style guidelines of this project
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • I have made corresponding change to the default configuration files
  • I have added tests that prove my fix is effective or that my feature works.
  • I have added an entry in ./changelog/fragments using the changelog tool.

Disruptive User Impact

None. It prevents a potential panic

How to test this PR locally

Unit test :

cd x-pack/metricbeat/module/azure
go test -run TestInitResourcesForBatch -v

Without the fix the new subtest crashes the test binary with
panic: close of nil channel at client_batch.go:144. With the fix
the full subtest passes.

End-to-end repro:

- module: azure
  metricsets: ["container_service"]
  period: 60s
  client_id: ...
  client_secret: ...
  tenant_id: ...
  subscription_id: <a subscription with no Container Instances>
  resources:
    - resource_query: "resourceType eq 'Microsoft.ContainerInstance/containerGroups'"
      metrics:
        - name: ["CpuUsage"]

Before this PR Metricbeat panics on the first refresh; after this PR
it logs the per-config "No resources returned" error and proceeds.

Related issues

Logs

Production stack trace (pre-fix) reproduced by the new unit test:

panic: close of nil channel
goroutine 20 [running]:
github.com/elastic/beats/v7/x-pack/metricbeat/module/azure.(*BatchClient).InitResources.func2()
        .../x-pack/metricbeat/module/azure/client_batch.go:144 +0x88
created by github.com/elastic/beats/v7/x-pack/metricbeat/module/azure.(*BatchClient).InitResources in goroutine 19
        .../x-pack/metricbeat/module/azure/client_batch.go:140 +0x5dd
```<hr>This is an automatic backport of pull request #50498 done by [Mergify](https://mergify.com).

* fix: Safeguard closing nil channels in azure metrics module

* add changelog

(cherry picked from commit caea6ba)
@mergify mergify Bot added the backport label May 6, 2026
@mergify mergify Bot requested a review from a team as a code owner May 6, 2026 11:52
@mergify mergify Bot added the backport label May 6, 2026
@botelastic botelastic Bot added the needs_team Indicates that the issue/PR needs a Team:* label label May 6, 2026
@github-actions github-actions Bot added the Team:obs-ds-hosted-services Label for the Observability Hosted Services team label May 6, 2026
@infra-vault-gh-plugin-prod
Copy link
Copy Markdown

Pinging @elastic/obs-ds-hosted-services (Team:obs-ds-hosted-services)

@botelastic botelastic Bot removed the needs_team Indicates that the issue/PR needs a Team:* label label May 6, 2026
@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented May 6, 2026

🤖 GitHub comments

Just comment with:

  • run docs-build : Re-trigger the docs validation. (use unformatted text in the comment!)
  • /test : Run the Buildkite pipeline.

@MichaelKatsoulis MichaelKatsoulis enabled auto-merge (squash) May 7, 2026 07:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backport Team:obs-ds-hosted-services Label for the Observability Hosted Services team

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant