From 8c7c67bbe5af118112ed6019b32cb766b4e5617d Mon Sep 17 00:00:00 2001 From: Kirk Stewart Date: Tue, 8 Oct 2024 17:40:52 -0700 Subject: [PATCH] Suppress errors on EBADF when unlocking files (#35706) #### Description This error is harmless and happens regularly when `delete_after_read` is set. This is because we acquire the lock right at the start of the `ReadToEnd` function and then defer the unlock, but that function also performs the delete. So, by the time it returns and the defer runs the file descriptor is no longer valid. #### Testing Tested manually on a laptop running MacOS. When using `acquire_fs_locks` in conjunction with `delete_after_read` I observe this error in the logs *before applying this change*: ``` 2024-10-08T14:30:23.626-0700 error reader/reader_unix.go:28 Failed to unlock {"kind": "receiver", "name": "filelog", "data_type": "logs", "component": "fileconsumer", "path": "", "error": "bad file descriptor"} github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/fileconsumer/internal/reader.(*Reader).unlockFile github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza@v0.111.0/fileconsumer/internal/reader/reader_unix.go:28 github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/fileconsumer/internal/reader.(*Reader).ReadToEnd github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza@v0.111.0/fileconsumer/internal/reader/reader.go:126 github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/fileconsumer.(*Manager).consume.func1 github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza@v0.111.0/fileconsumer/file.go:160 ``` Performing the same test with this changed applied eliminates the error. --------- Co-authored-by: Bogdan Drutu --- ...ilelog-receiver-fs-lock-handle-delete.yaml | 27 +++++++++++++++++++ .../internal/reader/reader_unix.go | 6 ++++- 2 files changed, 32 insertions(+), 1 deletion(-) create mode 100644 .chloggen/filelog-receiver-fs-lock-handle-delete.yaml diff --git a/.chloggen/filelog-receiver-fs-lock-handle-delete.yaml b/.chloggen/filelog-receiver-fs-lock-handle-delete.yaml new file mode 100644 index 000000000000..5035d3aa4662 --- /dev/null +++ b/.chloggen/filelog-receiver-fs-lock-handle-delete.yaml @@ -0,0 +1,27 @@ +# Use this changelog template to create an entry for release notes. + +# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix' +change_type: bug_fix + +# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver) +component: filelogreceiver + +# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). +note: "Suppress errors on EBADF when unlocking files." + +# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists. +issues: [35706] + +# (Optional) One or more lines of additional information to render under the primary note. +# These lines will be padded with 2 spaces and then inserted directly into the document. +# Use pipe (|) for multiline entries. +subtext: "This error is harmless and happens regularly when delete_after_read is set. This is because we acquire the lock right at the start of the ReadToEnd function and then defer the unlock, but that function also performs the delete. So, by the time it returns and the defer runs the file descriptor is no longer valid." + +# If your change doesn't affect end users or the exported elements of any package, +# you should instead start your pull request title with [chore] or use the "Skip Changelog" label. +# Optional: The change log or logs in which this entry should be included. +# e.g. '[user]' or '[user, api]' +# Include 'user' if the change is relevant to end users. +# Include 'api' if there is a change to a library API. +# Default: '[user]' +change_logs: [user] diff --git a/pkg/stanza/fileconsumer/internal/reader/reader_unix.go b/pkg/stanza/fileconsumer/internal/reader/reader_unix.go index 1fb7d6d98201..a786cacef9e0 100644 --- a/pkg/stanza/fileconsumer/internal/reader/reader_unix.go +++ b/pkg/stanza/fileconsumer/internal/reader/reader_unix.go @@ -25,6 +25,10 @@ func (r *Reader) tryLockFile() bool { func (r *Reader) unlockFile() { if err := unix.Flock(int(r.file.Fd()), unix.LOCK_UN); err != nil { - r.set.Logger.Error("Failed to unlock", zap.Error(err)) + // If delete_after_read is set then the file may already have been deleted by this point, + // in which case we'll get EBADF. This is harmless and not worth logging. + if !errors.Is(err, unix.EBADF) { + r.set.Logger.Error("Failed to unlock", zap.Error(err)) + } } }