Skip to content

Commit 7e4a7f8

Browse files
committed
cancel device controller tasks on deinit, capture socket
1 parent 2d571ae commit 7e4a7f8

File tree

5 files changed

+30
-5
lines changed

5 files changed

+30
-5
lines changed

Sources/SwiftOCADevice/OCP.1/Backend/CF/Ocp1CFController.swift

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,13 +118,13 @@ actor Ocp1CFStreamController: Ocp1CFControllerPrivate, CustomStringConvertible {
118118
connectionPrefix = OcaTcpConnectionPrefix
119119
}
120120

121-
receiveMessageTask = Task { [weak self] in
121+
receiveMessageTask = Task { [weak self, socket] in
122122
do {
123123
repeat {
124+
guard !Task.isCancelled else { break }
124125
let messages = try await OcaDevice
125126
.receiveMessages { try await Array(socket.read(count: $0)) }
126127
self?._messagesContinuation.yield(messages)
127-
if Task.isCancelled { break }
128128
} while true
129129
} catch {
130130
self?._messagesContinuation.finish(throwing: error)
@@ -144,6 +144,12 @@ actor Ocp1CFStreamController: Ocp1CFControllerPrivate, CustomStringConvertible {
144144
_messagesContinuation.finish()
145145
}
146146

147+
deinit {
148+
receiveMessageTask?.cancel()
149+
keepAliveTask?.cancel()
150+
_messagesContinuation.finish()
151+
}
152+
147153
var heartbeatTime = Duration.seconds(0) {
148154
didSet {
149155
heartbeatTimeDidChange(from: oldValue)

Sources/SwiftOCADevice/OCP.1/Backend/FlyingSocks/Ocp1FlyingFoxController.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,11 @@ actor Ocp1FlyingFoxController: Ocp1ControllerInternal, CustomStringConvertible {
8585
keepAliveTask = nil
8686
}
8787

88+
deinit {
89+
keepAliveTask?.cancel()
90+
outputStream.finish()
91+
}
92+
8893
nonisolated var identifier: String {
8994
String(describing: id)
9095
}

Sources/SwiftOCADevice/OCP.1/Backend/FlyingSocks/Ocp1FlyingSocksStreamController.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,10 @@ actor Ocp1FlyingSocksStreamController: Ocp1ControllerInternal, CustomStringConve
8787
keepAliveTask = nil
8888
}
8989

90+
deinit {
91+
keepAliveTask?.cancel()
92+
}
93+
9094
nonisolated var identifier: String {
9195
address
9296
}

Sources/SwiftOCADevice/OCP.1/Backend/IORing/Ocp1IORingController.swift

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,23 +110,23 @@ actor Ocp1IORingStreamController: Ocp1IORingControllerPrivate, CustomStringConve
110110
connectionPrefix = OcaTcpConnectionPrefix
111111
}
112112

113-
receiveMessageTask = Task { [weak self] in
113+
receiveMessageTask = Task { [weak self, socket] in
114114
do {
115115
repeat {
116+
guard !Task.isCancelled else { break }
116117
let messages = try await OcaDevice.receiveMessages { try await socket.read(
117118
count: $0,
118119
awaitingAllRead: true
119120
) }
120121
self?._messagesContinuation.yield(messages)
121-
if Task.isCancelled { break }
122122
} while true
123123
} catch {
124124
self?._messagesContinuation.finish(throwing: error)
125125
}
126126
}
127127
}
128128

129-
func close() async throws {
129+
func close() {
130130
// don't close the socket, it will be closed when last reference is released
131131

132132
keepAliveTask?.cancel()
@@ -138,6 +138,12 @@ actor Ocp1IORingStreamController: Ocp1IORingControllerPrivate, CustomStringConve
138138
_messagesContinuation.finish()
139139
}
140140

141+
deinit {
142+
receiveMessageTask?.cancel()
143+
keepAliveTask?.cancel()
144+
_messagesContinuation.finish()
145+
}
146+
141147
var heartbeatTime = Duration.seconds(1) {
142148
didSet {
143149
heartbeatTimeDidChange(from: oldValue)

Sources/SwiftOCADevice/OCP.1/Backend/Local/LocalController.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,8 @@ actor OcaLocalController: Ocp1ControllerInternal {
5353
}
5454

5555
func close() throws {}
56+
57+
deinit {
58+
keepAliveTask?.cancel()
59+
}
5660
}

0 commit comments

Comments
 (0)