Skip to content

Commit

Permalink
Merge pull request #73 from projectdiscovery/feat/catch-ListenAndServ…
Browse files Browse the repository at this point in the history
…e-error

feat: catch `http.ListenAndServe` error
  • Loading branch information
Mzack9999 committed Aug 9, 2024
2 parents 3294cde + 236bce0 commit 0406a98
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
17 changes: 16 additions & 1 deletion clistats.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,9 +187,24 @@ func (s *Statistics) Start() error {
Handler: mux,
}

errChan := make(chan error, 1)
var done atomic.Bool

go func() {
_ = s.httpServer.ListenAndServe()
if err := s.httpServer.ListenAndServe(); err != nil && err != http.ErrServerClosed && !done.Load() {
errChan <- err
}
}()

// catch initial fatal errors
select {
case err := <-errChan:
return err
case <-time.After(250 * time.Millisecond):
done.Store(true)
close(errChan)
}

}
return nil
}
Expand Down
18 changes: 18 additions & 0 deletions clistats_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,21 @@ func TestStartMultipleTimes(t *testing.T) {
require.Nil(t, err)
}
}

func TestStartMultipleTimesWithoutStopping(t *testing.T) {
client, err := New()
require.Nil(t, err)

for i := 1; i <= 2; i++ {
err = client.Start()

if i == 1 {
require.Nil(t, err)
} else {
require.NotNil(t, err)
}
}

err = client.Stop()
require.Nil(t, err)
}

0 comments on commit 0406a98

Please sign in to comment.