From 3efb25667dc3508ce29e057977ae01aa858925d1 Mon Sep 17 00:00:00 2001 From: Ben Schumacher Date: Thu, 4 Apr 2024 10:25:16 +0200 Subject: [PATCH] [MM-57298] Fix context cancellation for integration requests (#26516) --- server/channels/app/integration_action.go | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/server/channels/app/integration_action.go b/server/channels/app/integration_action.go index 15847dee26..f840cd9824 100644 --- a/server/channels/app/integration_action.go +++ b/server/channels/app/integration_action.go @@ -232,7 +232,10 @@ func (a *App) DoPostActionWithCookie(c request.CTX, postID, actionId, userID, se if err != nil { return "", model.NewAppError("DoPostActionWithCookie", "api.marshal_error", nil, "", http.StatusInternalServerError).Wrap(err) } - resp, appErr := a.DoActionRequest(c, upstreamURL, requestJSON) + + ctx, cancel := context.WithTimeout(context.Background(), time.Duration(*a.Config().ServiceSettings.OutgoingIntegrationRequestsTimeout)*time.Second) + defer cancel() + resp, appErr := a.DoActionRequest(c.WithContext(ctx), upstreamURL, requestJSON) if appErr != nil { return "", appErr } @@ -307,10 +310,7 @@ func (a *App) DoActionRequest(c request.CTX, rawURL string, body []byte) (*http. return a.DoLocalRequest(c, rawURLPath, body) } - ctx, cancel := context.WithTimeout(context.Background(), time.Duration(*a.Config().ServiceSettings.OutgoingIntegrationRequestsTimeout)*time.Second) - defer cancel() - - req, err := http.NewRequestWithContext(ctx, "POST", rawURL, bytes.NewReader(body)) + req, err := http.NewRequestWithContext(c.Context(), "POST", rawURL, bytes.NewReader(body)) if err != nil { if errors.Is(err, context.DeadlineExceeded) { c.Logger().Info("Outgoing Integration Action request timed out. Consider increasing ServiceSettings.OutgoingIntegrationRequestsTimeout.") @@ -484,7 +484,9 @@ func (a *App) SubmitInteractiveDialog(c request.CTX, request model.SubmitDialogR return nil, model.NewAppError("SubmitInteractiveDialog", "app.submit_interactive_dialog.json_error", nil, "", http.StatusBadRequest).Wrap(err) } - resp, appErr := a.DoActionRequest(c, url, b) + ctx, cancel := context.WithTimeout(context.Background(), time.Duration(*a.Config().ServiceSettings.OutgoingIntegrationRequestsTimeout)*time.Second) + defer cancel() + resp, appErr := a.DoActionRequest(c.WithContext(ctx), url, b) if appErr != nil { return nil, appErr }