Skip to content

Commit 57a671a

Browse files
sfc-gh-kstewartbogdandrutu
authored andcommitted
Suppress errors on EBADF when unlocking files (open-telemetry#35706)
<!--Ex. Fixing a bug - Describe the bug and how this fixes the issue. Ex. Adding a feature - Explain what this achieves.--> #### 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. <!--Describe what testing was performed and which tests were added.--> #### 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": "<redacted>", "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/[email protected]/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/[email protected]/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/[email protected]/fileconsumer/file.go:160 ``` Performing the same test with this changed applied eliminates the error. --------- Co-authored-by: Bogdan Drutu <[email protected]>
1 parent 547d470 commit 57a671a

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Use this changelog template to create an entry for release notes.
2+
3+
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
4+
change_type: bug_fix
5+
6+
# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver)
7+
component: filelogreceiver
8+
9+
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
10+
note: "Suppress errors on EBADF when unlocking files."
11+
12+
# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
13+
issues: [35706]
14+
15+
# (Optional) One or more lines of additional information to render under the primary note.
16+
# These lines will be padded with 2 spaces and then inserted directly into the document.
17+
# Use pipe (|) for multiline entries.
18+
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."
19+
20+
# If your change doesn't affect end users or the exported elements of any package,
21+
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label.
22+
# Optional: The change log or logs in which this entry should be included.
23+
# e.g. '[user]' or '[user, api]'
24+
# Include 'user' if the change is relevant to end users.
25+
# Include 'api' if there is a change to a library API.
26+
# Default: '[user]'
27+
change_logs: [user]

pkg/stanza/fileconsumer/internal/reader/reader_unix.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ func (r *Reader) tryLockFile() bool {
2525

2626
func (r *Reader) unlockFile() {
2727
if err := unix.Flock(int(r.file.Fd()), unix.LOCK_UN); err != nil {
28-
r.set.Logger.Error("Failed to unlock", zap.Error(err))
28+
// If delete_after_read is set then the file may already have been deleted by this point,
29+
// in which case we'll get EBADF. This is harmless and not worth logging.
30+
if !errors.Is(err, unix.EBADF) {
31+
r.set.Logger.Error("Failed to unlock", zap.Error(err))
32+
}
2933
}
3034
}

0 commit comments

Comments
 (0)