Skip to content

Commit 82146cd

Browse files
committed
fix: memory leak on account of pending dials
Due to whatever reason if the endpoint is not respondind and the server isn't able to establish a tunnelm the pending dials keep accumulating and never get cleared. Signed-off-by: Imran Pochi <[email protected]>
1 parent dac431f commit 82146cd

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed

pkg/server/tunnel.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,8 @@ func (t *Tunnel) ServeHTTP(w http.ResponseWriter, r *http.Request) {
102102
t.Server.PendingDial.Add(random, connection)
103103
if err := backend.Send(dialRequest); err != nil {
104104
klog.ErrorS(err, "failed to tunnel dial request", "host", r.Host, "dialID", connection.dialID, "agentID", connection.agentID)
105+
t.Server.PendingDial.Remove(random)
106+
metrics.Metrics.ObserveDialFailure(metrics.DialFailureBackendClose)
105107
// Send proper HTTP error response
106108
conn.Write([]byte(fmt.Sprintf("HTTP/1.1 502 Bad Gateway\r\nContent-Type: text/plain; charset=utf-8\r\n\r\nFailed to tunnel dial request: %v\r\n", err)))
107109
conn.Close()
@@ -110,6 +112,9 @@ func (t *Tunnel) ServeHTTP(w http.ResponseWriter, r *http.Request) {
110112
ctxt := backend.Context()
111113
if ctxt.Err() != nil {
112114
klog.ErrorS(ctxt.Err(), "context reports failure")
115+
t.Server.PendingDial.Remove(random)
116+
metrics.Metrics.ObserveDialFailure(metrics.DialFailureBackendClose)
117+
// Send proper HTTP error response
113118
conn.Write([]byte(fmt.Sprintf("HTTP/1.1 502 Bad Gateway\r\nContent-Type: text/plain; charset=utf-8\r\n\r\nBackend context error: %v\r\n", ctxt.Err())))
114119
conn.Close()
115120
return
@@ -133,6 +138,10 @@ func (t *Tunnel) ServeHTTP(w http.ResponseWriter, r *http.Request) {
133138
klog.V(3).InfoS("Connection established, sent 200 OK", "host", r.Host, "agentID", connection.agentID, "connectionID", connection.connectID)
134139

135140
case <-closed: // Connection was closed before being established
141+
if t.Server.PendingDial.Remove(random) != nil {
142+
metrics.Metrics.ObserveDialFailure(metrics.DialFailureFrontendClose)
143+
}
144+
return
136145
}
137146

138147
defer func() {

0 commit comments

Comments
 (0)