@@ -275,7 +275,7 @@ func (a *App) UpdatePortVisibility(ctx context.Context, codespaceName string, ar
275275 }
276276 defer safeClose (session , & err )
277277
278- errc := make ( chan error , 1 )
278+ g , ctx := errgroup . WithContext ( ctx )
279279
280280 // TODO: check if port visibility can be updated in parallel instead of sequentially
281281 for _ , port := range ports {
@@ -285,36 +285,33 @@ func (a *App) UpdatePortVisibility(ctx context.Context, codespaceName string, ar
285285 ctx , cancel := context .WithTimeout (ctx , 30 * time .Second )
286286 defer cancel ()
287287
288- go func (port portVisibility ) {
288+ g . Go ( func () error {
289289 updateNotif , err := session .WaitForPortNotification (ctx , port .number , liveshare .PortChangeKindUpdate )
290290 if err != nil {
291- errc <- fmt .Errorf ("error waiting for port %d to update: %w" , port .number , err )
292- return
291+ return fmt .Errorf ("error waiting for port %d to update: %w" , port .number , err )
292+
293293 }
294294 if ! updateNotif .Success {
295295 if updateNotif .StatusCode == http .StatusForbidden {
296- errc <- newErrUpdatingPortVisibility (port .number , port .visibility , errUpdatePortVisibilityForbidden )
297- return
296+ return newErrUpdatingPortVisibility (port .number , port .visibility , errUpdatePortVisibilityForbidden )
298297 }
299- errc <- newErrUpdatingPortVisibility (port .number , port .visibility , errors .New (updateNotif .ErrorDetail ))
300- return
301- }
302- errc <- nil // success
303- }(port )
298+ return newErrUpdatingPortVisibility (port .number , port .visibility , errors .New (updateNotif .ErrorDetail ))
304299
305- err := session .UpdateSharedServerPrivacy (ctx , port .number , port .visibility )
306- if err != nil {
307- return fmt .Errorf ("error updating port %d to %s: %w" , port .number , port .visibility , err )
308- }
300+ }
301+ return nil // success
302+ })
309303
310- // wait for success or failure
311- select {
312- case <- ctx .Done ():
313- return ctx .Err ()
314- case err := <- errc :
304+ g .Go (func () error {
305+ err := session .UpdateSharedServerPrivacy (ctx , port .number , port .visibility )
315306 if err != nil {
316- return err
307+ return fmt . Errorf ( "error updating port %d to %s: %w" , port . number , port . visibility , err )
317308 }
309+ return nil
310+ })
311+
312+ // wait for success or failure
313+ if err := g .Wait (); err != nil {
314+ return err
318315 }
319316
320317 a .StopProgressIndicator ()
0 commit comments