Close directory watcher descriptors after failed startup - #2036
Open
stephenlclarke wants to merge 7 commits into
Open
Close directory watcher descriptors after failed startup#2036stephenlclarke wants to merge 7 commits into
stephenlclarke wants to merge 7 commits into
Conversation
Close the directory descriptor when initial enumeration or the watch handler fails before the dispatch source takes ownership. Use a direct repeated-failure regression test with an open-descriptor baseline. The negative control leaked exactly 32 descriptors and the fixed implementation leaked none. Upstream-PR: apple#1773 Upstream-Head: 77196fc
Count only descriptors that resolve to the watched directory so the repeated-failure regression remains exact under concurrent test execution.
Treat file descriptor zero as a successful open result so a watcher created after standard input is closed does not reject and leak the valid descriptor.
7 tasks
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.
Important
All commits are signed and verified.
Type of Change
Motivation and Context
During testing of my Container Compose plugin, I found and fixed a path-specific file-descriptor leak in
DirectoryWatcher._startWatchingopens anO_EVTONLYdescriptor before it lists the directory and calls the handler. If either operation throws, the dispatch source that normally owns and closes the descriptor is never created. The retry loop can therefore leak one descriptor on every failed startup.This submission retains the correct production fix from #1773 and credits its author, @muk2. It supersedes that proposal with deterministic regression coverage: the test identifies only descriptors whose
F_GETPATHresolves to the watched directory, verifies the failing handler ran exactly 32 times, and requires the path-specific count to return to its exact baseline. It also accepts valid descriptor zero, makes the internal escaping handler contract@Sendable, and replaces fixed-sleep test races with explicit watcher readiness and restart synchronisation, bounded condition polling, and locked snapshots.This PR deliberately does not close #1097. That issue reproduces descriptor growth from guest filesystem traversal through virtiofs, while
DirectoryWatcheris used by the host DNS resolver configuration watcher. The available evidence does not establish that this code path causes #1097.Fixes #2034
Reproduction on stock Apple
mainBase tested:
48145ac7fb177d9fb14e015a0bdea4c642b36729. A test-only visibility and@Sendableannotation were applied to the stock method so the regression test could invoke it; the stock descriptor guard and missing error-pathclosewere left unchanged.Actual stock result: the single test fails in 0.010 seconds with
baseline=0andafterFailures=32.Expected result: after 32 intentional handler failures, the number of descriptors resolving to the watched path equals the baseline.
Changes
openresult.@Sendable, matching the public API and dispatch-source use.Testing
Tested locally
Added/updated tests
Added/updated docs
make check: passed in 4.84 seconds wall time.swift test --filter DirectoryWatcherTest: 5 tests in 1 suite passed in 1.081 seconds on the final head; 5.84 seconds wall time including the incremental build.Stability run: 10 consecutive
swift test --filter DirectoryWatcherTestattempts passed, 50 tests total; suite times remained between 1.062 and 1.092 seconds, with cached wall times between 1.89 and 1.97 seconds after the first build refresh.make test: 590 tests in 71 suites passed in 1.266 seconds on the final head; 114.91 seconds wall time including the 105.82-second full rebuild.Stock negative control: the targeted test failed as expected in 0.010 seconds, reporting exactly 32 leaked descriptors for the watched path; 0.63 seconds wall time.
git diff --check: passed.All seven commits verified with the configured signing key.