From 89c18ad45cd2d83e60c08786cfea0e1e200a206d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maximilian=20Pa=C3=9F?= <22845248+mpass99@users.noreply.github.com> Date: Wed, 16 Aug 2023 12:18:02 +0200 Subject: [PATCH] Refactor to WithoutCancel context. With Go 1.21 the WithoutCancel context was introduced. This way we can keep the values passed in a new context without having the new context being canceled together with its parent. This behavior suits well for two occurrences where we explicitly had to copy one required value instead of implicitly keeping all values. --- internal/api/websocket.go | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/internal/api/websocket.go b/internal/api/websocket.go index 5c31b5c..e173e86 100644 --- a/internal/api/websocket.go +++ b/internal/api/websocket.go @@ -4,7 +4,6 @@ import ( "context" "errors" "fmt" - "github.com/getsentry/sentry-go" "github.com/gorilla/websocket" "github.com/openHPI/poseidon/internal/api/ws" "github.com/openHPI/poseidon/internal/runner" @@ -37,9 +36,9 @@ func upgradeConnection(writer http.ResponseWriter, request *http.Request) (ws.Co // As this proxy is already started, a start message is send to the client. func newWebSocketProxy(connection ws.Connection, proxyCtx context.Context) *webSocketProxy { // wsCtx is detached from the proxyCtx - // as it should send all messages in the buffer even if the execution/proxy is done. - wsCtx, cancelWsCommunication := context.WithCancel(context.Background()) - wsCtx = sentry.SetHubOnContext(wsCtx, sentry.GetHubFromContext(proxyCtx)) + // as it should send all messages in the buffer even shortly after the execution/proxy is done. + wsCtx := context.WithoutCancel(proxyCtx) + wsCtx, cancelWsCommunication := context.WithCancel(wsCtx) proxy := &webSocketProxy{ ctx: wsCtx, @@ -92,9 +91,9 @@ func (r *RunnerController) connectToRunner(writer http.ResponseWriter, request * return } - // ToDo: Why can we not inherit from request.Context() here? - proxyCtx, cancelProxy := context.WithCancel(context.Background()) - proxyCtx = sentry.SetHubOnContext(proxyCtx, sentry.GetHubFromContext(request.Context())) + // We do not inherit from the request.Context() here because we rely on the WebSocket Close Handler. + proxyCtx := context.WithoutCancel(request.Context()) + proxyCtx, cancelProxy := context.WithCancel(proxyCtx) defer cancelProxy() proxy := newWebSocketProxy(connection, proxyCtx)