Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit 1f94e2c

Browse files
committed
Merge pull request #2690 from Maxwe11/win32fs-cached-task
Win32FileStream: Return cached task at synchronous completion
2 parents 1820ee2 + 0c6ae1b commit 1f94e2c

File tree

1 file changed

+5
-4
lines changed

1 file changed

+5
-4
lines changed

src/System.IO.FileSystem/src/System/IO/Win32FileStream.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -577,7 +577,7 @@ private void FlushWrite(bool calledFromFinalizer)
577577
#if USE_OVERLAPPED
578578
if (_isAsync)
579579
{
580-
Task<int> writeTask = WriteInternalCoreAsync(_buffer, 0, _writePos, CancellationToken.None);
580+
Task writeTask = WriteInternalCoreAsync(_buffer, 0, _writePos, CancellationToken.None);
581581
// With our Whidbey async IO & overlapped support for AD unloads,
582582
// we don't strictly need to block here to release resources
583583
// since that support takes care of the pinning & freeing the
@@ -1394,7 +1394,7 @@ private Task WriteInternalAsync(byte[] array, int offset, int numBytes, Cancella
13941394
}
13951395

13961396
[System.Security.SecuritySafeCritical] // auto-generated
1397-
unsafe private Task<int> WriteInternalCoreAsync(byte[] bytes, int offset, int numBytes, CancellationToken cancellationToken)
1397+
private unsafe Task WriteInternalCoreAsync(byte[] bytes, int offset, int numBytes, CancellationToken cancellationToken)
13981398
{
13991399
Debug.Assert(!_handle.IsClosed, "!_handle.IsClosed");
14001400
Debug.Assert(_parent.CanWrite, "_parent.CanWrite");
@@ -1458,9 +1458,10 @@ unsafe private Task<int> WriteInternalCoreAsync(byte[] bytes, int offset, int nu
14581458
// For pipes, when they are closed on the other side, they will come here.
14591459
if (errorCode == ERROR_NO_DATA)
14601460
{
1461-
// Not an error, but EOF. AsyncFSCallback will NOT be
1462-
// called. Call the user callback here.
1461+
// Not an error, but EOF. AsyncFSCallback will NOT be called.
1462+
// Completing TCS and return cached task allowing the GC to collect TCS.
14631463
completionSource.SetCompletedSynchronously(0);
1464+
return Task.CompletedTask;
14641465
}
14651466
else if (errorCode != ERROR_IO_PENDING)
14661467
{

0 commit comments

Comments
 (0)