Close directory watcher descriptor when the handler fails#1773
Open
muk2 wants to merge 1 commit into
Open
Conversation
DirectoryWatcher._startWatching opens an O_EVTONLY descriptor and then runs the change handler before creating the DispatchSource that takes ownership of the descriptor (and closes it on cancellation). If the handler or contentsOfDirectory call throws, the function returns without ever creating the dispatch source, leaking the descriptor. Because startWatching retries roughly once per second whenever the source is still nil, this leaks one descriptor per second on the long-lived host daemon, eventually exhausting file descriptors for the whole system and crashing unrelated host apps with "too many open files". Close the descriptor on the error path and add a regression test that verifies a continuously failing handler does not accumulate open fds. Fixes apple#1097
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
Closes a file-descriptor leak in
DirectoryWatcher. The descriptor is openedbefore the dispatch source that would normally own and close it is created. If
listing the directory or invoking the handler throws, the function returned
without closing that descriptor, and because the watcher retries roughly once
per second, the leak accumulated over time. This adds a
close(descriptor)inthe error path before rethrowing. (The existing cancel-handler close only runs
once the dispatch source has been created, so it does not cover this earlier
failure.)
Testing
make checkpasses (formatting + license headers).DirectoryWatcherTestaddstestFailingHandlerDoesNotLeakDescriptors,which forces a throwing handler through several retry cycles and asserts the
descriptor count stays bounded. Full suite (5 tests) passes.
Fixes #1097