Skip to content

Commit 62e6d93

Browse files
author
msftbot[bot]
authored
Merge pull request #22833 from dotnet-maestro-bot/merge/release/3.1-to-master
[automated] Merge branch 'release/3.1' => 'master'
2 parents 73f04ee + 812f2f8 commit 62e6d93

File tree

3 files changed

+62
-53
lines changed

3 files changed

+62
-53
lines changed

src/Servers/IIS/AspNetCoreModuleV2/InProcessRequestHandler/inprocesshandler.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ IN_PROCESS_HANDLER::NotifyDisconnect()
112112

113113
if (pManagedHttpContext != nullptr)
114114
{
115-
m_pDisconnectHandler(m_pManagedHttpContext);
115+
m_pDisconnectHandler(pManagedHttpContext);
116116
}
117117
}
118118

@@ -139,14 +139,17 @@ IN_PROCESS_HANDLER::SetManagedHttpContext(
139139
PVOID pManagedHttpContext
140140
)
141141
{
142+
bool disconnectFired = false;
143+
142144
{
143145
SRWExclusiveLock lock(m_srwDisconnectLock);
144146
m_pManagedHttpContext = pManagedHttpContext;
147+
disconnectFired = m_disconnectFired;
145148
}
146149

147-
if (m_disconnectFired && m_pManagedHttpContext != nullptr)
150+
if (disconnectFired && pManagedHttpContext != nullptr)
148151
{
149-
m_pDisconnectHandler(m_pManagedHttpContext);
152+
m_pDisconnectHandler(pManagedHttpContext);
150153
}
151154
}
152155

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

