Skip to content

Commit

Permalink
Avoid assertion failures in deinitializers (#168)
Browse files Browse the repository at this point in the history
  • Loading branch information
tristanlabelle authored Dec 6, 2024
1 parent 24f495b commit 5bae870
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 18 deletions.
7 changes: 1 addition & 6 deletions Sources/WebDriver/Session.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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.
}
}
27 changes: 15 additions & 12 deletions Sources/WinAppDriver/WinAppDriver.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand All @@ -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
}
}
}

0 comments on commit 5bae870

Please sign in to comment.