Skip to content

Commit 2a133d1

Browse files
committed
properly release net.Conn
1 parent d4181b0 commit 2a133d1

File tree

4 files changed

+29
-10
lines changed

4 files changed

+29
-10
lines changed

coderd/tailnet.go

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ func (s *ServerTailnet) updateNode(id uuid.UUID, node *tailnet.Node) {
138138
}
139139
}
140140

141-
func (s *ServerTailnet) ReverseProxy(targetURL, dashboardURL *url.URL, agentID uuid.UUID) (*httputil.ReverseProxy, func(), error) {
141+
func (s *ServerTailnet) ReverseProxy(targetURL, dashboardURL *url.URL, agentID uuid.UUID) (_ *httputil.ReverseProxy, release func(), _ error) {
142142
proxy := httputil.NewSingleHostReverseProxy(targetURL)
143143
proxy.ErrorHandler = func(w http.ResponseWriter, r *http.Request, err error) {
144144
site.RenderStaticErrorPage(w, r, site.ErrorPageData{
@@ -257,7 +257,7 @@ func (*ServerTailnet) nodeIsLegacy(node *tailnet.Node) bool {
257257
return node.Addresses[0].Addr() == codersdk.WorkspaceAgentIP
258258
}
259259

260-
func (s *ServerTailnet) AgentConn(ctx context.Context, agentID uuid.UUID) (*codersdk.WorkspaceAgentConn, func(), error) {
260+
func (s *ServerTailnet) AgentConn(ctx context.Context, agentID uuid.UUID) (_ *codersdk.WorkspaceAgentConn, release func(), _ error) {
261261
node, err := s.awaitNodeExists(ctx, agentID, 5*time.Second)
262262
if err != nil {
263263
return nil, nil, xerrors.Errorf("get agent node: %w", err)
@@ -297,8 +297,6 @@ func (s *ServerTailnet) DialAgentNetConn(ctx context.Context, agentID uuid.UUID,
297297
if err != nil {
298298
return nil, xerrors.Errorf("acquire agent conn: %w", err)
299299
}
300-
defer release()
301-
defer conn.Close()
302300

303301
node, err := s.getNode(agentID)
304302
if err != nil {
@@ -309,13 +307,29 @@ func (s *ServerTailnet) DialAgentNetConn(ctx context.Context, agentID uuid.UUID,
309307
port, _ := strconv.ParseUint(rawPort, 10, 16)
310308
ipp := netip.AddrPortFrom(node.Addresses[0].Addr(), uint16(port))
311309

310+
var nc net.Conn
312311
if network == "tcp" {
313-
return conn.DialContextTCP(ctx, ipp)
312+
nc, err = conn.DialContextTCP(ctx, ipp)
314313
} else if network == "udp" {
315-
return conn.DialContextUDP(ctx, ipp)
314+
nc, err = conn.DialContextUDP(ctx, ipp)
316315
} else {
317316
return nil, xerrors.Errorf("unknown network %q", network)
318317
}
318+
319+
return &netConnCloser{Conn: nc, close: func() {
320+
release()
321+
conn.Close()
322+
}}, err
323+
}
324+
325+
type netConnCloser struct {
326+
net.Conn
327+
close func()
328+
}
329+
330+
func (c *netConnCloser) Close() error {
331+
c.close()
332+
return c.Conn.Close()
319333
}
320334

321335
func (s *ServerTailnet) Close() error {

coderd/workspaceapps/proxy.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,18 @@ var nonCanonicalHeaders = map[string]string{
6161
}
6262

6363
type AgentProvider interface {
64+
// ReverseProxy returns an httputil.ReverseProxy for proxying HTTP requests
65+
// to the specified agent.
66+
//
67+
// TODO: after wsconncache is deleted this doesn't need to return an error.
68+
ReverseProxy(targetURL, dashboardURL *url.URL, agentID uuid.UUID) (_ *httputil.ReverseProxy, release func(), _ error)
69+
70+
// AgentConn returns a new connection to the specified agent.
71+
//
6472
// TODO: after wsconncache is deleted this doesn't need to return a release
6573
// func.
6674
AgentConn(ctx context.Context, agentID uuid.UUID) (_ *codersdk.WorkspaceAgentConn, release func(), _ error)
67-
// TODO: after wsconncache is deleted this doesn't need to return an error.
68-
ReverseProxy(targetURL, dashboardURL *url.URL, agentID uuid.UUID) (_ *httputil.ReverseProxy, release func(), _ error)
75+
6976
Close() error
7077
}
7178

go.mod

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,6 @@ require (
218218
github.com/docker/docker v23.0.3+incompatible // indirect
219219
github.com/docker/go-connections v0.4.0 // indirect
220220
github.com/docker/go-units v0.5.0 // indirect
221-
github.com/dustin/go-humanize v1.0.1
222221
github.com/elastic/go-windows v1.0.0 // indirect
223222
github.com/fxamacker/cbor/v2 v2.4.0 // indirect
224223
github.com/gabriel-vasile/mimetype v1.4.2 // indirect

go.sum

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,6 @@ github.com/docker/go-units v0.4.0/go.mod h1:fgPhTUdO+D/Jk86RDLlptpiXQzgHJF7gydDD
238238
github.com/docker/go-units v0.5.0 h1:69rxXcBk27SvSaaxTtLh/8llcHD8vYHT7WSdRZ/jvr4=
239239
github.com/docker/go-units v0.5.0/go.mod h1:fgPhTUdO+D/Jk86RDLlptpiXQzgHJF7gydDDbaIK4Dk=
240240
github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY=
241-
github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto=
242241
github.com/elastic/go-sysinfo v1.11.0 h1:QW+6BF1oxBoAprH3w2yephF7xLkrrSXj7gl2xC2BM4w=
243242
github.com/elastic/go-sysinfo v1.11.0/go.mod h1:6KQb31j0QeWBDF88jIdWSxE8cwoOB9tO4Y4osN7Q70E=
244243
github.com/elastic/go-windows v1.0.0 h1:qLURgZFkkrYyTTkvYpsZIgf83AUsdIHfvlJaqaZ7aSY=

0 commit comments

Comments
 (0)