Skip to content

exporter/file: serialize writer swap in Shutdown to stop nil-deref race#47833

Open
SAY-5 wants to merge 1 commit intoopen-telemetry:mainfrom
SAY-5:fix/fileexporter-shutdown-race-46871
Open

exporter/file: serialize writer swap in Shutdown to stop nil-deref race#47833
SAY-5 wants to merge 1 commit intoopen-telemetry:mainfrom
SAY-5:fix/fileexporter-shutdown-race-46871

Conversation

@SAY-5
Copy link
Copy Markdown

@SAY-5 SAY-5 commented Apr 21, 2026

Description:

Shutdown set e.writer = nil and then called w.shutdown() on the captured pointer, but consume* read e.writer unsynchronized. A concurrent consumeMetrics / consumeTraces / consumeLogs / consumeProfiles could observe nil after Shutdown's assignment and crash:

panic: runtime error: invalid memory address or nil pointer dereference
fileexporter.(*fileExporter).consumeMetrics ... file_exporter.go:36 +0x46

Fix: add a sync.RWMutex guarding writer. consume* take the read lock and return a descriptive "already shut down" error when the writer has been cleared; Shutdown takes the write lock for the swap before closing the previously-captured writer. Existing tests that access e.writer directly keep compiling because the field type is unchanged.

Link to tracking issue: Fixes #46871

Testing: go test ./exporter/fileexporter/... -count=1 passes.

Documentation: Added .chloggen/46871-fileexporter-shutdown-race.yaml under bug_fix.

Signed-off-by: SAY-5 SAY-5@users.noreply.github.com
Assisted-by: Claude Opus 4.7

Shutdown set e.writer = nil and then called w.shutdown() on the
captured pointer, but consume* read e.writer unsynchronized. A
concurrent consumeMetrics (or consumeTraces/consumeLogs/consumeProfiles)
could observe nil after Shutdown's assignment and crash in
file_exporter.go:36 with:

  panic: runtime error: invalid memory address or nil pointer dereference
  fileexporter.(*fileExporter).consumeMetrics ... file_exporter.go:36 +0x46

Add a sync.RWMutex guarding writer; consume* take the read lock and
return a descriptive "already shut down" error when the writer has
been cleared, and Shutdown takes the write lock for the swap before
closing the previously-captured writer. Existing tests that still
access e.writer directly keep compiling because the field type is
unchanged.

Fixes open-telemetry#46871.

Signed-off-by: SAY-5 <SAY-5@users.noreply.github.com>
Assisted-by: Claude Opus 4.7
@SAY-5 SAY-5 requested a review from a team as a code owner April 21, 2026 19:40
@SAY-5 SAY-5 requested a review from VihasMakwana April 21, 2026 19:40
@github-actions github-actions Bot requested a review from atingchen April 21, 2026 19:40
@github-actions github-actions Bot added the first-time contributor PRs made by new contributors label Apr 21, 2026
@github-actions
Copy link
Copy Markdown
Contributor

Welcome, contributor! Thank you for your contribution to opentelemetry-collector-contrib.

Important reminders:

A maintainer will review your pull request soon. Thank you for helping make OpenTelemetry better!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

exporter/file first-time contributor PRs made by new contributors

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[fileexporter] nil pointer dereference during collector shutdown

2 participants