Skip to content
This repository has been archived by the owner on Jan 23, 2023. It is now read-only.
/ corefx Public archive

Commit

Permalink
Fix
Browse files Browse the repository at this point in the history
  • Loading branch information
benaadams committed Feb 12, 2019
1 parent 5129e3a commit ea5199c
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
16 changes: 14 additions & 2 deletions src/System.IO.Pipelines/src/System/IO/Pipelines/Pipe.cs
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,8 @@ private void ReturnSegments(BufferSegment from, BufferSegment toExclusive)
do
{
BufferSegment next = from.NextSegment;
Debug.Assert(next != null || toExclusive == null);

from.ResetMemory();

if ((uint)index < (uint)segmentPool.Length)
Expand Down Expand Up @@ -440,6 +442,7 @@ private void AdvanceReader(BufferSegment consumedSegment, int consumedIndex, Buf
BufferSegment returnEnd = null;

CompletionData completionData = default;
bool isReadComplete;

lock (_sync)
{
Expand Down Expand Up @@ -517,15 +520,24 @@ private void AdvanceReader(BufferSegment consumedSegment, int consumedIndex, Buf
_readerAwaitable.SetUncompleted();
}

_operationState.EndRead();
isReadComplete = _operationState.TryEndRead();
}

TrySchedule(_writerScheduler, completionData);
if (isReadComplete)
{
TrySchedule(_writerScheduler, completionData);
}

if (returnStart != null && returnStart != returnEnd)
{
ReturnSegments(returnStart, returnEnd);
}

if (!isReadComplete)
{
// Segments need to be returned (above) prior to the throw.
ThrowHelper.ThrowInvalidOperationException_NoReadToComplete();
}
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,20 @@ public void EndRead()
_state &= ~(State.Reading | State.ReadingTentative);
}


[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool TryEndRead()
{
if ((_state & State.Reading) != State.Reading &&
(_state & State.ReadingTentative) != State.ReadingTentative)
{
return false;
}

_state &= ~(State.Reading | State.ReadingTentative);
return true;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void BeginWrite()
{
Expand Down

0 comments on commit ea5199c

Please sign in to comment.