@@ -465,7 +465,29 @@ void BaseTracer::adjustSpanTime(CompleteSpan& span) {
465465 // missing topLevelInvocationSpanContext right after).
466466 if (weakIoContext != kj::none) {
467467 auto & weakIoCtx = KJ_ASSERT_NONNULL (weakIoContext);
468- weakIoCtx->runIfAlive ([&span](IoContext& context) { span.endTime = context.now (); });
468+ weakIoCtx->runIfAlive ([this , &span](IoContext& context) {
469+ if (context.hasCurrentIncomingRequest ()) {
470+ span.endTime = context.now ();
471+ } else {
472+ // We have an IOContext, but there's no current IncomingRequest. Always log a warning here,
473+ // this should not be happening. Still report completeTime as a useful timestamp if
474+ // available.
475+ bool hasCompleteTime = false ;
476+ if (completeTime != kj::UNIX_EPOCH) {
477+ span.endTime = completeTime;
478+ hasCompleteTime = true ;
479+ } else {
480+ span.endTime = span.startTime ;
481+ }
482+ if (isPredictableModeForTest ()) {
483+ KJ_FAIL_ASSERT (
484+ " reported span without current request" , span.operationName , hasCompleteTime);
485+ } else {
486+ KJ_LOG (WARNING, " reported span without current request" , span.operationName ,
487+ hasCompleteTime);
488+ }
489+ }
490+ });
469491 if (!weakIoCtx->isValid ()) {
470492 // This can happen if we start a customEvent from this event and cancel it after this IoContext
471493 // gets destroyed. In that case we no longer have an IoContext available and can't get the
0 commit comments