Skip to content

Commit 5ce9097

Browse files
committed
fix: run dns flush async to prevent blocking main runloop timer
1 parent 124b015 commit 5ce9097

1 file changed

Lines changed: 17 additions & 14 deletions

File tree

TomeHelper/HostsEditor.swift

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -104,21 +104,24 @@ class HostsEditor {
104104
}
105105

106106
private func flushDNSCache() {
107-
let task = Process()
108-
task.launchPath = "/usr/bin/dscacheutil"
109-
task.arguments = ["-flushcache"]
110-
try? task.run()
111-
task.waitUntilExit()
112-
113-
guard let pid = mDNSResponderPID() else {
114-
log("WARN: could not find mDNSResponder PID — DNS cache not flushed via HUP")
115-
return
107+
// run async so a hung process can't block the main RunLoop timer
108+
DispatchQueue.global(qos: .utility).async {
109+
let task = Process()
110+
task.launchPath = "/usr/bin/dscacheutil"
111+
task.arguments = ["-flushcache"]
112+
try? task.run()
113+
task.waitUntilExit()
114+
115+
guard let pid = mDNSResponderPID() else {
116+
log("WARN: could not find mDNSResponder PID — DNS cache not flushed via HUP")
117+
return
118+
}
119+
let task2 = Process()
120+
task2.launchPath = "/bin/kill"
121+
task2.arguments = ["-HUP", String(pid)]
122+
try? task2.run()
123+
task2.waitUntilExit()
116124
}
117-
let task2 = Process()
118-
task2.launchPath = "/bin/kill"
119-
task2.arguments = ["-HUP", String(pid)]
120-
try? task2.run()
121-
task2.waitUntilExit()
122125
}
123126

124127
private func mDNSResponderPID() -> Int32? {

0 commit comments

Comments
 (0)