Skip to content

Commit 7c9d51a

Browse files
committed
fix(coderd/healthcheck/derphealth): do not override parent context deadline
1 parent 2fa3710 commit 7c9d51a

File tree

2 files changed

+48
-2
lines changed

2 files changed

+48
-2
lines changed

coderd/healthcheck/derphealth/derp.go

+6-2
Original file line numberDiff line numberDiff line change
@@ -236,8 +236,12 @@ func (r *NodeReport) derpURL() *url.URL {
236236
}
237237

238238
func (r *NodeReport) Run(ctx context.Context) {
239-
ctx, cancel := context.WithTimeout(ctx, 10*time.Second)
240-
defer cancel()
239+
// If there already is a deadline set on the context, do not override it.
240+
if _, ok := ctx.Deadline(); !ok {
241+
dCtx, cancel := context.WithTimeout(ctx, 10*time.Second)
242+
defer cancel()
243+
ctx = dCtx
244+
}
241245

242246
r.Severity = health.SeverityOK
243247
r.ClientLogs = [][]string{}

coderd/healthcheck/derphealth/derp_test.go

+42
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"net/http/httptest"
99
"net/url"
1010
"testing"
11+
"time"
1112

1213
"github.com/stretchr/testify/assert"
1314
"github.com/stretchr/testify/require"
@@ -84,6 +85,47 @@ func TestDERP(t *testing.T) {
8485
}
8586
})
8687

88+
t.Run("TimeoutCtx", func(t *testing.T) {
89+
t.Parallel()
90+
91+
derpSrv := derp.NewServer(key.NewNode(), func(format string, args ...any) { t.Logf(format, args...) })
92+
defer derpSrv.Close()
93+
srv := httptest.NewServer(derphttp.Handler(derpSrv))
94+
defer srv.Close()
95+
96+
var (
97+
// nolint:gocritic // testing a deadline exceeded
98+
ctx, cancel = context.WithTimeout(context.Background(), time.Nanosecond)
99+
report = derphealth.Report{}
100+
derpURL, _ = url.Parse(srv.URL)
101+
opts = &derphealth.ReportOptions{
102+
DERPMap: &tailcfg.DERPMap{Regions: map[int]*tailcfg.DERPRegion{
103+
1: {
104+
EmbeddedRelay: true,
105+
RegionID: 999,
106+
Nodes: []*tailcfg.DERPNode{{
107+
Name: "1a",
108+
RegionID: 999,
109+
HostName: derpURL.Host,
110+
IPv4: derpURL.Host,
111+
STUNPort: -1,
112+
InsecureForTests: true,
113+
ForceHTTP: true,
114+
}},
115+
},
116+
}},
117+
}
118+
)
119+
cancel()
120+
121+
report.Run(ctx, opts)
122+
123+
assert.False(t, report.Healthy)
124+
if assert.NotNil(t, report.Error) {
125+
assert.Contains(t, context.DeadlineExceeded, *report.Error)
126+
}
127+
})
128+
87129
t.Run("HealthyWithNodeDegraded", func(t *testing.T) {
88130
t.Parallel()
89131

0 commit comments

Comments
 (0)