diff --git a/federationapi/routing/send.go b/federationapi/routing/send.go index 405d7b864..dbfd3ff92 100644 --- a/federationapi/routing/send.go +++ b/federationapi/routing/send.go @@ -164,7 +164,7 @@ func Send( util.GetLogger(httpReq.Context()).Infof("Received transaction %q from %q containing %d PDUs, %d EDUs", txnID, request.Origin(), len(t.PDUs), len(t.EDUs)) - resp, jsonErr := t.processTransaction(context.Background()) + resp, jsonErr := t.processTransaction(httpReq.Context()) if jsonErr != nil { util.GetLogger(httpReq.Context()).WithField("jsonErr", jsonErr).Error("t.processTransaction failed") return *jsonErr @@ -276,7 +276,7 @@ func (t *txnReq) processTransaction(ctx context.Context) (*gomatrixserverlib.Res // If the event fail auth checks, gmsl.NotAllowed error will be returned which we be silently // discarded by the caller of this function if err = api.SendEvents( - context.Background(), + ctx, t.rsAPI, api.KindNew, []*gomatrixserverlib.HeaderedEvent{ diff --git a/roomserver/internal/input/input_events.go b/roomserver/internal/input/input_events.go index 59ce99691..5f9115223 100644 --- a/roomserver/internal/input/input_events.go +++ b/roomserver/internal/input/input_events.go @@ -65,24 +65,25 @@ var processRoomEventDuration = prometheus.NewHistogramVec( // TODO: Break up function - we should probably do transaction ID checks before calling this. // nolint:gocyclo func (r *Inputer) processRoomEvent( - ctx context.Context, + inctx context.Context, input *api.InputRoomEvent, ) (err error) { select { - case <-ctx.Done(): + case <-inctx.Done(): // Before we do anything, make sure the context hasn't expired for this pending task. // If it has then we'll give up straight away — it's probably a synchronous input // request and the caller has already given up, but the inbox task was still queued. return context.DeadlineExceeded default: - // Otherwise we're going to wrap the context with a time limit. We'll allow no more - // than MaximumProcessingTime for everything that we need to do for this event, or - // it's possible that we could end up wedging the roomserver for a very long time. - var cancel context.CancelFunc - ctx, cancel = context.WithTimeout(ctx, MaximumProcessingTime) - defer cancel() } + // Wrap the context with a time limit. We'll allow no more than MaximumProcessingTime for + // everything that we need to do for this event, or it's possible that we could end up wedging + // the roomserver for a very long time. + var cancel context.CancelFunc + ctx, cancel := context.WithTimeout(inctx, MaximumProcessingTime) + defer cancel() + // Measure how long it takes to process this event. started := time.Now() defer func() { diff --git a/roomserver/internal/input/input_missing.go b/roomserver/internal/input/input_missing.go index 7eed98c8f..5296d9213 100644 --- a/roomserver/internal/input/input_missing.go +++ b/roomserver/internal/input/input_missing.go @@ -394,9 +394,7 @@ func (t *missingStateReq) getMissingEvents(ctx context.Context, e *gomatrixserve var missingResp *gomatrixserverlib.RespMissingEvents for server := range t.servers { var m gomatrixserverlib.RespMissingEvents - reqctx, cancel := context.WithTimeout(ctx, time.Second*30) - defer cancel() - if m, err = t.federation.LookupMissingEvents(reqctx, server, e.RoomID(), gomatrixserverlib.MissingEvents{ + if m, err = t.federation.LookupMissingEvents(ctx, server, e.RoomID(), gomatrixserverlib.MissingEvents{ Limit: 20, // The latest event IDs that the sender already has. These are skipped when retrieving the previous events of latest_events. EarliestEvents: latestEvents, @@ -409,10 +407,10 @@ func (t *missingStateReq) getMissingEvents(ctx context.Context, e *gomatrixserve logger.WithError(err).Errorf("%s pushed us an event but %q did not respond to /get_missing_events", t.origin, server) if errors.Is(err, context.DeadlineExceeded) { select { - case <-reqctx.Done(): // this server took too long - continue - case <-ctx.Done(): // the input request timed out + case <-ctx.Done(): // the parent request context timed out return nil, context.DeadlineExceeded + default: // this request exceed its own timeout + continue } } }