Skip to content

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

Merged
MichaelKatsoulis merged 1 commit into
8.19from
mergify/bp/8.19/pr-50498
May 11, 2026
Merged

[8.19](backport #50498) fix: Safeguard closing nil channels in azure metrics module#50587
MichaelKatsoulis merged 1 commit into
8.19from
mergify/bp/8.19/pr-50498

Conversation

@mergify
Copy link
Copy Markdown
Contributor

@mergify mergify Bot commented May 11, 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 requested a review from a team as a code owner May 11, 2026 14:31
@mergify mergify Bot added the backport label May 11, 2026
@mergify mergify Bot requested a review from a team as a code owner May 11, 2026 14:31
@mergify mergify Bot requested review from orestisfl and rdner and removed request for a team May 11, 2026 14:31
@botelastic botelastic Bot added the needs_team Indicates that the issue/PR needs a Team:* label label May 11, 2026
@github-actions
Copy link
Copy Markdown
Contributor

🤖 GitHub comments

Just comment with:

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

@github-actions github-actions Bot added the Team:obs-ds-hosted-services Label for the Observability Hosted Services team label May 11, 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 11, 2026
@MichaelKatsoulis MichaelKatsoulis enabled auto-merge (squash) May 11, 2026 14:35
@MichaelKatsoulis MichaelKatsoulis merged commit 9a18222 into 8.19 May 11, 2026
34 checks passed
@MichaelKatsoulis MichaelKatsoulis deleted the mergify/bp/8.19/pr-50498 branch May 11, 2026 15:40
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