@@ -157,36 +157,56 @@ abstract class AbstractGitpodPortForwardingService : GitpodPortForwardingService
157157 .filter { portsNumbersFromNonServedPorts.contains(it) || ! portsNumbersFromPortsList.contains(it) }
158158
159159 runJob(lifetime) {
160- processPortsInBatches(servedPortsToStartForwarding) { port ->
161- operationSemaphore.withPermit {
162- startForwarding(port)
163- allPortsToKeep.add(port.localPort)
160+ coroutineScope {
161+ // Stop operations first to free up resources
162+ launch {
163+ processPortsInBatches(forwardedPortsToStopForwarding) { port ->
164+ operationSemaphore.withPermit { stopForwarding(port) }
165+ }
164166 }
165- }
166-
167- processPortsInBatches(exposedPortsToStartExposingOnClient) { port ->
168- operationSemaphore.withPermit {
169- startExposingOnClient(port)
170- allPortsToKeep.add(port.localPort)
167+ launch {
168+ processPortsInBatches(exposedPortsToStopExposingOnClient) { port ->
169+ operationSemaphore.withPermit { stopExposingOnClient(port) }
170+ }
171171 }
172- }
173172
174- processPortsInBatches(forwardedPortsToStopForwarding) { port ->
175- operationSemaphore.withPermit { stopForwarding(port) }
176- }
173+ // Wait for stop operations to complete
174+ awaitAll()
177175
178- processPortsInBatches(exposedPortsToStopExposingOnClient) { port ->
179- operationSemaphore.withPermit { stopExposingOnClient(port) }
180- }
176+ // Start new operations
177+ launch {
178+ processPortsInBatches(servedPortsToStartForwarding) { port ->
179+ operationSemaphore.withPermit {
180+ startForwarding(port)
181+ allPortsToKeep.add(port.localPort)
182+ }
183+ }
184+ }
185+ launch {
186+ processPortsInBatches(exposedPortsToStartExposingOnClient) { port ->
187+ operationSemaphore.withPermit {
188+ startExposingOnClient(port)
189+ allPortsToKeep.add(port.localPort)
190+ }
191+ }
192+ }
181193
182- processPortsInBatches(portsList) { port ->
183- application.invokeLater {
184- updatePortsPresentation(port)
185- allPortsToKeep.add(port.localPort)
194+ // Update presentation in parallel with start operations
195+ launch {
196+ processPortsInBatches(portsList) { port ->
197+ application.invokeLater {
198+ updatePortsPresentation(port)
199+ allPortsToKeep.add(port.localPort)
200+ }
201+ }
186202 }
187- }
188203
189- cleanupUnusedLifetimes(allPortsToKeep)
204+ // Wait for all operations to complete
205+ awaitAll()
206+
207+ // Clean up after all operations are done
208+ cleanupUnusedLifetimes(allPortsToKeep)
209+ }
190210 }
191211 }
192212
@@ -195,7 +215,7 @@ abstract class AbstractGitpodPortForwardingService : GitpodPortForwardingService
195215 try {
196216 batch.forEach { port ->
197217 try {
198- withTimeout(5000 ) { // Add timeout to prevent hanging operations
218+ withTimeout(5000 ) {
199219 action(port)
200220 }
201221 } catch (e: Exception ) {
@@ -205,7 +225,7 @@ abstract class AbstractGitpodPortForwardingService : GitpodPortForwardingService
205225 delay(BATCH_DELAY )
206226 } catch (e: Exception ) {
207227 thisLogger().error(" gitpod: Error processing batch" , e)
208- delay(BATCH_DELAY * 2 ) // Double delay on error
228+ delay(BATCH_DELAY * 2 )
209229 }
210230 }
211231 }
0 commit comments