Skip to content

Commit dfcea35

Browse files
refactor: improve task management in PortForward and AppState classes
1 parent b83ee68 commit dfcea35

File tree

5 files changed

+26
-0
lines changed

5 files changed

+26
-0
lines changed

platforms/macos/Sources/AppState+AutoRefresh.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ extension AppState {
1010

1111
/// Starts a background task that periodically refreshes the port list.
1212
func startAutoRefresh() {
13+
stopAutoRefresh()
1314
refreshTask = Task { @MainActor in
1415
await self.refresh()
1516
while !Task.isCancelled {

platforms/macos/Sources/Managers/PortForwardManager+Monitoring.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@ import Foundation
33
extension PortForwardManager {
44
/// Starts the connection monitoring task.
55
func startMonitoring() {
6+
if isMonitoring, let monitorTask, !monitorTask.isCancelled {
7+
return
8+
}
9+
10+
monitorTask?.cancel()
611
isMonitoring = true
712
monitorTask = Task {
813
while !Task.isCancelled && isMonitoring {

platforms/macos/Sources/Managers/PortForwardManager.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,9 @@ final class PortForwardManager {
134134
func startConnection(_ id: UUID) {
135135
guard !isKillingProcesses else { return }
136136
guard let state = connection(for: id) else { return }
137+
guard state.portForwardTask == nil, state.proxyTask == nil else { return }
138+
guard state.portForwardStatus != .connecting, state.proxyStatus != .connecting else { return }
139+
guard !state.isFullyConnected else { return }
137140
let config = state.config
138141

139142
// Reset intentional stop flag when starting

platforms/macos/Sources/Managers/PortForwardProcessManager+Execution.swift

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@ extension PortForwardProcessManager {
2727
process.standardOutput = pipe
2828
process.standardError = pipe
2929

30+
outputTasks[id]?[.portForward]?.cancel()
31+
if let existing = processes[id]?[.portForward], existing.isRunning {
32+
existing.terminate()
33+
}
34+
3035
try process.run()
3136

3237
if processes[id] == nil {
@@ -60,6 +65,11 @@ extension PortForwardProcessManager {
6065
process.standardOutput = pipe
6166
process.standardError = pipe
6267

68+
outputTasks[id]?[.proxy]?.cancel()
69+
if let existing = processes[id]?[.proxy], existing.isRunning {
70+
existing.terminate()
71+
}
72+
6373
try process.run()
6474

6575
if processes[id] == nil {
@@ -116,6 +126,11 @@ extension PortForwardProcessManager {
116126
process.standardOutput = pipe
117127
process.standardError = pipe
118128

129+
outputTasks[id]?[.proxy]?.cancel()
130+
if let existing = processes[id]?[.proxy], existing.isRunning {
131+
existing.terminate()
132+
}
133+
119134
try process.run()
120135

121136
if processes[id] == nil {

platforms/macos/Sources/Managers/PortForwardProcessManager.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ actor PortForwardProcessManager {
3333
// MARK: - Output Reading
3434

3535
func startReadingOutput(pipe: Pipe, id: UUID, type: PortForwardProcessType) {
36+
outputTasks[id]?[type]?.cancel()
37+
3638
let task = Task { [weak self] in
3739
let handle = pipe.fileHandleForReading
3840

0 commit comments

Comments
 (0)