Improve error when socket ValueTask consumed incorrectly (dotnet/corefx#41831)
authorStephen Toub <stoub@microsoft.com>
Wed, 16 Oct 2019 21:28:34 +0000 (17:28 -0400)
committerSantiago Fernandez Madero <safern@microsoft.com>
Wed, 16 Oct 2019 21:28:34 +0000 (14:28 -0700)
* Improve error when socket ValueTask consumed incorrectly

* Update src/System.Net.Sockets/src/Resources/Strings.resx

Co-Authored-By: David Shulman <david.shulman@microsoft.com>
Commit migrated from https://github.com/dotnet/corefx/commit/a60703d66f0a75a6bb05d584b7677d9b04edf46a

src/libraries/System.Net.Sockets/src/Resources/Strings.resx
src/libraries/System.Net.Sockets/src/System/Net/Sockets/Socket.Tasks.cs

index 547bb2c..cad4341 100644 (file)
   <data name="net_sockets_sendpackelement_FileStreamMustBeAsync" xml:space="preserve">
     <value>The FileStream must have been opened for asynchronous reading and writing.</value>
   </data>
+  <data name="net_sockets_valuetaskmisuse" xml:space="preserve">
+    <value>A ValueTask returned from an asynchronous socket operation was consumed concurrently. ValueTasks must only ever be awaited once. (Id: {0}).</value>
+  </data>
 </root>
index 1dc7680..304f10a 100644 (file)
@@ -811,9 +811,9 @@ namespace System.Net.Sockets
         {
             internal static readonly AwaitableSocketAsyncEventArgs Reserved = new AwaitableSocketAsyncEventArgs() { _continuation = null };
             /// <summary>Sentinel object used to indicate that the operation has completed prior to OnCompleted being called.</summary>
-            private static readonly Action<object> s_completedSentinel = new Action<object>(state => throw new Exception(nameof(s_completedSentinel)));
+            private static readonly Action<object> s_completedSentinel = new Action<object>(state => throw new InvalidOperationException(SR.Format(SR.net_sockets_valuetaskmisuse, nameof(s_completedSentinel))));
             /// <summary>Sentinel object used to indicate that the instance is available for use.</summary>
-            private static readonly Action<object> s_availableSentinel = new Action<object>(state => throw new Exception(nameof(s_availableSentinel)));
+            private static readonly Action<object> s_availableSentinel = new Action<object>(state => throw new InvalidOperationException(SR.Format(SR.net_sockets_valuetaskmisuse, nameof(s_availableSentinel))));
             /// <summary>
             /// <see cref="s_availableSentinel"/> if the object is available for use, after GetResult has been called on a previous use.
             /// null if the operation has not completed.