Skip to content

fix(healthcheck): don't allow panics to exit coderd #7276

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion coderd/apidoc/docs.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion coderd/apidoc/swagger.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion coderd/coderd.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,8 @@ func New(options *Options) *API {
if options.HealthcheckFunc == nil {
options.HealthcheckFunc = func(ctx context.Context) (*healthcheck.Report, error) {
return healthcheck.Run(ctx, &healthcheck.ReportOptions{
DERPMap: options.DERPMap.Clone(),
AccessURL: options.AccessURL,
DERPMap: options.DERPMap.Clone(),
})
}
}
Expand Down
15 changes: 10 additions & 5 deletions coderd/healthcheck/accessurl.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ type AccessURLReport struct {
Reachable bool
StatusCode int
HealthzResponse string
Err error
Error error
}

type AccessURLOptions struct {
Expand All @@ -27,32 +27,37 @@ func (r *AccessURLReport) Run(ctx context.Context, opts *AccessURLOptions) {
ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
defer cancel()

if opts.AccessURL == nil {
r.Error = xerrors.New("access URL is nil")
return
}

if opts.Client == nil {
opts.Client = http.DefaultClient
}

accessURL, err := opts.AccessURL.Parse("/healthz")
if err != nil {
r.Err = xerrors.Errorf("parse healthz endpoint: %w", err)
r.Error = xerrors.Errorf("parse healthz endpoint: %w", err)
return
}

req, err := http.NewRequestWithContext(ctx, "GET", accessURL.String(), nil)
if err != nil {
r.Err = xerrors.Errorf("create healthz request: %w", err)
r.Error = xerrors.Errorf("create healthz request: %w", err)
return
}

res, err := opts.Client.Do(req)
if err != nil {
r.Err = xerrors.Errorf("get healthz endpoint: %w", err)
r.Error = xerrors.Errorf("get healthz endpoint: %w", err)
return
}
defer res.Body.Close()

body, err := io.ReadAll(res.Body)
if err != nil {
r.Err = xerrors.Errorf("read healthz response: %w", err)
r.Error = xerrors.Errorf("read healthz response: %w", err)
return
}

Expand Down
6 changes: 3 additions & 3 deletions coderd/healthcheck/accessurl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func TestAccessURL(t *testing.T) {
assert.True(t, report.Reachable)
assert.Equal(t, http.StatusOK, report.StatusCode)
assert.Equal(t, "OK", report.HealthzResponse)
assert.NoError(t, report.Err)
assert.NoError(t, report.Error)
})

t.Run("404", func(t *testing.T) {
Expand Down Expand Up @@ -66,7 +66,7 @@ func TestAccessURL(t *testing.T) {
assert.True(t, report.Reachable)
assert.Equal(t, http.StatusNotFound, report.StatusCode)
assert.Equal(t, string(resp), report.HealthzResponse)
assert.NoError(t, report.Err)
assert.NoError(t, report.Error)
})

t.Run("ClientErr", func(t *testing.T) {
Expand Down Expand Up @@ -102,7 +102,7 @@ func TestAccessURL(t *testing.T) {
assert.False(t, report.Reachable)
assert.Equal(t, 0, report.StatusCode)
assert.Equal(t, "", report.HealthzResponse)
assert.ErrorIs(t, report.Err, expErr)
assert.ErrorIs(t, report.Error, expErr)
})
}

Expand Down
36 changes: 27 additions & 9 deletions coderd/healthcheck/derp.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ type DERPReport struct {
Netcheck *netcheck.Report `json:"netcheck"`
NetcheckErr error `json:"netcheck_err"`
NetcheckLogs []string `json:"netcheck_logs"`

Error error `json:"error"`
}

type DERPRegionReport struct {
Expand All @@ -41,6 +43,7 @@ type DERPRegionReport struct {

Region *tailcfg.DERPRegion `json:"region"`
NodeReports []*DERPNodeReport `json:"node_reports"`
Error error `json:"error"`
}
type DERPNodeReport struct {
mu sync.Mutex
Expand All @@ -55,6 +58,7 @@ type DERPNodeReport struct {
UsesWebsocket bool `json:"uses_websocket"`
ClientLogs [][]string `json:"client_logs"`
ClientErrs [][]error `json:"client_errs"`
Error error `json:"error"`

STUN DERPStunReport `json:"stun"`
}
Expand All @@ -77,12 +81,19 @@ func (r *DERPReport) Run(ctx context.Context, opts *DERPReportOptions) {

wg.Add(len(opts.DERPMap.Regions))
for _, region := range opts.DERPMap.Regions {
region := region
go func() {
defer wg.Done()
regionReport := DERPRegionReport{
var (
region = region
regionReport = DERPRegionReport{
Region: region,
}
)
go func() {
defer wg.Done()
defer func() {
if err := recover(); err != nil {
regionReport.Error = xerrors.Errorf("%v", err)
}
}()

regionReport.Run(ctx)

Expand Down Expand Up @@ -117,14 +128,21 @@ func (r *DERPRegionReport) Run(ctx context.Context) {

wg.Add(len(r.Region.Nodes))
for _, node := range r.Region.Nodes {
node := node
go func() {
defer wg.Done()

nodeReport := DERPNodeReport{
var (
node = node
nodeReport = DERPNodeReport{
Node: node,
Healthy: true,
}
)

go func() {
defer wg.Done()
defer func() {
if err := recover(); err != nil {
nodeReport.Error = xerrors.Errorf("%v", err)
}
}()

nodeReport.Run(ctx)

Expand Down
13 changes: 13 additions & 0 deletions coderd/healthcheck/healthcheck.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"sync"
"time"

"golang.org/x/xerrors"
"tailscale.com/tailcfg"
)

Expand Down Expand Up @@ -38,6 +39,12 @@ func Run(ctx context.Context, opts *ReportOptions) (*Report, error) {
wg.Add(1)
go func() {
defer wg.Done()
defer func() {
if err := recover(); err != nil {
report.DERP.Error = xerrors.Errorf("%v", err)
}
}()

report.DERP.Run(ctx, &DERPReportOptions{
DERPMap: opts.DERPMap,
})
Expand All @@ -46,6 +53,12 @@ func Run(ctx context.Context, opts *ReportOptions) (*Report, error) {
wg.Add(1)
go func() {
defer wg.Done()
defer func() {
if err := recover(); err != nil {
report.AccessURL.Error = xerrors.Errorf("%v", err)
}
}()

report.AccessURL.Run(ctx, &AccessURLOptions{
AccessURL: opts.AccessURL,
Client: opts.Client,
Expand Down
7 changes: 6 additions & 1 deletion docs/api/debug.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,14 @@ curl -X GET http://coder-server:8080/api/v2/debug/health \
```json
{
"access_url": {
"err": null,
"error": null,
"healthy": true,
"healthzResponse": "string",
"reachable": true,
"statusCode": 0
},
"derp": {
"error": null,
"healthy": true,
"netcheck": {
"captivePortal": "string",
Expand Down Expand Up @@ -82,12 +83,14 @@ curl -X GET http://coder-server:8080/api/v2/debug/health \
"netcheck_logs": ["string"],
"regions": {
"property1": {
"error": null,
"healthy": true,
"node_reports": [
{
"can_exchange_messages": true,
"client_errs": [[null]],
"client_logs": [["string"]],
"error": null,
"healthy": true,
"node": {
"certName": "string",
Expand Down Expand Up @@ -141,12 +144,14 @@ curl -X GET http://coder-server:8080/api/v2/debug/health \
}
},
"property2": {
"error": null,
"healthy": true,
"node_reports": [
{
"can_exchange_messages": true,
"client_errs": [[null]],
"client_logs": [["string"]],
"error": null,
"healthy": true,
"node": {
"certName": "string",
Expand Down
Loading