Skip to content

Commit 301f6c4

Browse files
jeanp413roboquat
authored andcommitted
Don't try to connect to opened ports
Otherwise it causes an exception to be thrown constantly in the vscode-js-debug extension while debugging causing the extension host to crash after some time
1 parent afe8c7c commit 301f6c4

File tree

4 files changed

+143
-155
lines changed

4 files changed

+143
-155
lines changed

components/supervisor/pkg/ports/ports.go

Lines changed: 26 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,12 @@ import (
2626
"github.com/gitpod-io/gitpod/supervisor/api"
2727
)
2828

29+
var workspaceIPAdress string
30+
31+
func init() {
32+
workspaceIPAdress = defaultRoutableIP()
33+
}
34+
2935
// NewManager creates a new port manager
3036
func NewManager(exposed ExposedPortsInterface, served ServedPortsObserver, config ConfigInterace, tunneled TunneledPortsInterface, slirp SlirpClient, internalPorts ...uint32) *Manager {
3137
state := make(map[uint32]*managedPort)
@@ -159,42 +165,6 @@ func (pm *Manager) Run(ctx context.Context, wg *sync.WaitGroup) {
159165
}()
160166
defer cancel()
161167

162-
// set a health check for exposed port trough the proxies
163-
go func() {
164-
tick := time.NewTicker(2 * time.Second)
165-
defer tick.Stop()
166-
167-
for {
168-
<-tick.C
169-
170-
pm.mu.RLock()
171-
servedGlobal := make(map[uint32]struct{})
172-
for _, p := range pm.served {
173-
if !p.BoundToLocalhost {
174-
servedGlobal[p.Port] = struct{}{}
175-
}
176-
}
177-
pm.mu.RUnlock()
178-
179-
for localPort, proxy := range pm.proxies {
180-
_, openedGlobal := servedGlobal[localPort]
181-
openedLocal := isLocalPortOpen(int(localPort))
182-
if !openedLocal && openedGlobal {
183-
pm.mu.Lock()
184-
delete(pm.proxies, localPort)
185-
pm.mu.Unlock()
186-
187-
err := proxy.Close()
188-
if err != nil {
189-
log.WithError(err).WithField("localPort", localPort).Warn("cannot stop localhost proxy")
190-
} else {
191-
log.WithField("localPort", localPort).Info("localhost proxy has been stopped")
192-
}
193-
}
194-
}
195-
}
196-
}()
197-
198168
go pm.E.Run(ctx)
199169
exposedUpdates, exposedErrors := pm.E.Observe(ctx)
200170
servedUpdates, servedErrors := pm.S.Observe(ctx)
@@ -289,6 +259,12 @@ func (pm *Manager) updateState(ctx context.Context, exposed []ExposedPort, serve
289259
if served != nil {
290260
servedMap := make(map[uint32]ServedPort)
291261
for _, port := range served {
262+
if port.Address.String() == workspaceIPAdress {
263+
// Ignore entries that are bound to the workspace ip address
264+
// as they are created by the reverse proxy
265+
continue
266+
}
267+
292268
current, exists := servedMap[port.Port]
293269
if !exists || (!port.BoundToLocalhost && current.BoundToLocalhost) {
294270
servedMap[port.Port] = port
@@ -575,13 +551,20 @@ func (pm *Manager) updateSlirp() {
575551
}
576552

577553
func (pm *Manager) updateProxies() {
578-
servedLocal := make(map[uint32]struct{})
579-
servedGlobal := make(map[uint32]struct{})
580-
for _, p := range pm.served {
581-
if p.BoundToLocalhost {
582-
servedLocal[p.Port] = struct{}{}
583-
} else {
584-
servedGlobal[p.Port] = struct{}{}
554+
servedPortMap := map[uint32]bool{}
555+
for _, s := range pm.served {
556+
servedPortMap[s.Port] = s.BoundToLocalhost
557+
}
558+
559+
for port, proxy := range pm.proxies {
560+
if boundToLocalhost, exists := servedPortMap[port]; !exists || !boundToLocalhost {
561+
delete(pm.proxies, port)
562+
err := proxy.Close()
563+
if err != nil {
564+
log.WithError(err).WithField("localPort", port).Warn("cannot stop localhost proxy")
565+
} else {
566+
log.WithField("localPort", port).Info("localhost proxy has been stopped")
567+
}
585568
}
586569
}
587570

@@ -801,11 +784,6 @@ func (pm *Manager) getPortStatus(port uint32) *api.PortsStatus {
801784
return ps
802785
}
803786

804-
var workspaceIPAdress string
805-
806-
func init() {
807-
workspaceIPAdress = defaultRoutableIP()
808-
}
809787
func startLocalhostProxy(port uint32) (io.Closer, error) {
810788
host := fmt.Sprintf("localhost:%d", port)
811789

@@ -864,19 +842,3 @@ func defaultRoutableIP() string {
864842

865843
return addresses[0].(*net.IPNet).IP.String()
866844
}
867-
868-
func isLocalPortOpen(port int) bool {
869-
timeout := 1 * time.Second
870-
target := fmt.Sprintf("127.0.0.1:%d", port)
871-
conn, err := net.DialTimeout("tcp", target, timeout)
872-
if err != nil {
873-
return false
874-
}
875-
876-
if conn != nil {
877-
conn.Close()
878-
return true
879-
}
880-
881-
return false
882-
}

components/supervisor/pkg/ports/ports_test.go

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,10 @@ func TestPortsUpdateState(t *testing.T) {
4949
{
5050
Desc: "basic locally served",
5151
Changes: []Change{
52-
{Served: []ServedPort{{"0100007F", 8080, true}}},
52+
{Served: []ServedPort{{net.IPv4(127, 0, 0, 1), 8080, true}}},
5353
{Exposed: []ExposedPort{{LocalPort: 8080, URL: "foobar"}}},
54-
{Served: []ServedPort{{"0100007F", 8080, true}, {"00000000", 60000, false}}},
55-
{Served: []ServedPort{{"00000000", 60000, false}}},
54+
{Served: []ServedPort{{net.IPv4(127, 0, 0, 1), 8080, true}, {net.IPv4zero, 60000, false}}},
55+
{Served: []ServedPort{{net.IPv4zero, 60000, false}}},
5656
{Served: []ServedPort{}},
5757
},
5858
ExpectedExposure: []ExposedPort{
@@ -71,7 +71,7 @@ func TestPortsUpdateState(t *testing.T) {
7171
{
7272
Desc: "basic globally served",
7373
Changes: []Change{
74-
{Served: []ServedPort{{"00000000", 8080, false}}},
74+
{Served: []ServedPort{{net.IPv4zero, 8080, false}}},
7575
{Served: []ServedPort{}},
7676
},
7777
ExpectedExposure: []ExposedPort{
@@ -102,7 +102,7 @@ func TestPortsUpdateState(t *testing.T) {
102102
InternalPorts: []uint32{8080},
103103
Changes: []Change{
104104
{Served: []ServedPort{}},
105-
{Served: []ServedPort{{"00000000", 8080, false}}},
105+
{Served: []ServedPort{{net.IPv4zero, 8080, false}}},
106106
},
107107

108108
ExpectedExposure: ExposureExpectation(nil),
@@ -125,8 +125,8 @@ func TestPortsUpdateState(t *testing.T) {
125125
},
126126
{
127127
Served: []ServedPort{
128-
{"00000000", 8080, false},
129-
{"0100007F", 9229, true},
128+
{net.IPv4zero, 8080, false},
129+
{net.IPv4(127, 0, 0, 1), 9229, true},
130130
},
131131
},
132132
},
@@ -156,9 +156,9 @@ func TestPortsUpdateState(t *testing.T) {
156156
Port: "4000-5000",
157157
}},
158158
}},
159-
{Served: []ServedPort{{"0100007F", 4040, true}}},
159+
{Served: []ServedPort{{net.IPv4(127, 0, 0, 1), 4040, true}}},
160160
{Exposed: []ExposedPort{{LocalPort: 4040, Public: true, URL: "4040-foobar"}}},
161-
{Served: []ServedPort{{"0100007F", 4040, true}, {"00000000", 60000, false}}},
161+
{Served: []ServedPort{{net.IPv4(127, 0, 0, 1), 4040, true}, {net.IPv4zero, 60000, false}}},
162162
},
163163
ExpectedExposure: []ExposedPort{
164164
{LocalPort: 4040},
@@ -189,19 +189,19 @@ func TestPortsUpdateState(t *testing.T) {
189189
Exposed: []ExposedPort{{LocalPort: 8080, Public: true, URL: "foobar"}},
190190
},
191191
{
192-
Served: []ServedPort{{"0100007F", 8080, true}},
192+
Served: []ServedPort{{net.IPv4(127, 0, 0, 1), 8080, true}},
193193
},
194194
{
195195
Exposed: []ExposedPort{{LocalPort: 8080, Public: true, URL: "foobar"}},
196196
},
197197
{
198-
Served: []ServedPort{{"0100007F", 8080, true}},
198+
Served: []ServedPort{{net.IPv4(127, 0, 0, 1), 8080, true}},
199199
},
200200
{
201201
Served: []ServedPort{},
202202
},
203203
{
204-
Served: []ServedPort{{"0100007F", 8080, false}},
204+
Served: []ServedPort{{net.IPv4(127, 0, 0, 1), 8080, false}},
205205
},
206206
},
207207
ExpectedExposure: []ExposedPort{
@@ -221,7 +221,7 @@ func TestPortsUpdateState(t *testing.T) {
221221
Desc: "starting multiple proxies for the same served event",
222222
Changes: []Change{
223223
{
224-
Served: []ServedPort{{"0100007F", 8080, true}, {"00000000", 3000, true}},
224+
Served: []ServedPort{{net.IPv4(127, 0, 0, 1), 8080, true}, {net.IPv4zero, 3000, true}},
225225
},
226226
},
227227
ExpectedExposure: []ExposedPort{
@@ -245,7 +245,7 @@ func TestPortsUpdateState(t *testing.T) {
245245
}},
246246
},
247247
{
248-
Served: []ServedPort{{"00000000", 8080, false}},
248+
Served: []ServedPort{{net.IPv4zero, 8080, false}},
249249
},
250250
{
251251
Exposed: []ExposedPort{{LocalPort: 8080, Public: false, URL: "foobar"}},
@@ -265,13 +265,13 @@ func TestPortsUpdateState(t *testing.T) {
265265
Desc: "the same port served locally and then globally too, prefer globally (exposed in between)",
266266
Changes: []Change{
267267
{
268-
Served: []ServedPort{{"0100007F", 5900, true}},
268+
Served: []ServedPort{{net.IPv4(127, 0, 0, 1), 5900, true}},
269269
},
270270
{
271271
Exposed: []ExposedPort{{LocalPort: 5900, URL: "foobar"}},
272272
},
273273
{
274-
Served: []ServedPort{{"0100007F", 5900, true}, {"00000000", 5900, false}},
274+
Served: []ServedPort{{net.IPv4(127, 0, 0, 1), 5900, true}, {net.IPv4zero, 5900, false}},
275275
},
276276
{
277277
Exposed: []ExposedPort{{LocalPort: 5900, URL: "foobar"}},
@@ -290,10 +290,10 @@ func TestPortsUpdateState(t *testing.T) {
290290
Desc: "the same port served locally and then globally too, prefer globally (exposed after)",
291291
Changes: []Change{
292292
{
293-
Served: []ServedPort{{"0100007F", 5900, true}},
293+
Served: []ServedPort{{net.IPv4(127, 0, 0, 1), 5900, true}},
294294
},
295295
{
296-
Served: []ServedPort{{"0100007F", 5900, true}, {"00000000", 5900, false}},
296+
Served: []ServedPort{{net.IPv4(127, 0, 0, 1), 5900, true}, {net.IPv4zero, 5900, false}},
297297
},
298298
{
299299
Exposed: []ExposedPort{{LocalPort: 5900, URL: "foobar"}},
@@ -315,13 +315,13 @@ func TestPortsUpdateState(t *testing.T) {
315315
Desc: "the same port served globally and then locally too, prefer globally (exposed in between)",
316316
Changes: []Change{
317317
{
318-
Served: []ServedPort{{"00000000", 5900, false}},
318+
Served: []ServedPort{{net.IPv4zero, 5900, false}},
319319
},
320320
{
321321
Exposed: []ExposedPort{{LocalPort: 5900, URL: "foobar"}},
322322
},
323323
{
324-
Served: []ServedPort{{"00000000", 5900, false}, {"0100007F", 5900, true}},
324+
Served: []ServedPort{{net.IPv4zero, 5900, false}, {net.IPv4(127, 0, 0, 1), 5900, true}},
325325
},
326326
},
327327
ExpectedExposure: []ExposedPort{
@@ -337,10 +337,10 @@ func TestPortsUpdateState(t *testing.T) {
337337
Desc: "the same port served globally and then locally too, prefer globally (exposed after)",
338338
Changes: []Change{
339339
{
340-
Served: []ServedPort{{"00000000", 5900, false}},
340+
Served: []ServedPort{{net.IPv4zero, 5900, false}},
341341
},
342342
{
343-
Served: []ServedPort{{"00000000", 5900, false}, {"0100007F", 5900, true}},
343+
Served: []ServedPort{{net.IPv4zero, 5900, false}, {net.IPv4(127, 0, 0, 1), 5900, true}},
344344
},
345345
{
346346
Exposed: []ExposedPort{{LocalPort: 5900, URL: "foobar"}},
@@ -359,13 +359,13 @@ func TestPortsUpdateState(t *testing.T) {
359359
Desc: "the same port served locally on ip4 and then locally on ip6 too, prefer first (exposed in between)",
360360
Changes: []Change{
361361
{
362-
Served: []ServedPort{{"0100007F", 5900, true}},
362+
Served: []ServedPort{{net.IPv4(127, 0, 0, 1), 5900, true}},
363363
},
364364
{
365365
Exposed: []ExposedPort{{LocalPort: 5900, URL: "foobar"}},
366366
},
367367
{
368-
Served: []ServedPort{{"0100007F", 5900, true}, {"00000000000000000000010000000000", 5900, true}},
368+
Served: []ServedPort{{net.IPv4(127, 0, 0, 1), 5900, true}, {net.IPv6zero, 5900, true}},
369369
},
370370
},
371371
ExpectedExposure: []ExposedPort{
@@ -381,10 +381,10 @@ func TestPortsUpdateState(t *testing.T) {
381381
Desc: "the same port served locally on ip4 and then locally on ip6 too, prefer first (exposed after)",
382382
Changes: []Change{
383383
{
384-
Served: []ServedPort{{"0100007F", 5900, true}},
384+
Served: []ServedPort{{net.IPv4(127, 0, 0, 1), 5900, true}},
385385
},
386386
{
387-
Served: []ServedPort{{"0100007F", 5900, true}, {"00000000000000000000010000000000", 5900, true}},
387+
Served: []ServedPort{{net.IPv4(127, 0, 0, 1), 5900, true}, {net.IPv6zero, 5900, true}},
388388
},
389389
{
390390
Exposed: []ExposedPort{{LocalPort: 5900, URL: "foobar"}},
@@ -403,13 +403,13 @@ func TestPortsUpdateState(t *testing.T) {
403403
Desc: "the same port served locally on ip4 and then globally on ip6 too, prefer first (exposed in between)",
404404
Changes: []Change{
405405
{
406-
Served: []ServedPort{{"00000000", 5900, false}},
406+
Served: []ServedPort{{net.IPv4zero, 5900, false}},
407407
},
408408
{
409409
Exposed: []ExposedPort{{LocalPort: 5900, URL: "foobar"}},
410410
},
411411
{
412-
Served: []ServedPort{{"00000000", 5900, false}, {"00000000000000000000000000000000", 5900, false}},
412+
Served: []ServedPort{{net.IPv4zero, 5900, false}, {net.IPv6zero, 5900, false}},
413413
},
414414
},
415415
ExpectedExposure: []ExposedPort{
@@ -425,10 +425,10 @@ func TestPortsUpdateState(t *testing.T) {
425425
Desc: "the same port served locally on ip4 and then globally on ip6 too, prefer first (exposed after)",
426426
Changes: []Change{
427427
{
428-
Served: []ServedPort{{"00000000", 5900, false}},
428+
Served: []ServedPort{{net.IPv4zero, 5900, false}},
429429
},
430430
{
431-
Served: []ServedPort{{"00000000", 5900, false}, {"00000000000000000000000000000000", 5900, false}},
431+
Served: []ServedPort{{net.IPv4zero, 5900, false}, {net.IPv6zero, 5900, false}},
432432
},
433433
{
434434
Exposed: []ExposedPort{{LocalPort: 5900, URL: "foobar"}},
@@ -452,7 +452,7 @@ func TestPortsUpdateState(t *testing.T) {
452452
}},
453453
},
454454
{
455-
Served: []ServedPort{{"00000000", 8080, false}},
455+
Served: []ServedPort{{net.IPv4zero, 8080, false}},
456456
},
457457
{
458458
Exposed: []ExposedPort{{LocalPort: 8080, Public: false, URL: "foobar"}},
@@ -477,7 +477,7 @@ func TestPortsUpdateState(t *testing.T) {
477477
}},
478478
},
479479
{
480-
Served: []ServedPort{{"00000000", 3000, false}},
480+
Served: []ServedPort{{net.IPv4zero, 3000, false}},
481481
},
482482
{
483483
Exposed: []ExposedPort{{LocalPort: 3000, Public: false, URL: "foobar"}},

0 commit comments

Comments
 (0)