Free fork of NotProton: Windows Steam games in Mac Steam, on CodeWeavers' Wine and Apple's Game Porting Toolkit
Swift apple-silicond3dmetaldxmtdxvkgame-porting-toolkitgptkmac-gamingmacosmetalfxrosetta-2steamsteam-playwinewinecx
fix(setup): stop a Steam that hangs after its own shutdown
raymondchinsdea809c79489
diff --git a/app/Sources/BridgeplayApp/Model/SteamBundle.swift b/app/Sources/BridgeplayApp/Model/SteamBundle.swiftindex 2534b5c..15c1b3a 100644--- a/app/Sources/BridgeplayApp/Model/SteamBundle.swift+++ b/app/Sources/BridgeplayApp/Model/SteamBundle.swift@@ -99,6 +99,15 @@ enum SteamBundle {steam.terminate()}}+ // Steam on its login screen takes steam://exit, logs its shutdown, then hangs in+ // exit() (#158). Once it has logged that and its helpers are gone, it has nothing+ // left to write.+ var stuckExiting: @Sendable (Date) -> Bool = { asked in+ SteamBundle.loggedShutdown(since: asked) && !Shell.processIsRunning(named: "Steam Helper")+ }+ var kill: @Sendable () -> Void = {+ _ = try? Shell.run("/usr/bin/pkill", ["-KILL", "-x", "-U", String(getuid()), "steam_osx"])+ }var sweep: @Sendable (String) -> Void = { _ = try? Shell.run("/usr/bin/pkill", ["-f", $0]) }var pause: @Sendable () -> Void = { Thread.sleep(forTimeInterval: 0.5) }var attempts = 40@@ -106,9 +115,10 @@ enum SteamBundle {}// Asks this account's Steam to quit with steam://exit, which Steam can ignore on its- // login or update screen, then with a macOS quit. Steam is never killed: it may be- // writing its config or an update. Steam ignores SIGTERM, so none is sent. A Steam- // another account runs is out of reach and reported as such.+ // login or update screen, then with a macOS quit. Steam is killed only when it is stuck+ // after its own shutdown; otherwise it may be writing its config or an update. Steam+ // ignores SIGTERM, so none is sent. A Steam another account runs is out of reach and+ // reported as such.static func stopClient(app: URL = SupportPaths.Steam.app,helpers: [URL] = [SupportPaths.Steam.innerApp],@@ -126,6 +136,7 @@ enum SteamBundle {}return !client.runningHere()}+ let asked = Date()client.askToQuit()var quit = quits(within: client.attempts)if !quit {@@ -133,6 +144,11 @@ enum SteamBundle {client.requestQuit()quit = quits(within: client.escalationAttempts)}+ if !quit, client.stuckExiting(asked) {+ AppLog.note("stop: Steam logged its shutdown but did not exit, killing it")+ client.kill()+ quit = quits(within: client.escalationAttempts)+ }guard quit else {throw StepFailure(@@ -148,6 +164,34 @@ enum SteamBundle {return true}+ static func loggedShutdown(+ since date: Date,+ log: URL = SupportPaths.Steam.userData.appending(path: "logs/bootstrap_log.txt")+ ) -> Bool {+ guard let handle = try? FileHandle(forReadingFrom: log) else { return false }+ defer { try? handle.close() }+ let end = (try? handle.seekToEnd()) ?? 0+ try? handle.seek(toOffset: end > 16_384 ? end - 16_384 : 0)+ let tail = String(decoding: (try? handle.readToEnd()) ?? Data(), as: UTF8.self)+ return loggedShutdown(in: tail, since: date)+ }++ // bootstrap_log.txt lines read "[2026-10-22 20:37:09] Shutdown", in local time.+ static func loggedShutdown(in log: String, since date: Date, timeZone: TimeZone = .current) -> Bool {+ let format = DateFormatter()+ format.locale = Locale(identifier: "en_US_POSIX")+ format.timeZone = timeZone+ format.dateFormat = "yyyy-MM-dd HH:mm:ss"+ for line in log.split(whereSeparator: \.isNewline).reversed() {+ let text = line.trimmingCharacters(in: .whitespaces)+ guard text.hasPrefix("["), text.hasSuffix("] Shutdown"),+ let at = format.date(from: String(text.dropFirst().prefix(19))) else { continue }+ // The log keeps whole seconds.+ return at >= date.addingTimeInterval(-1)+ }+ return false+ }+// Its own type, so a caller that only touches this account's files can carry on.struct OpenInAnotherAccount: LocalizedError {let step: Stringdiff --git a/app/Tests/BridgeplayAppTests/SteamBundleTests.swift b/app/Tests/BridgeplayAppTests/SteamBundleTests.swiftindex e726207..693db3e 100644--- a/app/Tests/BridgeplayAppTests/SteamBundleTests.swift+++ b/app/Tests/BridgeplayAppTests/SteamBundleTests.swift@@ -48,21 +48,24 @@ struct SteamBundleTests {// MARK: Stopping Steam- // A stand-in Steam: running here and in another account, and which request it quits on.+ // A stand-in Steam: running here and in another account, which request it quits on,+ // and whether it is stuck after logging its shutdown.private final class FakeSteam: @unchecked Sendable {- enum Request: Comparable { case exitURL, quit, never }+ enum Request: Comparable { case exitURL, quit, kill, never }private let lock = NSLock()private var here: Boolprivate var elsewhere: Boolprivate let quitsOn: Request+ private let stuck: Boolprivate(set) var requests: [Request] = []private(set) var swept: [String] = []- init(here: Bool, elsewhere: Bool = false, quitsOn: Request = .exitURL) {+ init(here: Bool, elsewhere: Bool = false, quitsOn: Request = .exitURL, stuck: Bool = false) {self.here = hereself.elsewhere = elsewhereself.quitsOn = quitsOn+ self.stuck = stuck}private func receive(_ request: Request) {@@ -78,6 +81,8 @@ struct SteamBundleTests {runningHere: { self.lock.withLock { self.here } },askToQuit: { self.receive(.exitURL) },requestQuit: { self.receive(.quit) },+ stuckExiting: { _ in self.stuck },+ kill: { self.receive(.kill) },sweep: { pattern in self.lock.withLock { self.swept.append(pattern) } },pause: {},attempts: 3,@@ -127,6 +132,50 @@ struct SteamBundleTests {#expect(steam.swept.isEmpty)}+ @Test("Steam stuck after logging its shutdown is killed, then swept")+ func stuckSteamIsKilled() throws {+ let steam = FakeSteam(here: true, quitsOn: .kill, stuck: true)+ #expect(try stop(steam))+ #expect(steam.requests == [.exitURL, .quit, .kill])+ #expect(steam.swept == [#"/tmp/Steam\.app/"#])+ }++ @Test("Steam that has not logged its shutdown is never killed")+ func runningSteamIsNotKilled() {+ let steam = FakeSteam(here: true, quitsOn: .never, stuck: false)+ #expect(throws: StepFailure.self) { try stop(steam) }+ #expect(!steam.requests.contains(.kill))+ }++ private static let utc = TimeZone(identifier: "UTC")!+ private static func at(_ text: String) -> Date {+ let format = DateFormatter()+ format.locale = Locale(identifier: "en_US_POSIX")+ format.timeZone = utc+ format.dateFormat = "yyyy-MM-dd HH:mm:ss"+ return format.date(from: text)!+ }++ @Test("A shutdown logged after the request counts; an earlier one or none does not")+ func shutdownInTheLog() {+ let log = """+ [2026-10-22 20:24:56] Nothing to do+ [2026-10-22 20:37:09] Shutdown+ """+ let asked = Self.at("2026-10-22 20:37:05")+ #expect(SteamBundle.loggedShutdown(in: log, since: asked, timeZone: Self.utc))+ #expect(SteamBundle.loggedShutdown(in: log + "\r\n", since: asked, timeZone: Self.utc))+ #expect(!SteamBundle.loggedShutdown(in: log, since: Self.at("2026-10-22 20:40:00"), timeZone: Self.utc))+ #expect(!SteamBundle.loggedShutdown(in: "[2026-10-22 20:37:09] Startup", since: asked, timeZone: Self.utc))+ // Only the latest shutdown counts: an old one above a newer start is not this quit.+ let restarted = """+ [2026-10-22 20:30:00] Shutdown+ [2026-10-22 20:31:00] Startup+ """+ #expect(!SteamBundle.loggedShutdown(in: restarted, since: asked, timeZone: Self.utc))+ #expect(!SteamBundle.loggedShutdown(since: asked, log: URL(filePath: "/nonexistent/bootstrap_log.txt")))+ }+// steam://exit only reaches this account's Steam; another account's keeps the bundle open.@Test("Steam open in another account stops the change without asking this one")func otherAccountsSteamIsReported() {