Skip to content

Commit

Permalink
Merge in 'release/6.0' changes
Browse files Browse the repository at this point in the history
  • Loading branch information
dotnet-bot committed Apr 13, 2022
2 parents 46f6513 + a21b9a2 commit 70ae3df
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 8 deletions.
Expand Up @@ -9,7 +9,8 @@ Commonly Used Types:
System.IO.Pipelines.Pipe
System.IO.Pipelines.PipeWriter
System.IO.Pipelines.PipeReader</PackageDescription>
<ServicingVersion>2</ServicingVersion>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<ServicingVersion>3</ServicingVersion>
</PropertyGroup>
<ItemGroup>
<Compile Include="$(CommonPath)System\Threading\Tasks\TaskToApm.cs"
Expand Down
Expand Up @@ -347,15 +347,15 @@ internal ValueTask<FlushResult> FlushAsync(CancellationToken cancellationToken)
ValueTask<FlushResult> result;
lock (SyncObj)
{
PrepareFlush(out completionData, out result, cancellationToken);
PrepareFlushUnsynchronized(out completionData, out result, cancellationToken);
}

TrySchedule(ReaderScheduler, completionData);

return result;
}

private void PrepareFlush(out CompletionData completionData, out ValueTask<FlushResult> result, CancellationToken cancellationToken)
private void PrepareFlushUnsynchronized(out CompletionData completionData, out ValueTask<FlushResult> result, CancellationToken cancellationToken)
{
var completeReader = CommitUnsynchronized();

Expand Down Expand Up @@ -691,6 +691,9 @@ internal ValueTask<ReadResult> ReadAtLeastAsync(int minimumBytes, CancellationTo

// We also need to flip the reading state off
_operationState.EndRead();

// Begin read again to wire up cancellation token
_readerAwaitable.BeginOperation(token, s_signalReaderAwaitable, this);
}

// If the writer is currently paused and we are about the wait for more data then this would deadlock.
Expand Down Expand Up @@ -1057,7 +1060,7 @@ internal ValueTask<FlushResult> WriteAsync(ReadOnlyMemory<byte> source, Cancella
WriteMultiSegment(source.Span);
}

PrepareFlush(out completionData, out result, cancellationToken);
PrepareFlushUnsynchronized(out completionData, out result, cancellationToken);
}

TrySchedule(ReaderScheduler, completionData);
Expand Down
Expand Up @@ -162,5 +162,36 @@ public async Task WriteAndCancellingPendingReadBeforeReadAtLeastAsync()
Assert.True(result.IsCanceled);
PipeReader.AdvanceTo(buffer.End);
}

[Fact]
public Task ReadAtLeastAsyncCancelableWhenWaitingForMoreData()
{
CancellationTokenSource cts = new CancellationTokenSource();
ValueTask<ReadResult> task = PipeReader.ReadAtLeastAsync(1, cts.Token);
cts.Cancel();
return Assert.ThrowsAsync<OperationCanceledException>(async () => await task);
}

[Fact]
public async Task ReadAtLeastAsyncCancelableAfterReadingSome()
{
CancellationTokenSource cts = new CancellationTokenSource();
await Pipe.WriteAsync(new byte[10], default);
ValueTask<ReadResult> task = PipeReader.ReadAtLeastAsync(11, cts.Token);
cts.Cancel();
await Assert.ThrowsAsync<OperationCanceledException>(async () => await task);
}

[Fact]
public async Task ReadAtLeastAsyncCancelableAfterReadingSomeAndWritingAfterStartingRead()
{
CancellationTokenSource cts = new CancellationTokenSource();
await Pipe.WriteAsync(new byte[10], default);
ValueTask<ReadResult> task = PipeReader.ReadAtLeastAsync(12, cts.Token);
// Write, but not enough to unblock ReadAtLeastAsync
await Pipe.WriteAsync(new byte[1], default);
cts.Cancel();
await Assert.ThrowsAnyAsync<OperationCanceledException>(async () => await task);
}
}
}
3 changes: 2 additions & 1 deletion src/libraries/System.Text.Json/src/System.Text.Json.csproj
Expand Up @@ -9,7 +9,8 @@
<Nullable>enable</Nullable>
<IncludeInternalObsoleteAttribute>true</IncludeInternalObsoleteAttribute>
<IsPackable>true</IsPackable>
<ServicingVersion>3</ServicingVersion>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<ServicingVersion>4</ServicingVersion>
<PackageDescription>Provides high-performance and low-allocating types that serialize objects to JavaScript Object Notation (JSON) text and deserialize JSON text to objects, with UTF-8 support built-in. Also provides types to read and write JSON text encoded as UTF-8, and to create an in-memory document object model (DOM), that is read-only, for random access of the JSON elements within a structured view of the data.

Commonly Used Types:
Expand Down
Expand Up @@ -10,6 +10,7 @@
using System.Text.Json.Serialization;
using System.Text.Json.Serialization.Converters;
using System.Text.Json.Serialization.Metadata;
using System.Threading;

namespace System.Text.Json
{
Expand All @@ -30,10 +31,10 @@ public sealed partial class JsonSerializerOptions
[RequiresUnreferencedCode(JsonSerializer.SerializationUnreferencedCodeMessage)]
private void RootBuiltInConverters()
{
if (s_defaultSimpleConverters is null)
if (Volatile.Read(ref s_defaultFactoryConverters) is null)
{
s_defaultSimpleConverters = GetDefaultSimpleConverters();
s_defaultFactoryConverters = new JsonConverter[]
Volatile.Write(ref s_defaultFactoryConverters, new JsonConverter[]
{
// Check for disallowed types.
new UnsupportedTypeConverterFactory(),
Expand All @@ -48,7 +49,7 @@ private void RootBuiltInConverters()
new IEnumerableConverterFactory(),
// Object should always be last since it converts any type.
new ObjectConverterFactory()
};
});
}
}

Expand Down

0 comments on commit 70ae3df

Please sign in to comment.