From be3ae4e90a135cde62e41875e9f7b158cd93e52d Mon Sep 17 00:00:00 2001 From: Tristan Labelle Date: Fri, 6 Dec 2024 13:46:39 -0500 Subject: [PATCH] Avoid assertion failures in deinitializers --- Sources/WebDriver/Session.swift | 7 +------ Sources/WinAppDriver/WinAppDriver.swift | 27 ++++++++++++++----------- 2 files changed, 16 insertions(+), 18 deletions(-) diff --git a/Sources/WebDriver/Session.swift b/Sources/WebDriver/Session.swift index 5e7b37f..2097e12 100644 --- a/Sources/WebDriver/Session.swift +++ b/Sources/WebDriver/Session.swift @@ -363,11 +363,6 @@ public class Session { } deinit { - do { try delete() } - catch let error as ErrorResponse { - assertionFailure("Error in Session.delete: \(error)") - } catch { - assertionFailure("Unexpected error in Session.delete: \(error)") - } + try? delete() // Call `delete` directly to handle errors. } } diff --git a/Sources/WinAppDriver/WinAppDriver.swift b/Sources/WinAppDriver/WinAppDriver.swift index 31bf80b..6855dd2 100644 --- a/Sources/WinAppDriver/WinAppDriver.swift +++ b/Sources/WinAppDriver/WinAppDriver.swift @@ -18,9 +18,9 @@ public class WinAppDriver: WebDriver { public static let defaultStartWaitTime: TimeInterval = 1.0 private let httpWebDriver: HTTPWebDriver - private let processTree: Win32ProcessTree? + private var processTree: Win32ProcessTree? /// The write end of a pipe that is connected to the child process's stdin. - private let childStdinHandle: HANDLE? + private var childStdinHandle: HANDLE? private init( httpWebDriver: HTTPWebDriver, @@ -115,16 +115,7 @@ public class WinAppDriver: WebDriver { } deinit { - if let processTree { - do { - try processTree.terminate(waitTime: TimeInterval.infinity) - } catch { - assertionFailure("WinAppDriver did not terminate within the expected time: \(error).") - } - } - if let childStdinHandle { - CloseHandle(childStdinHandle) - } + try? close() // Call close() directly to handle errors. } @discardableResult @@ -135,4 +126,16 @@ public class WinAppDriver: WebDriver { public func isInconclusiveInteraction(error: ErrorResponse.Status) -> Bool { error == .winAppDriver_elementNotInteractable || httpWebDriver.isInconclusiveInteraction(error: error) } + + public func close() throws { + if let childStdinHandle { + CloseHandle(childStdinHandle) + self.childStdinHandle = nil + } + + if let processTree { + try processTree.terminate(waitTime: TimeInterval.infinity) + self.processTree = nil + } + } }