feat(allowedpaths): guarantee Close on context expiry to prevent fd leaks#157
Merged
feat(allowedpaths): guarantee Close on context expiry to prevent fd leaks#157
Conversation
…eaks Add WithContextClose wrapper that ensures any file opened via the sandbox's OpenFile hook has Close() called when the call's context expires. A background goroutine waits for ctx.Done() or an explicit Close, whichever comes first. The underlying file is closed at most once (sync.Once guards). Existing defer f.Close() calls in builtin implementations are untouched — this is a safety backstop, not a replacement for explicit cleanup. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
…s allowlist context_file.go in allowedpaths/ uses these two symbols for the WithContextClose wrapper. They were missing from the allowlist, causing TestAllowedPathsAllowedSymbols to fail on CI. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
…file types Adds context_file_linux_test.go covering Read-unblock behaviour for: Non-blocking (Read returns immediately — verify no hang): - Regular file - /dev/null (returns EOF) - /dev/zero (returns zeros immediately) - /dev/urandom (returns random bytes immediately) - Symbolic link → regular file - Directory (returns EISDIR immediately) - Block device (skipped if none accessible) Blocking (Read blocks until Close — verify context cancel unblocks): - Named pipe / FIFO (O_RDWR, empty buffer) - net.Pipe() socketpair (Unix socket, no sender) - /dev/random (may block on low-entropy kernels) - /dev/tty (skipped if unavailable in CI) - Filesystem Unix domain socket (/tmp sock, server conn with no data) Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
val06
approved these changes
Mar 26, 2026
astuyve
approved these changes
Mar 26, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Read-unblock behaviour by file type
Linux unit tests (`context_file_linux_test.go`) verify two things per file type: (1) that `Close()` is guaranteed to be called on context cancellation, and (2) whether a goroutine blocked in `Read()` is unblocked when the context is cancelled.
Go registers FIFOs, sockets, and character devices with its epoll poller. When `Close()` is called, the runtime calls `evict()` which wakes any goroutine blocked on that fd. Regular files are not registered with epoll but never block, so they are safe regardless.
Non-blocking file types — Read returns immediately
Blocking file types — Read blocks; context cancel must unblock it
Test plan
🤖 Generated with Claude Code