Re-enable CA2008 (tasks without a TaskScheduler) (dotnet/corefx#39707)
authorStephen Toub <stoub@microsoft.com>
Wed, 24 Jul 2019 07:44:54 +0000 (03:44 -0400)
committerViktor Hofer <viktor.hofer@microsoft.com>
Wed, 24 Jul 2019 07:44:54 +0000 (09:44 +0200)
Commit migrated from https://github.com/dotnet/corefx/commit/9958740c24865cad40dd84bcd8c416cea927e3cc

src/libraries/CodeAnalysis.ruleset
src/libraries/System.Data.SqlClient/src/System/Data/SqlClient/SqlBulkCopy.cs
src/libraries/System.Data.SqlClient/src/System/Data/SqlClient/SqlCommand.cs
src/libraries/System.Data.SqlClient/src/System/Data/SqlClient/SqlUtil.cs
src/libraries/System.Data.SqlClient/src/System/Data/SqlClient/TdsParser.cs
src/libraries/System.Data.SqlClient/src/System/Data/SqlClient/TdsParserStateObject.cs

index 845ee61..cd4c4aa 100644 (file)
@@ -58,7 +58,6 @@
     <Rule Id="CA1823" Action="None" /> <!-- Avoid unused private fields -->
     <Rule Id="CA1825" Action="None" /> <!-- Avoid zero-length array allocations. -->
     <Rule Id="CA2000" Action="None" /> <!-- Dispose objects before losing scope -->
-    <Rule Id="CA2008" Action="None" /> <!-- Do not create tasks without passing a TaskScheduler -->
     <Rule Id="CA2010" Action="None" /> <!-- Always consume the value returned by methods marked with PreserveSigAttribute -->
     <Rule Id="CA2100" Action="None" /> <!-- Review SQL queries for security vulnerabilities -->
     <Rule Id="CA2101" Action="None" /> <!-- Specify marshaling for P/Invoke string arguments -->
index ff278b9..39aa38c 100644 (file)
@@ -1846,7 +1846,7 @@ namespace System.Data.SqlClient
                                 onSuccess: state => ((TaskCompletionSource<object>)state).SetResult(null)
                             );
                         }
-                    }, ctoken); // We do not need to propagate exception, etc, from reconnect task, we just need to wait for it to finish.
+                    }, ctoken, TaskContinuationOptions.DenyChildAttach, TaskScheduler.Default); // We do not need to propagate exception, etc, from reconnect task, we just need to wait for it to finish.
                     return tcs.Task;
                 }
                 else
index 73ed22a..9f5fa08 100644 (file)
@@ -952,9 +952,11 @@ namespace System.Data.SqlClient
                 if (callback != null)
                 {
                     completion.Task.ContinueWith(
-                        (task,state) => ((AsyncCallback)state)(task),
-                        state: callback
-                    );
+                        (task, state) => ((AsyncCallback)state)(task),
+                        state: callback,
+                        CancellationToken.None,
+                        TaskContinuationOptions.DenyChildAttach,
+                        TaskScheduler.Default);
                 }
 
                 return completion.Task;
@@ -1298,9 +1300,11 @@ namespace System.Data.SqlClient
                 if (callback != null)
                 {
                     completion.Task.ContinueWith(
-                        (task,state) => ((AsyncCallback)state)(task),
-                        state: callback
-                    );
+                        (task, state) => ((AsyncCallback)state)(task),
+                        state: callback,
+                        CancellationToken.None,
+                        TaskContinuationOptions.DenyChildAttach,
+                        TaskScheduler.Default);
                 }
                 return completion.Task;
             }
@@ -1585,9 +1589,11 @@ namespace System.Data.SqlClient
                 if (callback != null)
                 {
                     completion.Task.ContinueWith(
-                        (task,state) => ((AsyncCallback)state)(task),
-                        state: callback
-                    );
+                        (task, state) => ((AsyncCallback)state)(task),
+                        state: callback,
+                        CancellationToken.None,
+                        TaskContinuationOptions.DenyChildAttach,
+                        TaskScheduler.Default);
                 }
                 return completion.Task;
             }
index d972511..7dce1aa 100644 (file)
@@ -216,7 +216,7 @@ namespace System.Data.SqlClient
                     {
                         completion.TrySetException(exc());
                     }
-                });
+                }, CancellationToken.None, TaskContinuationOptions.DenyChildAttach, TaskScheduler.Default);
             }
         }
     }
index 44b6760..b22c8fc 100644 (file)
@@ -1108,7 +1108,7 @@ namespace System.Data.SqlClient
                     var connHandler = _connHandler;
                     Action<Action> wrapCloseAction = closeAction =>
                     {
-                        Task.Factory.StartNew(() =>
+                        Task.Run(() =>
                         {
                             connHandler._parserLock.Wait(canReleaseFromAnyThread: false);
                             connHandler.ThreadHasParserLockForClose = true;
index c1d32cd..33c0af0 100644 (file)
@@ -2257,7 +2257,7 @@ namespace System.Data.SqlClient
                                         DecrementPendingCallbacks(release: false);
                                     }
                                 }
-                            });
+                            }, CancellationToken.None, TaskContinuationOptions.DenyChildAttach, TaskScheduler.Default);
                         }
                     }
                 }
@@ -2784,7 +2784,7 @@ namespace System.Data.SqlClient
 
                 // The safest thing to do is to ensure that the connection is broken and attempt to cancel the task
                 // This must be done from another thread to not block the callback thread                
-                Task.Factory.StartNew(() =>
+                Task.Run(() =>
                 {
                     _parser.State = TdsParserState.Broken;
                     _parser.Connection.BreakConnection();