Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@iheanyi
Copy link
Contributor

@iheanyi iheanyi commented Oct 18, 2021

This pull request handles the errors from running the proxy in a goroutine more gracefully. Before, we'd ignore any errors that happen when the proxy starts up, so the program would freeze when we try to get the local address. Now we conditionally create an error channel to return the errors from the the proxy runner gracefully.

@iheanyi iheanyi requested a review from fatih October 18, 2021 19:40
@iheanyi iheanyi requested a review from a team as a code owner October 18, 2021 19:40
@fatih
Copy link
Member

fatih commented Oct 19, 2021

This looks good. There are two edge cases, though, that could make it still stuck:

  1. The client.DatabaseBranches call could be finished earlier than p.Run, and errCh could still be nil in that case. Hence, it would block getting the local address.
  2. The p.Run() could run for an extended time before returning an error, so we should have a timeout.

I added an improvement that solves both cases. I didn't pushed it to your branch, but you can apply the patch directly:

diff --git a/internal/cmd/shell/shell.go b/internal/cmd/shell/shell.go
index e537c57..ebedb07 100644
--- a/internal/cmd/shell/shell.go
+++ b/internal/cmd/shell/shell.go
@@ -7,6 +7,7 @@ import (
 	"net"
 	"os"
 	"path/filepath"
+	"time"
 
 	"github.com/planetscale/cli/internal/cmdutil"
 	"github.com/planetscale/cli/internal/printer"
@@ -116,14 +117,9 @@ second argument:
 				return fmt.Errorf("couldn't create proxy client: %s", err)
 			}
 
-			var errCh chan error
-
+			errCh := make(chan error, 1)
 			go func() {
-				err := p.Run(ctx)
-				if err != nil {
-					errCh = make(chan error, 1)
-					errCh <- err
-				}
+				errCh <- p.Run(ctx)
 			}()
 
 			dbBranch, err := client.DatabaseBranches.Get(ctx, &ps.GetDatabaseBranchRequest{
@@ -145,8 +141,11 @@ second argument:
 				return errors.New("database branch is not ready yet")
 			}
 
-			if errCh != nil {
-				return <-errCh
+			select {
+			case proxyErr := <-errCh:
+				return proxyErr
+			case <-time.After(time.Second * 10):
+				return errors.New("proxy timeout retrieving the certs")
 			}
 
 			addr, err := p.LocalAddr()

@iheanyi
Copy link
Contributor Author

iheanyi commented Oct 19, 2021

@fatih With this select block, won't it always return early because we're always sending an error to it? We don't want to return nil in this case.

Copy link
Member

@fatih fatih left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me 👍🏼

@iheanyi iheanyi merged commit da1aec4 into main Oct 19, 2021
@iheanyi iheanyi deleted the iheanyi/gracefully-handle-proxy-errors branch October 19, 2021 17:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants