From 396b51ed1f68d977c712e0f9c62dd39d284eddc9 Mon Sep 17 00:00:00 2001 From: Christian Weichel Date: Tue, 19 Jan 2021 09:25:02 +0000 Subject: [PATCH] [ws-proxy] Don't leak workspace info Go routines --- components/ws-proxy/pkg/proxy/infoprovider.go | 9 ++++++++- components/ws-proxy/pkg/proxy/routes.go | 5 +---- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/components/ws-proxy/pkg/proxy/infoprovider.go b/components/ws-proxy/pkg/proxy/infoprovider.go index e755b86e20..a1e81e54d0 100644 --- a/components/ws-proxy/pkg/proxy/infoprovider.go +++ b/components/ws-proxy/pkg/proxy/infoprovider.go @@ -275,8 +275,15 @@ func mapWorkspaceStatusToInfo(status *wsapi.WorkspaceStatus) *WorkspaceInfo { } } -// WorkspaceInfo return the WorkspaceInfo avaiable for the given workspaceID +// WorkspaceInfo return the WorkspaceInfo avaiable for the given workspaceID. +// Callers should make sure their context gets canceled properly. For good measure +// this function will timeout by itself as well. func (p *RemoteWorkspaceInfoProvider) WorkspaceInfo(ctx context.Context, workspaceID string) *WorkspaceInfo { + // In case the parent context does not cancel for some reason, we want to make sure + // we clean up after ourselves to not leak Go routines. + ctx, cancel := context.WithTimeout(ctx, 5*time.Second) + defer cancel() + info, present := p.cache.WaitFor(ctx, workspaceID) if !present { return nil diff --git a/components/ws-proxy/pkg/proxy/routes.go b/components/ws-proxy/pkg/proxy/routes.go index af2e41d98d..ec80d6f003 100644 --- a/components/ws-proxy/pkg/proxy/routes.go +++ b/components/ws-proxy/pkg/proxy/routes.go @@ -482,10 +482,7 @@ func workspaceMustExistHandler(config *Config, infoProvider WorkspaceInfoProvide return func(h http.Handler) http.Handler { return http.HandlerFunc(func(resp http.ResponseWriter, req *http.Request) { coords := getWorkspaceCoords(req) - // it might take some time until the event comes in. Let's wait for it at most 3 secs. - ctx, cancel := context.WithTimeout(req.Context(), 3*time.Second) - defer cancel() - info := infoProvider.WorkspaceInfo(ctx, coords.ID) + info := infoProvider.WorkspaceInfo(req.Context(), coords.ID) if info == nil { log.WithFields(log.OWI("", coords.ID, "")).Info("no workspace info found - redirecting to start") redirectURL := fmt.Sprintf("%s://%s/start/#%s", config.GitpodInstallation.Scheme, config.GitpodInstallation.HostName, coords.ID)