Skip to content

Commit 8ef825d

Browse files
committed
Revert "Handle IIS OnCompleted callbacks later #17268 (#17756)"
This reverts commit 11ecc62.
1 parent 8cd404d commit 8ef825d

File tree

4 files changed

+60
-75
lines changed

4 files changed

+60
-75
lines changed

src/Servers/IIS/IIS/src/Core/IISHttpContext.cs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -411,7 +411,7 @@ public unsafe void SetResponseHeaders()
411411
}
412412
}
413413

414-
public abstract Task ProcessRequestAsync();
414+
public abstract Task<bool> ProcessRequestAsync();
415415

416416
public void OnStarting(Func<object, Task> callback, object state)
417417
{
@@ -601,19 +601,30 @@ public void Execute()
601601

602602
private async Task HandleRequest()
603603
{
604+
bool successfulRequest = false;
604605
try
605606
{
606-
await ProcessRequestAsync();
607+
successfulRequest = await ProcessRequestAsync();
607608
}
608609
catch (Exception ex)
609610
{
610611
_logger.LogError(0, ex, $"Unexpected exception in {nameof(IISHttpContext)}.{nameof(HandleRequest)}.");
611612
}
612613
finally
613614
{
615+
// Post completion after completing the request to resume the state machine
616+
PostCompletion(ConvertRequestCompletionResults(successfulRequest));
617+
618+
614619
// Dispose the context
615620
Dispose();
616621
}
617622
}
623+
624+
private static NativeMethods.REQUEST_NOTIFICATION_STATUS ConvertRequestCompletionResults(bool success)
625+
{
626+
return success ? NativeMethods.REQUEST_NOTIFICATION_STATUS.RQ_NOTIFICATION_CONTINUE
627+
: NativeMethods.REQUEST_NOTIFICATION_STATUS.RQ_NOTIFICATION_FINISH_REQUEST;
628+
}
618629
}
619630
}

src/Servers/IIS/IIS/src/Core/IISHttpContextOfT.cs

Lines changed: 47 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -23,33 +23,32 @@ public IISHttpContextOfT(MemoryPool<byte> memoryPool, IHttpApplication<TContext>
2323
_application = application;
2424
}
2525

26-
public override async Task ProcessRequestAsync()
26+
public override async Task<bool> ProcessRequestAsync()
2727
{
28+
InitializeContext();
29+
2830
var context = default(TContext);
2931
var success = true;
3032

3133
try
3234
{
33-
InitializeContext();
34-
35-
try
36-
{
37-
context = _application.CreateContext(this);
38-
39-
await _application.ProcessRequestAsync(context);
40-
}
41-
catch (BadHttpRequestException ex)
42-
{
43-
SetBadRequestState(ex);
44-
ReportApplicationError(ex);
45-
success = false;
46-
}
47-
catch (Exception ex)
48-
{
49-
ReportApplicationError(ex);
50-
success = false;
51-
}
35+
context = _application.CreateContext(this);
5236

37+
await _application.ProcessRequestAsync(context);
38+
}
39+
catch (BadHttpRequestException ex)
40+
{
41+
SetBadRequestState(ex);
42+
ReportApplicationError(ex);
43+
success = false;
44+
}
45+
catch (Exception ex)
46+
{
47+
ReportApplicationError(ex);
48+
success = false;
49+
}
50+
finally
51+
{
5352
await CompleteResponseBodyAsync();
5453
_streams.Stop();
5554

@@ -59,18 +58,36 @@ public override async Task ProcessRequestAsync()
5958
// Dispose
6059
}
6160

62-
if (!_requestAborted)
63-
{
64-
await ProduceEnd();
65-
}
66-
else if (!HasResponseStarted && _requestRejectedException == null)
61+
if (_onCompleted != null)
6762
{
68-
// If the request was aborted and no response was sent, there's no
69-
// meaningful status code to log.
70-
StatusCode = 0;
71-
success = false;
63+
await FireOnCompleted();
7264
}
65+
}
66+
67+
if (!_requestAborted)
68+
{
69+
await ProduceEnd();
70+
}
71+
else if (!HasResponseStarted && _requestRejectedException == null)
72+
{
73+
// If the request was aborted and no response was sent, there's no
74+
// meaningful status code to log.
75+
StatusCode = 0;
76+
success = false;
77+
}
7378

79+
try
80+
{
81+
_application.DisposeContext(context, _applicationException);
82+
}
83+
catch (Exception ex)
84+
{
85+
// TODO Log this
86+
_applicationException = _applicationException ?? ex;
87+
success = false;
88+
}
89+
finally
90+
{
7491
// Complete response writer and request reader pipe sides
7592
_bodyOutput.Dispose();
7693
_bodyInputPipe?.Reader.Complete();
@@ -89,36 +106,7 @@ public override async Task ProcessRequestAsync()
89106
await _readBodyTask;
90107
}
91108
}
92-
catch (Exception ex)
93-
{
94-
success = false;
95-
ReportApplicationError(ex);
96-
}
97-
finally
98-
{
99-
// We're done with anything that touches the request or response, unblock the client.
100-
PostCompletion(ConvertRequestCompletionResults(success));
101-
102-
if (_onCompleted != null)
103-
{
104-
await FireOnCompleted();
105-
}
106-
107-
try
108-
{
109-
_application.DisposeContext(context, _applicationException);
110-
}
111-
catch (Exception ex)
112-
{
113-
ReportApplicationError(ex);
114-
}
115-
}
116-
}
117-
118-
private static NativeMethods.REQUEST_NOTIFICATION_STATUS ConvertRequestCompletionResults(bool success)
119-
{
120-
return success ? NativeMethods.REQUEST_NOTIFICATION_STATUS.RQ_NOTIFICATION_CONTINUE
121-
: NativeMethods.REQUEST_NOTIFICATION_STATUS.RQ_NOTIFICATION_FINISH_REQUEST;
109+
return success;
122110
}
123111
}
124112
}

src/Servers/IIS/IIS/test/Common.FunctionalTests/Inprocess/ResponseBodyTests.cs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,5 @@ public async Task ResponseBodyTest_FlushedPipeAndThenUnflushedPipe_AutoFlushed()
3030
{
3131
Assert.Equal(20, (await _fixture.Client.GetByteArrayAsync($"/FlushedPipeAndThenUnflushedPipe")).Length);
3232
}
33-
34-
[ConditionalFact]
35-
[RequiresNewHandler]
36-
public async Task ResponseBodyTest_BodyCompletionNotBlockedByOnCompleted()
37-
{
38-
Assert.Equal("SlowOnCompleted", await _fixture.Client.GetStringAsync($"/SlowOnCompleted"));
39-
}
4033
}
4134
}

src/Servers/IIS/IIS/test/testassets/InProcessWebSite/Startup.cs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1016,12 +1016,5 @@ public async Task HTTPS_PORT(HttpContext context)
10161016

10171017
await context.Response.WriteAsync(httpsPort.HasValue ? httpsPort.Value.ToString() : "NOVALUE");
10181018
}
1019-
1020-
public async Task SlowOnCompleted(HttpContext context)
1021-
{
1022-
// This shouldn't block the response or the server from shutting down.
1023-
context.Response.OnCompleted(() => Task.Delay(TimeSpan.FromMinutes(5)));
1024-
await context.Response.WriteAsync("SlowOnCompleted");
1025-
}
10261019
}
10271020
}

0 commit comments

Comments
 (0)