Skip to content

Commit 0c5da07

Browse files
committed
Fix LightningConnection to support the new connections semantics
1 parent 1f114f5 commit 0c5da07

File tree

1 file changed

+4
-12
lines changed

1 file changed

+4
-12
lines changed

YubiKit/YubiKit/LightningSmartCardConnection.swift

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -72,27 +72,19 @@ private actor LightningConnectionManager {
7272

7373
static let shared = LightningConnectionManager()
7474

75-
private var connectionTask: Task<LightningSmartCardConnection, Error>?
7675
private var pendingConnectionPromise: Promise<LightningSmartCardConnection>?
7776
private var connectionState: (connectionID: ConnectionID, didCloseConnection: (Promise<Error?>))?
7877

7978
private init() {}
8079

8180
func connect() async throws -> LightningSmartCardConnection {
82-
// If a connection task is already running, await its result
83-
if let connectionTask {
84-
trace(message: "awaiting existing connection task")
85-
_ = try await connectionTask.value
86-
// we cancel this task because only one of multiple
87-
// concurrent connections can succed
88-
throw ConnectionError.cancelled
81+
// If there is already a connection the caller must close the connection first.
82+
if connectionState != nil || pendingConnectionPromise != nil {
83+
throw ConnectionError.busy
8984
}
9085

9186
// Otherwise, create and store a new connection task.
9287
let task = Task { () -> LightningSmartCardConnection in
93-
// When the task finishes (on any path), clear it to allow a new connection.
94-
defer { self.connectionTask = nil }
95-
9688
trace(message: "begin new connection task")
9789

9890
do {
@@ -121,12 +113,12 @@ private actor LightningConnectionManager {
121113
trace(message: "connection failed: \(error.localizedDescription)")
122114
// Cleanup on failure
123115
self.pendingConnectionPromise = nil
116+
self.connectionState = nil
124117
await EAAccessoryWrapper.shared.stopMonitoring()
125118
throw error
126119
}
127120
}
128121

129-
self.connectionTask = task
130122
return try await task.value
131123
}
132124

0 commit comments

Comments
 (0)