Skip to content

fix(metrics): fallback to mock writer on file open failure to prevent panic#596

Open
abhaygoudannavar wants to merge 2 commits into
urunc-dev:mainfrom
abhaygoudannavar:one/metrics-file
Open

fix(metrics): fallback to mock writer on file open failure to prevent panic#596
abhaygoudannavar wants to merge 2 commits into
urunc-dev:mainfrom
abhaygoudannavar:one/metrics-file

Conversation

@abhaygoudannavar
Copy link
Copy Markdown

@abhaygoudannavar abhaygoudannavar commented May 1, 2026

Description

When timestamps are enabled in the urunc configuration (/etc/urunc/config.toml) but the target file path is invalid or the parent directory does not exist, NewZerologMetrics() returns nil. Because the caller assigns this directly without a nil check, any subsequent metrics.Capture() calls result in a nil pointer dereference, crashing urunc silently.

This PR fixes the issue by returning a &mockWriter{} (safe no-op fallback) and logging a warning instead of returning nil on file open failure. This ensures urunc gracefully disables metrics rather than panicking.

Related issues

How was this tested?

  1. Enabled timestamps in /etc/urunc/config.toml with enabled = true and destination set to a non-existent directory (/var/log/urunc/timestamps.log).
  2. Ran sudo nerdctl run --rm --runtime io.containerd.urunc.v2 harbor.nbfc.io/nubificus/urunc/nginx-qemu-unikraft-initrd:latest.
  3. Verified that urunc starts normally, logs a warning about the file, and gracefully disables metrics without crashing.
  4. Ran make unittest to ensure no existing tests were broken.

LLM usage

No usage

Checklist

  • I have read the contribution guide.
  • The linter passes locally (make lint).
  • The e2e tests of at least one tool pass locally (make test_ctr, make test_nerdctl, make test_docker, make test_crictl).
  • If LLMs were used: I have read the llm policy.

@netlify
Copy link
Copy Markdown

netlify Bot commented May 1, 2026

Deploy Preview for urunc ready!

Name Link
🔨 Latest commit f5df606
🔍 Latest deploy log https://app.netlify.com/projects/urunc/deploys/6a072fb7fd6e76000845c043
😎 Deploy Preview https://deploy-preview-596--urunc.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

@cmainas
Copy link
Copy Markdown
Contributor

cmainas commented May 5, 2026

Hello @abhaygoudannavar ,

please do not overwrite the PR description. Read the contribution guide for more information.

@abhaygoudannavar
Copy link
Copy Markdown
Author

@cmainas i have updated the PR description as asked for now it can be merged.

Copy link
Copy Markdown
Contributor

@cmainas cmainas left a comment

Choose a reason for hiding this comment

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

Hello @abhaygoudannavar ,

you have linked a PR instead of the issue in the description. Also, I left a comment for the unit test.

Comment thread internal/metrics/metrics_test.go
@abhaygoudannavar
Copy link
Copy Markdown
Author

abhaygoudannavar commented May 10, 2026

@cmainas i have done the changes asked for.

Copy link
Copy Markdown
Contributor

@cmainas cmainas left a comment

Choose a reason for hiding this comment

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

Hello @abhaygoudannavar ,

thank you for the changes. I have added a comment.

Comment thread internal/metrics/metrics_test.go Outdated
When timestamps are enabled but the target file cannot be opened,
NewZerologMetrics returned nil. This caused a nil pointer dereference
on subsequent metrics.Capture() calls.

This fix gracefully degrades to a no-op mockWriter and logs a warning.
Adds a regression test to verify the fallback behavior.

Fixes urunc-dev#595

Signed-off-by: abhaygoudannavar <abhaysgoudnvr@gmail.com>
Copy link
Copy Markdown
Contributor

@cmainas cmainas left a comment

Choose a reason for hiding this comment

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

Hello @abhaygoudannavar ,

it seems there was a misunderstanding. Maybe I di not place the comment in the correct line. The check for the nil return should remain, the call later to Capture should get removed. We should not test by checking if the test crashes.

Comment thread internal/metrics/metrics_test.go Outdated
@abhaygoudannavar
Copy link
Copy Markdown
Author

@cmainas Thanks for the clarification, I've updated the test to explicitly check for a non-nil writer instead of relying on the panic recovery. The PR has been updated.

@abhaygoudannavar abhaygoudannavar requested a review from cmainas May 15, 2026 14:18
…esting

Signed-off-by: abhaygoudannavar <abhaysgoudnvr@gmail.com>
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.

fix(metrics): NewZerologMetrics returns nil on file open failure causing panic

2 participants