From 442c9cc0e24c1b4f5f11559eb29dc0318669bef0 Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 29 Sep 2026 11:42:01 +0000 Subject: [PATCH] Bound dot_clean output draining after the process exits A backgrounded child that inherits stdout/stderr keeps the pipe open past the executable's exit, so the unconditional drain wait could outlive the configured timeout. Read via a readability handler, wait for EOF with a short grace period, then release and close the pipe. Co-Authored-By: Claude Sonnet 5.5 Claude-Session: https://claude.ai/code/session_01Apys6XL8BAS3tGcbfB2hYu --- Sources/BrickDrop/DotCleanRunner.swift | 45 +++++++++++++++---- .../BrickDropTests/DotCleanRunnerTests.swift | 14 ++++++ 2 files changed, 50 insertions(+), 9 deletions(-) diff --git a/Sources/BrickDrop/DotCleanRunner.swift b/Sources/BrickDrop/DotCleanRunner.swift index 0ed4b15..1c7713d 100644 --- a/Sources/BrickDrop/DotCleanRunner.swift +++ b/Sources/BrickDrop/DotCleanRunner.swift @@ -18,7 +18,20 @@ enum DotCleanError: LocalizedError { } private final class OutputBox: @unchecked Sendable { - var data = Data() + private let lock = NSLock() + private var buffer = Data() + + func append(_ chunk: Data) { + lock.lock() + buffer.append(chunk) + lock.unlock() + } + + var data: Data { + lock.lock() + defer { lock.unlock() } + return buffer + } } struct DotCleanRunner: Sendable { @@ -43,13 +56,27 @@ struct DotCleanRunner: Sendable { process.terminationHandler = { _ in finished.signal() } try process.run() - // Drain the pipe concurrently so a chatty child can't block on a full buffer. + // Drain the pipe as data arrives so a chatty child can't block on a full buffer. EOF only + // arrives once every holder of the write end closes it, which a backgrounded grandchild + // can delay long after the executable itself exits, so every wait on it is bounded. let box = OutputBox() - let drained = DispatchGroup() - drained.enter() - DispatchQueue.global().async { - box.data = output.fileHandleForReading.readDataToEndOfFile() - drained.leave() + let drained = DispatchSemaphore(value: 0) + let reader = output.fileHandleForReading + reader.readabilityHandler = { handle in + let chunk = handle.availableData + if chunk.isEmpty { + handle.readabilityHandler = nil + drained.signal() + } else { + box.append(chunk) + } + } + // Stops reading and closes our end so nothing is left blocked on an inherited pipe. + func releasePipe(waitingUpTo grace: TimeInterval) { + if drained.wait(timeout: .now() + grace) == .timedOut { + reader.readabilityHandler = nil + try? reader.close() + } } if finished.wait(timeout: .now() + timeout) == .timedOut { @@ -58,10 +85,10 @@ struct DotCleanRunner: Sendable { kill(process.processIdentifier, SIGKILL) _ = finished.wait(timeout: .now() + 2) } - _ = drained.wait(timeout: .now() + 2) + releasePipe(waitingUpTo: 1) throw DotCleanError.timedOut(seconds: timeout) } - drained.wait() + releasePipe(waitingUpTo: 1) let message = String(data: box.data, encoding: .utf8) ?? "" guard process.terminationStatus == 0 else { diff --git a/Tests/BrickDropTests/DotCleanRunnerTests.swift b/Tests/BrickDropTests/DotCleanRunnerTests.swift index a870da1..20eb9a6 100644 --- a/Tests/BrickDropTests/DotCleanRunnerTests.swift +++ b/Tests/BrickDropTests/DotCleanRunnerTests.swift @@ -37,4 +37,18 @@ final class DotCleanRunnerTests: XCTestCase { } XCTAssertLessThan(Date().timeIntervalSince(started), 10) } + + func testReturnsWhenExitedProcessLeavesBackgroundChildHoldingOutput() throws { + let script = FileManager.default.temporaryDirectory.appending(path: "orphan-\(UUID().uuidString).sh") + defer { try? FileManager.default.removeItem(at: script) } + try "#!/bin/sh\necho done\nsleep 30 &\nexit 0\n".write(to: script, atomically: true, encoding: .utf8) + try FileManager.default.setAttributes([.posixPermissions: 0o755], ofItemAtPath: script.path) + let runner = DotCleanRunner(executableURL: script, timeout: 0.5) + let started = Date() + + let output = try runner.clean(volumeURL: URL(fileURLWithPath: "/Volumes/Card")) + + XCTAssertEqual(output.trimmingCharacters(in: .whitespacesAndNewlines), "done") + XCTAssertLessThan(Date().timeIntervalSince(started), 10) + } }