Skip to content

Commit

Permalink
Fix oauth timeout bug
Browse files Browse the repository at this point in the history
  • Loading branch information
porjo committed Nov 23, 2023
1 parent 3162574 commit 2819935
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 6 deletions.
7 changes: 7 additions & 0 deletions internal/limiter/limiter.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,13 @@ func NewLimitTransport(logger utils.Logger, rt http.RoundTripper, lr LimitRange,
return lt, nil
}

// HasStarted returns whether the LimitTransport has seen use
func (t *LimitTransport) HasStarted() bool {
t.reader.Lock()
defer t.reader.Unlock()
return t.readerInit
}

func (t *LimitTransport) RoundTrip(r *http.Request) (*http.Response, error) {

contentType := r.Header.Get("Content-Type")
Expand Down
5 changes: 5 additions & 0 deletions internal/progress/progress.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,11 @@ func (p *Progress) Run(ctx context.Context, signalChan chan os.Signal) {
}

func (p *Progress) Output() {

if !p.transport.HasStarted() {
return
}

s := p.transport.GetMonitorStatus()
avgRate := float64(s.AvgRate)
elapsed := time.Since(s.Start).Round(time.Second)
Expand Down
12 changes: 6 additions & 6 deletions oauth.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,9 +153,8 @@ func readConfig(scopes []string) (*oauth2.Config, error) {

// startCallbackWebServer starts a web server that listens on http://localhost:8080.
// The webserver waits for an oauth code in the three-legged auth flow.
func startCallbackWebServer(oAuthPort int) (callbackCh chan CallbackStatus, err error) {
ctx, cancel := context.WithTimeout(context.Background(), callbackTimeout)
defer cancel()
func startCallbackWebServer(ctx context.Context, oAuthPort int) (callbackCh chan CallbackStatus, err error) {
ctx2, _ := context.WithTimeout(ctx, callbackTimeout)

Check failure on line 157 in oauth.go

View workflow job for this annotation

GitHub Actions / lint

lostcancel: the cancel function returned by context.WithTimeout should be called, not discarded, to avoid a context leak (govet)

quitChan := make(chan struct{})
defer close(quitChan)
Expand Down Expand Up @@ -188,7 +187,7 @@ func startCallbackWebServer(oAuthPort int) (callbackCh chan CallbackStatus, err
// shutdown server on context timeout
go func() {
select {
case <-ctx.Done():
case <-ctx2.Done():
log.Printf("Timed out waiting for request to callback server: http://localhost:%d\n", oAuthPort)
err := srv.Shutdown(ctx)
if err != nil {
Expand All @@ -201,7 +200,8 @@ func startCallbackWebServer(oAuthPort int) (callbackCh chan CallbackStatus, err

go func() {
defer close(callbackCh)
if err := srv.ListenAndServe(); err != http.ErrServerClosed {
//if err := srv.ListenAndServe(); err != http.ErrServerClosed {
if err := srv.ListenAndServe(); err != nil {
log.Printf("Callback server error: %s\n", err)
}
}()
Expand Down Expand Up @@ -254,7 +254,7 @@ func BuildOAuthHTTPClient(ctx context.Context, scopes []string, oAuthPort int) (
// Start web server.
// This is how this program receives the authorization code
// when the browser redirects.
callbackCh, err := startCallbackWebServer(oAuthPort)
callbackCh, err := startCallbackWebServer(ctx, oAuthPort)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit 2819935

Please sign in to comment.