fix(metrics): fallback to mock writer on file open failure to prevent panic - #596
Conversation
✅ Deploy Preview for urunc canceled.
|
|
Hello @abhaygoudannavar , please do not overwrite the PR description. Read the contribution guide for more information. |
|
@cmainas i have updated the PR description as asked for now it can be merged. |
cmainas
left a comment
There was a problem hiding this comment.
Hello @abhaygoudannavar ,
you have linked a PR instead of the issue in the description. Also, I left a comment for the unit test.
51ba5c5 to
8a31ff9
Compare
|
@cmainas i have done the changes asked for.
|
cmainas
left a comment
There was a problem hiding this comment.
Hello @abhaygoudannavar ,
thank you for the changes. I have added a comment.
2b92199 to
05344a3
Compare
cmainas
left a comment
There was a problem hiding this comment.
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.
9a4d910 to
852d6d6
Compare
|
@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. |
852d6d6 to
f5df606
Compare
cmainas
left a comment
There was a problem hiding this comment.
Hello @abhaygoudannavar ,
why were the asserts got removed?
a636578 to
dcef7e4
Compare
|
Hey @cmainas Updated the tests to use assert.Equal and require.NotNil/require.NoError from testify instead of manual checks. The asserts were removed unintentionally in the initial commit fixed now.v |
|
Thank you @abhaygoudannavar for the changes. Could you please rebase over main so the CI can recognize you? |
dcef7e4 to
8238532
Compare
|
@cmainas i have rebased over the main. |
|
Hello @abhaygoudannavar , could you rebase over main and squash the non-unit test commits (or squash everything)? |
8238532 to
fae8707
Compare
fae8707 to
3fd847e
Compare
|
@cmainas i have rebased over the main and squashed the non unit test commits. |
cmainas
left a comment
There was a problem hiding this comment.
Hello @abhaygoudannavar ,
thank you for the rebase. Just two more small comments and we are good to go.
| file, err := os.OpenFile(target, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666) | ||
| if err != nil { | ||
| return nil | ||
| logrus.Warnf("Failed to open metrics file %s: %v. Metrics will be disabled.", target, err) |
There was a problem hiding this comment.
It would be better to return the error and log it form the caller, rather than importing logrus here. This will allow us in the future to handle this error differently.
| // Create the metrics writer with timestamps enabled but an invalid path | ||
| writer := NewZerologMetrics(true, invalidPath, "container-test") | ||
|
|
||
| require.NotNil(t, writer, "expected non-nil writer (fallback to mockWriter), got nil") |
There was a problem hiding this comment.
Another useful check here would be to ensure that writer is mockWriter.
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>
3fd847e to
49d40bd
Compare
|
@cmainas i have addressed both the comments, All unit tests pass. The commit has been squashed and rebased over main. |
cmainas
left a comment
There was a problem hiding this comment.
Thank you @abhaygoudannavar for the changes.
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. PR: #596 Fixes: #595 Signed-off-by: abhaygoudannavar <abhaysgoudnvr@gmail.com> Reviewed-by: Charalampos Mainas <cmainas@nubificus.co.uk> Approved-by: Charalampos Mainas <cmainas@nubificus.co.uk>
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. PR: #596 Fixes: #595 Signed-off-by: abhaygoudannavar <abhaysgoudnvr@gmail.com> Reviewed-by: Charalampos Mainas <cmainas@nubificus.co.uk> Approved-by: Charalampos Mainas <cmainas@nubificus.co.uk>
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()returnsnil. Because the caller assigns this directly without a nil check, any subsequentmetrics.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 returningnilon file open failure. This ensures urunc gracefully disables metrics rather than panicking.Related issues
How was this tested?
/etc/urunc/config.tomlwithenabled = trueand destination set to a non-existent directory (/var/log/urunc/timestamps.log).sudo nerdctl run --rm --runtime io.containerd.urunc.v2 harbor.nbfc.io/nubificus/urunc/nginx-qemu-unikraft-initrd:latest.uruncstarts normally, logs a warning about the file, and gracefully disables metrics without crashing.make unittestto ensure no existing tests were broken.LLM usage
No usage
Checklist
make lint).make test_ctr,make test_nerdctl,make test_docker,make test_crictl).