Skip to content

Commit

Permalink
Suppress errors on EBADF when unlocking files (#35706)
Browse files Browse the repository at this point in the history
<!--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/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 <bogdandrutu@gmail.com>
  • Loading branch information
sfc-gh-kstewart and bogdandrutu authored Oct 9, 2024
1 parent e2e3b20 commit 8c7c67b
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
27 changes: 27 additions & 0 deletions .chloggen/filelog-receiver-fs-lock-handle-delete.yaml
Original file line number Diff line number Diff line change
@@ -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]
6 changes: 5 additions & 1 deletion pkg/stanza/fileconsumer/internal/reader/reader_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
}
}

0 comments on commit 8c7c67b

Please sign in to comment.