Skip to content

Commit 40383cf

Browse files
committed
Merged PR 9189: Modify WebSockets ValueTaskSource
Create the GCHandle per operation and Free it when resetting the ValueTaskSource for re-use.
1 parent c9f75a1 commit 40383cf

File tree

3 files changed

+9
-5
lines changed

3 files changed

+9
-5
lines changed

src/Servers/IIS/IIS/src/Core/IO/WebSocketsAsyncIOEngine.Read.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,19 +25,19 @@ internal class WebSocketReadOperation : AsyncIOOperation
2525
};
2626

2727
private readonly WebSocketsAsyncIOEngine _engine;
28-
private readonly GCHandle _thisHandle;
28+
private GCHandle _thisHandle;
2929
private MemoryHandle _inputHandle;
3030
private IntPtr _requestHandler;
3131
private Memory<byte> _memory;
3232

3333
public WebSocketReadOperation(WebSocketsAsyncIOEngine engine)
3434
{
3535
_engine = engine;
36-
_thisHandle = GCHandle.Alloc(this);
3736
}
3837

3938
protected override unsafe bool InvokeOperation(out int hr, out int bytes)
4039
{
40+
_thisHandle = GCHandle.Alloc(this);
4141
_inputHandle = _memory.Pin();
4242

4343
hr = NativeMethods.HttpWebsocketsReadBytes(
@@ -67,6 +67,8 @@ protected override void ResetOperation()
6767
{
6868
base.ResetOperation();
6969

70+
_thisHandle.Free();
71+
7072
_memory = default;
7173
_inputHandle.Dispose();
7274
_inputHandle = default;

src/Servers/IIS/IIS/src/Core/IO/WebSocketsAsyncIOEngine.Write.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,23 +25,25 @@ internal sealed class WebSocketWriteOperation : AsyncWriteOperationBase
2525
};
2626

2727
private readonly WebSocketsAsyncIOEngine _engine;
28-
private readonly GCHandle _thisHandle;
28+
private GCHandle _thisHandle;
2929

3030
public WebSocketWriteOperation(WebSocketsAsyncIOEngine engine)
3131
{
3232
_engine = engine;
33-
_thisHandle = GCHandle.Alloc(this);
3433
}
3534

3635
protected override unsafe int WriteChunks(IntPtr requestHandler, int chunkCount, HttpApiTypes.HTTP_DATA_CHUNK* dataChunks, out bool completionExpected)
3736
{
37+
_thisHandle = GCHandle.Alloc(this);
3838
return NativeMethods.HttpWebsocketsWriteBytes(requestHandler, dataChunks, chunkCount, WriteCallback, (IntPtr)_thisHandle, out completionExpected);
3939
}
4040

4141
protected override void ResetOperation()
4242
{
4343
base.ResetOperation();
4444

45+
_thisHandle.Free();
46+
4547
_engine.ReturnOperation(this);
4648
}
4749
}

src/Servers/IIS/IIS/src/Core/IO/WebSocketsAsyncIOEngine.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public ValueTask<int> ReadAsync(Memory<byte> memory)
3939
var read = GetReadOperation();
4040
read.Initialize(_handler, memory);
4141
read.Invoke();
42-
return new ValueTask<int>(read, 0);
42+
return new ValueTask<int>(read, 0);
4343
}
4444
}
4545

0 commit comments

Comments
 (0)