From a617423c6f4e2a0946eea5acb85a524622aec48b Mon Sep 17 00:00:00 2001 From: Shinolr Date: Thu, 3 Dec 2026 13:01:01 +0800 Subject: [PATCH] Fix aria2 download completion with user config enabled Pass --no-conf so aria2c ignores user configuration that can keep the process alive (enable-rpc/daemon) or change parsed output. Enable resume explicitly with --continue=true because archive services rely on the .aria2 sidecar. Add a regression test for the generated arguments. --- .../Services/Aria2DownloadService.swift | 42 ++++++++++++++----- .../Aria2DownloadServiceTests.swift | 30 +++++++++++++ 2 files changed, 61 insertions(+), 11 deletions(-) create mode 100644 Tests/XcodesKitTests/Aria2DownloadServiceTests.swift diff --git a/Sources/XcodesKit/Services/Aria2DownloadService.swift b/Sources/XcodesKit/Services/Aria2DownloadService.swift index 11ea699..3feeb02 100644 --- a/Sources/XcodesKit/Services/Aria2DownloadService.swift +++ b/Sources/XcodesKit/Services/Aria2DownloadService.swift @@ -15,17 +15,11 @@ public struct Aria2DownloadService: Sendable { ) -> AsyncThrowingStream { let process = Process() process.executableURL = aria2Path.url - process.arguments = [ - "--header=Cookie: \(cookies.map { "\($0.name)=\($0.value)" }.joined(separator: "; "))", - "--max-connection-per-server=16", - "--split=16", - "--summary-interval=1", - "--stop-with-process=\(ProcessInfo.processInfo.processIdentifier)", - "--dir=\(destination.parent.string)", - "--out=\(destination.basename())", - "--human-readable=false", - url.absoluteString, - ] + process.arguments = Self.arguments( + cookieHeader: cookies.map { "\($0.name)=\($0.value)" }.joined(separator: "; "), + destination: destination, + url: url + ) let state = UnauthorizedState() let runner = ProcessProgressStreamRunner( @@ -60,6 +54,32 @@ public struct Aria2DownloadService: Sendable { return runner.stream() } + + static func arguments( + cookieHeader: String, + destination: Path, + url: URL, + hostProcessID: Int32 = ProcessInfo.processInfo.processIdentifier + ) -> [String] { + // aria2c reads ~/.aria2/aria2.conf by default. Options such as enable-rpc + // or daemon can keep the process alive after the download finishes, which + // prevents ProcessProgressStreamRunner from completing the stream. + // Resume is enabled explicitly because the archive services use the .aria2 + // sidecar to detect incomplete downloads. + [ + "--no-conf", + "--continue=true", + "--header=Cookie: \(cookieHeader)", + "--max-connection-per-server=16", + "--split=16", + "--summary-interval=1", + "--stop-with-process=\(hostProcessID)", + "--dir=\(destination.parent.string)", + "--out=\(destination.basename())", + "--human-readable=false", + url.absoluteString, + ] + } } private final class UnauthorizedState: Sendable { diff --git a/Tests/XcodesKitTests/Aria2DownloadServiceTests.swift b/Tests/XcodesKitTests/Aria2DownloadServiceTests.swift new file mode 100644 index 0000000..5bece53 --- /dev/null +++ b/Tests/XcodesKitTests/Aria2DownloadServiceTests.swift @@ -0,0 +1,30 @@ +import XCTest +@preconcurrency import Path +@testable import XcodesKit + +final class Aria2DownloadServiceTests: XCTestCase { + func testArgumentsIgnoreUserConfigAndKeepResumeEnabled() throws { + let destination = try XCTUnwrap(Path("/tmp/Xcode-26.0.xip")) + let url = try XCTUnwrap(URL(string: "https://example.com/Xcode_26.0.xip")) + + let arguments = Aria2DownloadService.arguments( + cookieHeader: "myacinfo=token", + destination: destination, + url: url, + hostProcessID: 4242 + ) + + XCTAssertEqual(arguments.first, "--no-conf") + XCTAssertTrue(arguments.contains("--continue=true")) + XCTAssertFalse(arguments.contains { $0.hasPrefix("--conf-path=") }) + XCTAssertTrue(arguments.contains("--header=Cookie: myacinfo=token")) + XCTAssertTrue(arguments.contains("--max-connection-per-server=16")) + XCTAssertTrue(arguments.contains("--split=16")) + XCTAssertTrue(arguments.contains("--summary-interval=1")) + XCTAssertTrue(arguments.contains("--stop-with-process=4242")) + XCTAssertTrue(arguments.contains("--dir=/tmp")) + XCTAssertTrue(arguments.contains("--out=Xcode-26.0.xip")) + XCTAssertTrue(arguments.contains("--human-readable=false")) + XCTAssertEqual(arguments.last, url.absoluteString) + } +}