Skip to content

Commit 98dd7f8

Browse files
[release/8.0-staging] [WinHttpHandler] Move _cachedSendPinnedBuffer ownership to WinHttpRequestState (#102083)
* move _cachedSendPinnedBuffer ownership to WinHttpRequestState * set GeneratePackageOnBuild & ServicingVersion
1 parent bb432d7 commit 98dd7f8

File tree

3 files changed

+25
-22
lines changed

3 files changed

+25
-22
lines changed

src/libraries/System.Net.Http.WinHttpHandler/src/System.Net.Http.WinHttpHandler.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
<IncludeDllSafeSearchPathAttribute>true</IncludeDllSafeSearchPathAttribute>
55
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
66
<IsPackable>true</IsPackable>
7+
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
8+
<ServicingVersion>1</ServicingVersion>
79
<PackageDescription>Provides a message handler for HttpClient based on the WinHTTP interface of Windows. While similar to HttpClientHandler, it provides developers more granular control over the application's HTTP communication than the HttpClientHandler.
810

911
Commonly Used Types:

src/libraries/System.Net.Http.WinHttpHandler/src/System/Net/Http/WinHttpRequestState.cs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ public WinHttpTransportContext TransportContext
157157
public long CurrentBytesRead { get; set; }
158158

159159
private GCHandle _cachedReceivePinnedBuffer;
160+
private GCHandle _cachedSendPinnedBuffer;
160161

161162
public void PinReceiveBuffer(byte[] buffer)
162163
{
@@ -171,6 +172,19 @@ public void PinReceiveBuffer(byte[] buffer)
171172
}
172173
}
173174

175+
public void PinSendBuffer(byte[] buffer)
176+
{
177+
if (!_cachedSendPinnedBuffer.IsAllocated || _cachedSendPinnedBuffer.Target != buffer)
178+
{
179+
if (_cachedSendPinnedBuffer.IsAllocated)
180+
{
181+
_cachedSendPinnedBuffer.Free();
182+
}
183+
184+
_cachedSendPinnedBuffer = GCHandle.Alloc(buffer, GCHandleType.Pinned);
185+
}
186+
}
187+
174188
#region IDisposable Members
175189
private void Dispose(bool disposing)
176190
{
@@ -193,12 +207,18 @@ private void Dispose(bool disposing)
193207
{
194208
// This method only gets called when the WinHTTP request handle is fully closed and thus all
195209
// async operations are done. So, it is safe at this point to unpin the buffers and release
196-
// the strong GCHandle for this object.
210+
// the strong GCHandle for the pinned buffers.
197211
if (_cachedReceivePinnedBuffer.IsAllocated)
198212
{
199213
_cachedReceivePinnedBuffer.Free();
200214
_cachedReceivePinnedBuffer = default(GCHandle);
201215
}
216+
217+
if (_cachedSendPinnedBuffer.IsAllocated)
218+
{
219+
_cachedSendPinnedBuffer.Free();
220+
_cachedSendPinnedBuffer = default(GCHandle);
221+
}
202222
#if DEBUG
203223
Interlocked.Increment(ref s_dbg_operationHandleFree);
204224
#endif

src/libraries/System.Net.Http.WinHttpHandler/src/System/Net/Http/WinHttpRequestStream.cs

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@ internal sealed class WinHttpRequestStream : Stream
2424
private readonly SafeWinHttpHandle _requestHandle;
2525
private readonly WinHttpChunkMode _chunkedMode;
2626

27-
private GCHandle _cachedSendPinnedBuffer;
28-
2927
internal WinHttpRequestStream(WinHttpRequestState state, WinHttpChunkMode chunkedMode)
3028
{
3129
_state = state;
@@ -182,15 +180,7 @@ internal async Task EndUploadAsync(CancellationToken token)
182180

183181
protected override void Dispose(bool disposing)
184182
{
185-
if (!_disposed)
186-
{
187-
_disposed = true;
188-
if (_cachedSendPinnedBuffer.IsAllocated)
189-
{
190-
_cachedSendPinnedBuffer.Free();
191-
}
192-
}
193-
183+
_disposed = true;
194184
base.Dispose(disposing);
195185
}
196186

@@ -234,16 +224,7 @@ private Task<bool> InternalWriteDataAsync(byte[] buffer, int offset, int count,
234224
{
235225
Debug.Assert(count > 0);
236226

237-
if (!_cachedSendPinnedBuffer.IsAllocated || _cachedSendPinnedBuffer.Target != buffer)
238-
{
239-
if (_cachedSendPinnedBuffer.IsAllocated)
240-
{
241-
_cachedSendPinnedBuffer.Free();
242-
}
243-
244-
_cachedSendPinnedBuffer = GCHandle.Alloc(buffer, GCHandleType.Pinned);
245-
}
246-
227+
_state.PinSendBuffer(buffer);
247228
_state.TcsInternalWriteDataToRequestStream =
248229
new TaskCompletionSource<bool>(TaskCreationOptions.RunContinuationsAsynchronously);
249230

0 commit comments

Comments
 (0)