From 68f49e254cfbd901680bef9007ec757933bdcd84 Mon Sep 17 00:00:00 2001 From: "T. R. Bernstein" Date: Sun, 13 Sep 2026 23:06:28 +0200 Subject: [PATCH] Restore inotify limits even when a test body throws The limits were only written back after a successful body, so a failing limit test left the shared kernel at the lowered values and every later run failed with ENOSPC. The helper now restores in a `defer` and can lower a chosen subset of the limits. --- .../Utilities/withLowInotifyWatchLimit.swift | 28 +++++++++++++------ 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/Tests/InotifyIntegrationTests/Utilities/withLowInotifyWatchLimit.swift b/Tests/InotifyIntegrationTests/Utilities/withLowInotifyWatchLimit.swift index d50966e..8eea19b 100644 --- a/Tests/InotifyIntegrationTests/Utilities/withLowInotifyWatchLimit.swift +++ b/Tests/InotifyIntegrationTests/Utilities/withLowInotifyWatchLimit.swift @@ -1,10 +1,28 @@ import Foundation -func withInotifyWatchLimit(of limit: Int, _ body: () async throws -> Void) async throws { +enum InotifyLimit: String, CaseIterable { + case userWatches = "max_user_watches" + case userInstances = "max_user_instances" + case queuedEvents = "max_queued_events" +} + +func withInotifyWatchLimit( + of limit: Int, + for limits: [InotifyLimit] = InotifyLimit.allCases, + _ body: () async throws -> Void +) async throws { let confPath = URL(filePath: "/proc/sys/fs/inotify") - let filenames = ["max_user_watches", "max_user_instances", "max_queued_events"] + let filenames = limits.map(\.rawValue) var previousLimits: [String: String] = [:] + defer { + for filename in filenames { + let filePath = confPath.appending(path: filename) + guard let previousLimit = previousLimits[filename] else { continue } + try? previousLimit.write(to: filePath, atomically: false, encoding: .utf8) + } + } + for filename in filenames { let filePath = confPath.appending(path: filename) let currentLimit = try String(contentsOf: filePath, encoding: .utf8) @@ -13,10 +31,4 @@ func withInotifyWatchLimit(of limit: Int, _ body: () async throws -> Void) async } try await body() - - for filename in filenames { - let filePath = confPath.appending(path: filename) - guard let previousLimit = previousLimits[filename] else { continue } - try previousLimit.write(to: filePath, atomically: false, encoding: .utf8) - } }