Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix race condition with cancellation tokens in Read/Flush operations … #61500

Merged
merged 5 commits into from
Nov 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions eng/packaging.targets
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@
<VersionPrefix>$(MajorVersion).$(MinorVersion).$(ServicingVersion)</VersionPrefix>
<_IsWindowsDesktopApp Condition="$(WindowsDesktopCoreAppLibrary.Contains('$(AssemblyName);'))">true</_IsWindowsDesktopApp>
<_IsAspNetCoreApp Condition="$(AspNetCoreAppLibrary.Contains('$(AssemblyName);'))">true</_IsAspNetCoreApp>
<_AssemblyInTargetingPack Condition="'$(IsNETCoreAppSrc)' == 'true' or '$(_IsAspNetCoreApp)' == 'true' or '$(_IsWindowsDesktopApp)' == 'true'">true</_AssemblyInTargetingPack>
<_AssemblyInTargetingPack Condition="('$(IsNETCoreAppSrc)' == 'true' or '$(_IsAspNetCoreApp)' == 'true' or '$(_IsWindowsDesktopApp)' == 'true') and '$(TargetFrameworkIdentifier)' != '.NETFramework'">true</_AssemblyInTargetingPack>
<!-- Assembly version do not get updated in non-netfx ref pack assemblies. -->
<AssemblyVersion Condition="'$(_AssemblyInTargetingPack)' != 'true' or '$(TargetFrameworkIdentifier)' == '.NETFramework'">$(MajorVersion).$(MinorVersion).0.$(ServicingVersion)</AssemblyVersion>
<AssemblyVersion Condition="'$(_AssemblyInTargetingPack)' != 'true'">$(MajorVersion).$(MinorVersion).0.$(ServicingVersion)</AssemblyVersion>
</PropertyGroup>

<ItemGroup Condition="'$(EnablePackageValidation)' == 'true'">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ Commonly Used Types:
System.IO.Pipelines.Pipe
System.IO.Pipelines.PipeWriter
System.IO.Pipelines.PipeReader</PackageDescription>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<ServicingVersion>1</ServicingVersion>
</PropertyGroup>
<ItemGroup>
<Compile Include="$(CommonPath)System\Threading\Tasks\TaskToApm.cs"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,16 +47,33 @@ public void BeginOperation(CancellationToken cancellationToken, Action<object?>
{
cancellationToken.ThrowIfCancellationRequested();

_awaitableState |= AwaitableState.Running;

// Don't register if already completed, we would immediately unregistered in ObserveCancellation
if (cancellationToken.CanBeCanceled && !IsCompleted)
{
#if DEBUG
var previousAwaitableState = _awaitableState;
#endif

_cancellationTokenRegistration = cancellationToken.UnsafeRegister(callback, state);

// If we get back the default CancellationTokenRegistration then it means the
// callback synchronously and we can throw inline. This is safe because we haven't changed
// the state of the awaitable as yet.
if (_cancellationTokenRegistration == default(CancellationTokenRegistration))
{
#if DEBUG
Debug.Assert(previousAwaitableState == _awaitableState, "The awaitable state changed!");
#endif

cancellationToken.ThrowIfCancellationRequested();
}

#if (NETSTANDARD2_0 || NETFRAMEWORK)
_cancellationToken = cancellationToken;
#endif
_cancellationTokenRegistration = cancellationToken.UnsafeRegister(callback, state);
}

_awaitableState |= AwaitableState.Running;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
Expand Down