mirror of
https://github.com/aculix/knotchy.git
synced 2026-10-27 07:15:53 +00:00
Fix first-run setup timing out on the kernel download
On a Mac without the Linux kernel, setup ran `container system start --enable-kernel-install` with a 120 s limit. That command downloads a 697 MB archive without printing anything, so most connections hit the limit and the window showed `timedOut([...])`. - Start the service with --disable-kernel-install (10 min: a first start still pulls the 69 MB base filesystem). - Download the kernel in its own step with progress, then hand the file to `container system kernel set --tar ... --digest ...`, which checks it against the runtime's digest. - Opening the setup window no longer starts the download; Set Up does. - Timeouts and runner errors read as sentences. - A rebuild deletes the image it replaced, which frees about 2 GB each time, and deletes or stops the builder VM unless it was already running (it kept about 2.8 GB of memory). - Setup asks for 10 GB free, measured on a real setup.
This commit is contained in:
1 parent
4b67728cce
commit
9fd654298a
13 files changed
+559
-128
No files matched your search
@@ -0,0 +1,100 @@
|
||||
import Foundation
|
||||
|
||||
/// A data task streamed into a file, so a partial download lives only in its own folder.
|
||||
final class FileDownload: NSObject, URLSessionDataDelegate, @unchecked Sendable {
|
||||
// Touched only on the session's serial delegate queue.
|
||||
private var continuation: CheckedContinuation<Void, any Error>?
|
||||
/// A completion that arrived before anyone waited: a task cancelled before it started.
|
||||
private var outcome: Result<Void, any Error>?
|
||||
private var handle: FileHandle?
|
||||
private var written: Int64 = 0
|
||||
private var expected: Int64?
|
||||
private var writeError: (any Error)?
|
||||
private let destination: URL
|
||||
private let progress: @Sendable (_ written: Int64, _ expected: Int64?) -> Void
|
||||
|
||||
private init(destination: URL, progress: @escaping @Sendable (Int64, Int64?) -> Void) {
|
||||
self.destination = destination
|
||||
self.progress = progress
|
||||
}
|
||||
|
||||
/// `expected` is nil when the server doesn't say.
|
||||
static func fetch(_ url: URL, to destination: URL,
|
||||
progress: @escaping @Sendable (_ written: Int64, _ expected: Int64?) -> Void) async throws {
|
||||
let delegate = FileDownload(destination: destination, progress: progress)
|
||||
let queue = OperationQueue()
|
||||
queue.maxConcurrentOperationCount = 1
|
||||
let session = URLSession(configuration: .ephemeral, delegate: delegate, delegateQueue: queue)
|
||||
defer { session.finishTasksAndInvalidate() }
|
||||
// Created up front: cancelling before it exists would invalidate the session, and creating it then throws.
|
||||
let task = session.dataTask(with: url)
|
||||
try await withTaskCancellationHandler {
|
||||
try await withCheckedThrowingContinuation { (continuation: CheckedContinuation<Void, any Error>) in
|
||||
queue.addOperation {
|
||||
if let outcome = delegate.outcome {
|
||||
continuation.resume(with: outcome)
|
||||
} else {
|
||||
delegate.continuation = continuation
|
||||
task.resume()
|
||||
}
|
||||
}
|
||||
}
|
||||
} onCancel: {
|
||||
task.cancel()
|
||||
}
|
||||
}
|
||||
|
||||
func urlSession(_ session: URLSession, dataTask: URLSessionDataTask, didReceive response: URLResponse,
|
||||
completionHandler: @escaping @Sendable (URLSession.ResponseDisposition) -> Void) {
|
||||
if let http = response as? HTTPURLResponse, !(200..<300).contains(http.statusCode) {
|
||||
writeError = URLError(.badServerResponse)
|
||||
return completionHandler(.cancel)
|
||||
}
|
||||
do {
|
||||
guard FileManager.default.createFile(atPath: destination.path, contents: nil) else {
|
||||
throw CocoaError(.fileWriteUnknown, userInfo: [NSFilePathErrorKey: destination.path])
|
||||
}
|
||||
handle = try FileHandle(forWritingTo: destination)
|
||||
} catch {
|
||||
writeError = error
|
||||
return completionHandler(.cancel)
|
||||
}
|
||||
if response.expectedContentLength > 0 { expected = response.expectedContentLength }
|
||||
completionHandler(.allow)
|
||||
}
|
||||
|
||||
func urlSession(_ session: URLSession, dataTask: URLSessionDataTask, didReceive data: Data) {
|
||||
guard let handle, writeError == nil else { return }
|
||||
do {
|
||||
try handle.write(contentsOf: data)
|
||||
written += Int64(data.count)
|
||||
progress(written, expected)
|
||||
} catch {
|
||||
writeError = error
|
||||
dataTask.cancel()
|
||||
}
|
||||
}
|
||||
|
||||
func urlSession(_ session: URLSession, task: URLSessionTask, didCompleteWithError error: (any Error)?) {
|
||||
try? handle?.close()
|
||||
handle = nil
|
||||
// Ours comes first: cancelling after it adds a "cancelled" error.
|
||||
let result: Result<Void, any Error> = if let failure = writeError ?? error { .failure(failure) } else { .success(()) }
|
||||
if let waiting = continuation {
|
||||
continuation = nil
|
||||
waiting.resume(with: result)
|
||||
} else {
|
||||
outcome = result
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
extension FileDownload {
|
||||
static func removeFolders(prefix: String) {
|
||||
let temporary = URL(fileURLWithPath: NSTemporaryDirectory())
|
||||
let names = (try? FileManager.default.contentsOfDirectory(atPath: temporary.path)) ?? []
|
||||
for name in names where name.hasPrefix(prefix) {
|
||||
try? FileManager.default.removeItem(at: temporary.appendingPathComponent(name))
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -58,7 +58,7 @@ public struct SystemRuntimeInstaller: RuntimeInstalling {
|
||||
}
|
||||
let package = DownloadedPackage(directory: directory, name: release.url.lastPathComponent)
|
||||
do {
|
||||
try await PackageDownload.fetch(release.url, to: package.file, progress: progress)
|
||||
try await FileDownload.fetch(release.url, to: package.file) { written, _ in progress(written) }
|
||||
} catch {
|
||||
discard(package)
|
||||
throw DownloadError.failed(error.localizedDescription)
|
||||
@@ -76,11 +76,7 @@ public struct SystemRuntimeInstaller: RuntimeInstalling {
|
||||
|
||||
/// Folders left by quitting mid-download or a crash.
|
||||
public static func removeLeftovers() {
|
||||
let temporary = URL(fileURLWithPath: NSTemporaryDirectory())
|
||||
let names = (try? FileManager.default.contentsOfDirectory(atPath: temporary.path)) ?? []
|
||||
for name in names where name.hasPrefix("knotchy-runtime-") {
|
||||
try? FileManager.default.removeItem(at: temporary.appendingPathComponent(name))
|
||||
}
|
||||
FileDownload.removeFolders(prefix: "knotchy-runtime-")
|
||||
}
|
||||
|
||||
@MainActor
|
||||
@@ -122,88 +118,3 @@ private final class RunningInstaller: InstallerSession {
|
||||
|
||||
var hasQuit: Bool { app.isTerminated }
|
||||
}
|
||||
|
||||
/// A data task streamed into the package file, so the partial download lives only in its own folder.
|
||||
private final class PackageDownload: NSObject, URLSessionDataDelegate, @unchecked Sendable {
|
||||
// Touched only on the session's serial delegate queue.
|
||||
private var continuation: CheckedContinuation<Void, any Error>?
|
||||
/// A completion that arrived before anyone waited: a task cancelled before it started.
|
||||
private var outcome: Result<Void, any Error>?
|
||||
private var handle: FileHandle?
|
||||
private var written: Int64 = 0
|
||||
private var writeError: (any Error)?
|
||||
private let destination: URL
|
||||
private let progress: @Sendable (Int64) -> Void
|
||||
|
||||
private init(destination: URL, progress: @escaping @Sendable (Int64) -> Void) {
|
||||
self.destination = destination
|
||||
self.progress = progress
|
||||
}
|
||||
|
||||
static func fetch(_ url: URL, to destination: URL, progress: @escaping @Sendable (Int64) -> Void) async throws {
|
||||
let delegate = PackageDownload(destination: destination, progress: progress)
|
||||
let queue = OperationQueue()
|
||||
queue.maxConcurrentOperationCount = 1
|
||||
let session = URLSession(configuration: .ephemeral, delegate: delegate, delegateQueue: queue)
|
||||
defer { session.finishTasksAndInvalidate() }
|
||||
// Created up front: cancelling before it exists would invalidate the session, and creating it then throws.
|
||||
let task = session.dataTask(with: url)
|
||||
try await withTaskCancellationHandler {
|
||||
try await withCheckedThrowingContinuation { (continuation: CheckedContinuation<Void, any Error>) in
|
||||
queue.addOperation {
|
||||
if let outcome = delegate.outcome {
|
||||
continuation.resume(with: outcome)
|
||||
} else {
|
||||
delegate.continuation = continuation
|
||||
task.resume()
|
||||
}
|
||||
}
|
||||
}
|
||||
} onCancel: {
|
||||
task.cancel()
|
||||
}
|
||||
}
|
||||
|
||||
func urlSession(_ session: URLSession, dataTask: URLSessionDataTask, didReceive response: URLResponse,
|
||||
completionHandler: @escaping @Sendable (URLSession.ResponseDisposition) -> Void) {
|
||||
if let http = response as? HTTPURLResponse, !(200..<300).contains(http.statusCode) {
|
||||
writeError = URLError(.badServerResponse)
|
||||
return completionHandler(.cancel)
|
||||
}
|
||||
do {
|
||||
guard FileManager.default.createFile(atPath: destination.path, contents: nil) else {
|
||||
throw CocoaError(.fileWriteUnknown, userInfo: [NSFilePathErrorKey: destination.path])
|
||||
}
|
||||
handle = try FileHandle(forWritingTo: destination)
|
||||
} catch {
|
||||
writeError = error
|
||||
return completionHandler(.cancel)
|
||||
}
|
||||
completionHandler(.allow)
|
||||
}
|
||||
|
||||
func urlSession(_ session: URLSession, dataTask: URLSessionDataTask, didReceive data: Data) {
|
||||
guard let handle, writeError == nil else { return }
|
||||
do {
|
||||
try handle.write(contentsOf: data)
|
||||
written += Int64(data.count)
|
||||
progress(written)
|
||||
} catch {
|
||||
writeError = error
|
||||
dataTask.cancel()
|
||||
}
|
||||
}
|
||||
|
||||
func urlSession(_ session: URLSession, task: URLSessionTask, didCompleteWithError error: (any Error)?) {
|
||||
try? handle?.close()
|
||||
handle = nil
|
||||
// Ours comes first: cancelling after it adds a "cancelled" error.
|
||||
let result: Result<Void, any Error> = if let failure = writeError ?? error { .failure(failure) } else { .success(()) }
|
||||
if let waiting = continuation {
|
||||
continuation = nil
|
||||
waiting.resume(with: result)
|
||||
} else {
|
||||
outcome = result
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -17,8 +17,10 @@ public final class SetupFlow {
|
||||
case update
|
||||
}
|
||||
|
||||
public static let requiredFreeBytes: Int64 = 3_000_000_000
|
||||
/// The build peaks at about 9 GB: the builder alone takes 4 GB until it is deleted. About 5 GB stays in use.
|
||||
public static let requiredFreeBytes: Int64 = 10_000_000_000
|
||||
static let vmDetail = "About 3 minutes · downloads about 225 MB"
|
||||
static let kernelDownloading = "Downloading the Linux kernel…"
|
||||
|
||||
public private(set) var mac: Status = .pending("Checking…")
|
||||
public private(set) var unsupported: Unsupported?
|
||||
@@ -51,10 +53,11 @@ public final class SetupFlow {
|
||||
|
||||
/// With the controller's live progress on top, so a setup the notch started shows here too.
|
||||
public var runtime: Status {
|
||||
if case .settingUp(let issue) = driver.state, issue == .serviceStopped || issue == .kernelMissing {
|
||||
return .running("Starting the runtime…", fraction: nil)
|
||||
switch driver.state {
|
||||
case .settingUp(.serviceStopped): return .running("Starting the runtime…", fraction: nil)
|
||||
case .settingUp(.kernelMissing): return .running(driver.setupLog ?? Self.kernelDownloading, fraction: nil)
|
||||
default: return runtimeBase
|
||||
}
|
||||
return runtimeBase
|
||||
}
|
||||
|
||||
public var vm: Status {
|
||||
@@ -142,6 +145,9 @@ public final class SetupFlow {
|
||||
switch await driver.checkSetup() {
|
||||
case .ready?: vmBase = .done("Ready")
|
||||
case .needs(let issue)? where Self.isImageIssue(issue): vmBase = .pending(Self.vmDetail)
|
||||
case .needs(.kernelMissing)?:
|
||||
runtimeBase = .pending(Self.kernelDetail(version))
|
||||
vmBase = .pending(Self.vmDetail)
|
||||
case .failed(let issue, let message)?: fail(issue, message)
|
||||
case nil where Self.isBrowserOpen(driver.state): vmBase = .done("Ready") // A running VM has an image.
|
||||
case .needs?, nil: break // Busy, or the runtime row's business: the live rows show it.
|
||||
@@ -264,7 +270,7 @@ public final class SetupFlow {
|
||||
continue
|
||||
}
|
||||
switch checked {
|
||||
case .needs(let issue) where Self.isImageIssue(issue):
|
||||
case .needs(let issue) where Self.isImageIssue(issue) || issue == .kernelMissing:
|
||||
guard let result = await driver.runSetup(issue, continuing: true) else { continue }
|
||||
return apply(result)
|
||||
case .needs:
|
||||
@@ -279,14 +285,25 @@ public final class SetupFlow {
|
||||
private func apply(_ outcome: SetupRunOutcome) {
|
||||
switch outcome {
|
||||
case .ready:
|
||||
kernelInstalled()
|
||||
vmBase = .done("Ready")
|
||||
case .needs(let issue):
|
||||
if Self.isImageIssue(issue) { vmBase = .pending(Self.vmDetail) }
|
||||
if Self.isImageIssue(issue) {
|
||||
kernelInstalled()
|
||||
vmBase = .pending(Self.vmDetail)
|
||||
}
|
||||
case .failed(let issue, let message):
|
||||
if Self.isImageIssue(issue) { kernelInstalled() }
|
||||
fail(issue, message)
|
||||
}
|
||||
}
|
||||
|
||||
/// The run got past the kernel, so a row still waiting for it is done.
|
||||
private func kernelInstalled() {
|
||||
guard case .pending = runtimeBase, runtimeAction == nil, case .current(let version)? = runtimeStatus else { return }
|
||||
runtimeBase = .done(Self.installedText(version))
|
||||
}
|
||||
|
||||
private func fail(_ issue: SetupIssue, _ message: String) {
|
||||
if Self.isImageIssue(issue) {
|
||||
vmBase = .failed(message, details: driver.setupOutput)
|
||||
@@ -309,7 +326,7 @@ public final class SetupFlow {
|
||||
guard let facts else { return }
|
||||
let free = facts.bytesFree.map { "\(Self.gigabytes($0)) GB free" }
|
||||
if needsSpace, let bytes = facts.bytesFree, bytes < Self.requiredFreeBytes {
|
||||
mac = .failed("Setting up needs about 3 GB free. \(Self.gigabytes(bytes)) GB is free.", details: [])
|
||||
mac = .failed("Setting up needs about 10 GB free. \(Self.gigabytes(bytes)) GB is free.", details: [])
|
||||
} else {
|
||||
mac = .done([facts.osVersion, free].compactMap { $0 }.joined(separator: " · "))
|
||||
}
|
||||
@@ -325,13 +342,17 @@ public final class SetupFlow {
|
||||
// MARK: - Text
|
||||
|
||||
static func installDetail(_ release: ContainerRuntime.Release) -> String {
|
||||
"Apple's container \(release.version) · \(megabytes(release.size)) MB · installs with Apple's Installer"
|
||||
"Apple's container \(release.version) · \(megabytes(release.size)) MB, then a 700 MB Linux kernel · installs with Apple's Installer"
|
||||
}
|
||||
|
||||
static func installedText(_ version: String?) -> String {
|
||||
version.map { "container \($0) installed" } ?? "container installed"
|
||||
}
|
||||
|
||||
static func kernelDetail(_ version: String?) -> String {
|
||||
"\(installedText(version)) · still needs its Linux kernel (700 MB)"
|
||||
}
|
||||
|
||||
static func downloadText(_ bytes: Int64, of total: Int64) -> String {
|
||||
"Downloading… \(megabytes(bytes)) of \(megabytes(total)) MB"
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user