Lines changed: 54 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -121,69 +121,75 @@ internal unsafe IISHttpContext(
121121

122122
protected void InitializeContext()
123123
{
124-
_thisHandle = GCHandle.Alloc(this);
124+
// create a memory barrier between initialize and disconnect to prevent a possible
125+
// NullRef with disconnect being called before these fields have been written
126+
// disconnect aquires this lock as well
127+
lock (_abortLock)
128+
{
129+
_thisHandle = GCHandle.Alloc(this);
125130

126-
Method = GetVerb();
131+
Method = GetVerb();
127132

128-
RawTarget = GetRawUrl();
129-
// TODO version is slow.
130-
HttpVersion = GetVersion();
131-
Scheme = SslStatus != SslStatus.Insecure ? Constants.HttpsScheme : Constants.HttpScheme;
132-
KnownMethod = VerbId;
133-
StatusCode = 200;
133+
RawTarget = GetRawUrl();
134+
// TODO version is slow.
135+
HttpVersion = GetVersion();
136+
Scheme = SslStatus != SslStatus.Insecure ? Constants.HttpsScheme : Constants.HttpScheme;
137+
KnownMethod = VerbId;
138+
StatusCode = 200;
134139

135-
var originalPath = GetOriginalPath();
140+
var originalPath = GetOriginalPath();
136141

137-
if (KnownMethod == HttpApiTypes.HTTP_VERB.HttpVerbOPTIONS && string.Equals(RawTarget, "*", StringComparison.Ordinal))
138-
{
139-
PathBase = string.Empty;
140-
Path = string.Empty;
141-
}
142-
else
143-
{
144-
// Path and pathbase are unescaped by RequestUriBuilder
145-
// The UsePathBase middleware will modify the pathbase and path correctly
146-
PathBase = string.Empty;
147-
Path = originalPath;
148-
}
142+
if (KnownMethod == HttpApiTypes.HTTP_VERB.HttpVerbOPTIONS && string.Equals(RawTarget, "*", StringComparison.Ordinal))
143+
{
144+
PathBase = string.Empty;
145+
Path = string.Empty;
146+
}
147+
else
148+
{
149+
// Path and pathbase are unescaped by RequestUriBuilder
150+
// The UsePathBase middleware will modify the pathbase and path correctly
151+
PathBase = string.Empty;
152+
Path = originalPath;
153+
}
149154

150-
var cookedUrl = GetCookedUrl();
151-
QueryString = cookedUrl.GetQueryString() ?? string.Empty;
155+
var cookedUrl = GetCookedUrl();
156+
QueryString = cookedUrl.GetQueryString() ?? string.Empty;
152157

153-
RequestHeaders = new RequestHeaders(this);
154-
HttpResponseHeaders = new HeaderCollection();
155-
ResponseHeaders = HttpResponseHeaders;
158+
RequestHeaders = new RequestHeaders(this);
159+
HttpResponseHeaders = new HeaderCollection();
160+
ResponseHeaders = HttpResponseHeaders;
156161

157-
if (_options.ForwardWindowsAuthentication)
158-
{
159-
WindowsUser = GetWindowsPrincipal();
160-
if (_options.AutomaticAuthentication)
162+
if (_options.ForwardWindowsAuthentication)
161163
{
162-
User = WindowsUser;
164+
WindowsUser = GetWindowsPrincipal();
165+
if (_options.AutomaticAuthentication)
166+
{
167+
User = WindowsUser;
168+
}
163169
}
164-
}
165170

166-
MaxRequestBodySize = _options.MaxRequestBodySize;
171+
MaxRequestBodySize = _options.MaxRequestBodySize;
167172

168-
ResetFeatureCollection();
173+
ResetFeatureCollection();
169174

170-
if (!_server.IsWebSocketAvailable(_pInProcessHandler))
171-
{
172-
_currentIHttpUpgradeFeature = null;
173-
}
175+
if (!_server.IsWebSocketAvailable(_pInProcessHandler))
176+
{
177+
_currentIHttpUpgradeFeature = null;
178+
}
174179

175-
_streams = new Streams(this);
180+
_streams = new Streams(this);
176181

177-
(RequestBody, ResponseBody) = _streams.Start();
182+
(RequestBody, ResponseBody) = _streams.Start();
178183

179-
var pipe = new Pipe(
180-
new PipeOptions(
181-
_memoryPool,
182-
readerScheduler: PipeScheduler.ThreadPool,
183-
pauseWriterThreshold: PauseWriterThreshold,
184-
resumeWriterThreshold: ResumeWriterTheshold,
185-
minimumSegmentSize: MinAllocBufferSize));
186-
_bodyOutput = new OutputProducer(pipe);
184+
var pipe = new Pipe(
185+
new PipeOptions(
186+
_memoryPool,
187+
readerScheduler: PipeScheduler.ThreadPool,
188+
pauseWriterThreshold: PauseWriterThreshold,
189+
resumeWriterThreshold: ResumeWriterTheshold,
190+
minimumSegmentSize: MinAllocBufferSize));
191+
_bodyOutput = new OutputProducer(pipe);
192+
}
187193

188194
NativeMethods.HttpSetManagedContext(_pInProcessHandler, (IntPtr)_thisHandle);
189195
}

src/Servers/IIS/IIS/src/Core/IISHttpServer.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ private static void OnDisconnect(IntPtr pvManagedHttpContext)
170170
try
171171
{
172172
context = (IISHttpContext)GCHandle.FromIntPtr(pvManagedHttpContext).Target;
173-
context.AbortIO(clientDisconnect: true);
173+
context?.AbortIO(clientDisconnect: true);
174174
}
175175
catch (Exception ex)
176176
{
@@ -184,7 +184,7 @@ private static NativeMethods.REQUEST_NOTIFICATION_STATUS OnAsyncCompletion(IntPt
184184
try
185185
{
186186
context = (IISHttpContext)GCHandle.FromIntPtr(pvManagedHttpContext).Target;
187-
context.OnAsyncCompletion(hr, bytes);
187+
context?.OnAsyncCompletion(hr, bytes);
188188
return NativeMethods.REQUEST_NOTIFICATION_STATUS.RQ_NOTIFICATION_PENDING;
189189
}
190190
catch (Exception ex)

0 commit comments

Comments
 (0)