From 973156d54a55d7727810cb9f5d23da784b89261b Mon Sep 17 00:00:00 2001 From: Ilya Date: Thu, 1 Aug 2019 10:37:55 +0500 Subject: [PATCH] Cleanup: ThreadAbortException is now in .Net Core 3.0 (#10230) * Cleanup: ThreadAbortException is now in .Net Core 3.0 * Fix CodeFactor issue * Remove ThreadAbortException * Remove ThreadAbortException relared code --- .../ScheduledJobDefinition.cs | 4 -- .../engine/hostifaces/LocalPipeline.cs | 13 +---- .../engine/hostifaces/Pipeline.cs | 6 -- .../engine/hostifaces/PowerShell.cs | 30 ---------- .../interpreter/ControlFlowInstructions.cs | 21 ------- .../engine/interpreter/InterpretedFrame.cs | 4 -- .../engine/interpreter/Interpreter.cs | 56 +------------------ .../engine/remoting/client/JobManager.cs | 4 -- 8 files changed, 4 insertions(+), 134 deletions(-) diff --git a/src/Microsoft.PowerShell.ScheduledJob/ScheduledJobDefinition.cs b/src/Microsoft.PowerShell.ScheduledJob/ScheduledJobDefinition.cs index eafe58f3b97..474265eac42 100644 --- a/src/Microsoft.PowerShell.ScheduledJob/ScheduledJobDefinition.cs +++ b/src/Microsoft.PowerShell.ScheduledJob/ScheduledJobDefinition.cs @@ -2023,10 +2023,6 @@ public Job2 Run() { ex = e; } - catch (System.Threading.ThreadAbortException e) - { - ex = e; - } catch (IOException e) { ex = e; diff --git a/src/System.Management.Automation/engine/hostifaces/LocalPipeline.cs b/src/System.Management.Automation/engine/hostifaces/LocalPipeline.cs index 94f3bfb2e46..a0d50bd15d8 100644 --- a/src/System.Management.Automation/engine/hostifaces/LocalPipeline.cs +++ b/src/System.Management.Automation/engine/hostifaces/LocalPipeline.cs @@ -625,18 +625,11 @@ private void InvokeThreadProc() SetPipelineState(PipelineState.Failed, ex); SetHadErrors(true); } -#if !CORECLR // No ThreadAbortException In CoreCLR - catch (ThreadAbortException ex) - { - SetPipelineState(PipelineState.Failed, ex); - SetHadErrors(true); - } -#endif - // 1021203-2005/05/09-JonN - // HaltCommandException will cause the command - // to stop, but not be reported as an error. catch (HaltCommandException) { + // 1021203-2005/05/09-JonN + // HaltCommandException will cause the command + // to stop, but not be reported as an error. SetPipelineState(PipelineState.Completed); } finally diff --git a/src/System.Management.Automation/engine/hostifaces/Pipeline.cs b/src/System.Management.Automation/engine/hostifaces/Pipeline.cs index 4c37fed59ae..b73d4ed7db2 100644 --- a/src/System.Management.Automation/engine/hostifaces/Pipeline.cs +++ b/src/System.Management.Automation/engine/hostifaces/Pipeline.cs @@ -497,9 +497,6 @@ internal void SetHadErrors(bool status) /// because the current CLR permissions do not allow adequate /// reflection access to a cmdlet assembly. /// - /// - /// The thread in which the pipeline was executing was aborted. - /// /// /// Pipeline.Invoke can throw a variety of exceptions derived /// from RuntimeException. The most likely of these exceptions @@ -568,9 +565,6 @@ public Collection Invoke() /// because the current CLR permissions do not allow adequate /// reflection access to a cmdlet assembly. /// - /// - /// The thread in which the pipeline was executing was aborted. - /// /// /// Pipeline.Invoke can throw a variety of exceptions derived /// from RuntimeException. The most likely of these exceptions diff --git a/src/System.Management.Automation/engine/hostifaces/PowerShell.cs b/src/System.Management.Automation/engine/hostifaces/PowerShell.cs index 0517de0e178..c948b26b79a 100644 --- a/src/System.Management.Automation/engine/hostifaces/PowerShell.cs +++ b/src/System.Management.Automation/engine/hostifaces/PowerShell.cs @@ -2258,9 +2258,6 @@ internal void InvokeWithDebugger( /// because the current CLR permissions do not allow adequate /// reflection access to a cmdlet assembly. /// - /// - /// The thread in which the command was executing was aborted. - /// /// /// PowerShell.Invoke can throw a variety of exceptions derived /// from RuntimeException. The most likely of these exceptions @@ -2320,9 +2317,6 @@ public Collection Invoke() /// because the current CLR permissions do not allow adequate /// reflection access to a cmdlet assembly. /// - /// - /// The thread in which the command was executing was aborted. - /// /// /// PowerShell.Invoke can throw a variety of exceptions derived /// from RuntimeException. The most likely of these exceptions @@ -2385,9 +2379,6 @@ public Collection Invoke(IEnumerable input) /// because the current CLR permissions do not allow adequate /// reflection access to a cmdlet assembly. /// - /// - /// The thread in which the command was executing was aborted. - /// /// /// PowerShell.Invoke can throw a variety of exceptions derived /// from RuntimeException. The most likely of these exceptions @@ -2447,9 +2438,6 @@ public Collection Invoke(IEnumerable input, PSInvocationSettings setti /// because the current CLR permissions do not allow adequate /// reflection access to a cmdlet assembly. /// - /// - /// The thread in which the command was executing was aborted. - /// /// /// PowerShell.Invoke can throw a variety of exceptions derived /// from RuntimeException. The most likely of these exceptions @@ -2513,9 +2501,6 @@ public Collection Invoke() /// because the current CLR permissions do not allow adequate /// reflection access to a cmdlet assembly. /// - /// - /// The thread in which the command was executing was aborted. - /// /// /// PowerShell.Invoke can throw a variety of exceptions derived /// from RuntimeException. The most likely of these exceptions @@ -2580,9 +2565,6 @@ public Collection Invoke(IEnumerable input) /// because the current CLR permissions do not allow adequate /// reflection access to a cmdlet assembly. /// - /// - /// The thread in which the command was executing was aborted. - /// /// /// PowerShell.Invoke can throw a variety of exceptions derived /// from RuntimeException. The most likely of these exceptions @@ -2650,9 +2632,6 @@ public Collection Invoke(IEnumerable input, PSInvocationSettings settings) /// because the current CLR permissions do not allow adequate /// reflection access to a cmdlet assembly. /// - /// - /// The thread in which the command was executing was aborted. - /// /// /// PowerShell.Invoke can throw a variety of exceptions derived /// from RuntimeException. The most likely of these exceptions @@ -2715,9 +2694,6 @@ public void Invoke(IEnumerable input, IList output) /// because the current CLR permissions do not allow adequate /// reflection access to a cmdlet assembly. /// - /// - /// The thread in which the command was executing was aborted. - /// /// /// PowerShell.Invoke can throw a variety of exceptions derived /// from RuntimeException. The most likely of these exceptions @@ -2789,9 +2765,6 @@ public void Invoke(IEnumerable input, IList output, PSInvocationSettings s /// because the current CLR permissions do not allow adequate /// reflection access to a cmdlet assembly. /// - /// - /// The thread in which the command was executing was aborted. - /// /// /// PowerShell.Invoke can throw a variety of exceptions derived /// from RuntimeException. The most likely of these exceptions @@ -4457,9 +4430,6 @@ internal void SetIsNested(bool isNested) /// because the current CLR permissions do not allow adequate /// reflection access to a cmdlet assembly. /// - /// - /// The thread in which the command was executing was aborted. - /// /// /// PowerShell.Invoke can throw a variety of exceptions derived /// from RuntimeException. The most likely of these exceptions diff --git a/src/System.Management.Automation/engine/interpreter/ControlFlowInstructions.cs b/src/System.Management.Automation/engine/interpreter/ControlFlowInstructions.cs index f546106f0c8..1c2523874c1 100644 --- a/src/System.Management.Automation/engine/interpreter/ControlFlowInstructions.cs +++ b/src/System.Management.Automation/engine/interpreter/ControlFlowInstructions.cs @@ -249,9 +249,6 @@ public override string ToString() /// The jump needs to execute both finally blocks, the first one on stack level 4 the /// second one on stack level 2. So, it needs to jump the first finally block, pop 2 items from the stack, /// run second finally block and pop another 2 items from the stack and set instruction pointer to label L. - /// - /// Goto also needs to rethrow ThreadAbortException iff it jumps out of a catch handler and - /// the current thread is in "abort requested" state. /// internal sealed class GotoInstruction : IndexedBranchInstruction { @@ -302,9 +299,6 @@ internal static GotoInstruction Create(int labelIndex, bool hasResult, bool hasV public override int Run(InterpretedFrame frame) { - // Are we jumping out of catch/finally while aborting the current thread? - Interpreter.AbortThreadIfRequested(frame, _labelIndex); - // goto the target label or the current finally continuation: return frame.Goto(_labelIndex, _hasValue ? frame.Pop() : Interpreter.NoValue, gotoExceptionHandler: false); } @@ -387,15 +381,6 @@ public override int Run(InterpretedFrame frame) ExceptionHandler exHandler; frame.InstructionIndex += _tryHandler.GotoHandler(frame, exception, out exHandler); if (exHandler == null) { throw; } -#if !CORECLR // Thread.Abort and ThreadAbortException are not in CoreCLR. - // stay in the current catch so that ThreadAbortException is not rethrown by CLR: - var abort = exception as ThreadAbortException; - if (abort != null) - { - Interpreter.AnyAbortException = abort; - frame.CurrentAbortHandler = exHandler; - } -#endif bool rethrow = false; try { @@ -602,8 +587,6 @@ internal static LeaveExceptionHandlerInstruction Create(int labelIndex, bool has public override int Run(InterpretedFrame frame) { - // CLR rethrows ThreadAbortException when leaving catch handler if abort is requested on the current thread. - Interpreter.AbortThreadIfRequested(frame, _labelIndex); return GetLabel(frame).Index - frame.InstructionIndex; } } @@ -640,11 +623,7 @@ private LeaveFaultInstruction(bool hasValue) public override int Run(InterpretedFrame frame) { - // TODO: ThreadAbortException ? - object exception = frame.Pop(); - // ExceptionHandler handler; - // return frame.Interpreter.GotoHandler(frame, exception, out handler); throw new RethrowException(); } } diff --git a/src/System.Management.Automation/engine/interpreter/InterpretedFrame.cs b/src/System.Management.Automation/engine/interpreter/InterpretedFrame.cs index 0ce038ff4f3..3e15b1fbba4 100644 --- a/src/System.Management.Automation/engine/interpreter/InterpretedFrame.cs +++ b/src/System.Management.Automation/engine/interpreter/InterpretedFrame.cs @@ -48,10 +48,6 @@ internal sealed class InterpretedFrame public int StackIndex; public int InstructionIndex; - // When a ThreadAbortException is raised from interpreted code this is the first frame that caught it. - // No handlers within this handler re-abort the current thread when left. - public ExceptionHandler CurrentAbortHandler; - internal InterpretedFrame(Interpreter interpreter, StrongBox[] closure) { Interpreter = interpreter; diff --git a/src/System.Management.Automation/engine/interpreter/Interpreter.cs b/src/System.Management.Automation/engine/interpreter/Interpreter.cs index 45b0d8b9bf7..622364184e0 100644 --- a/src/System.Management.Automation/engine/interpreter/Interpreter.cs +++ b/src/System.Management.Automation/engine/interpreter/Interpreter.cs @@ -13,17 +13,9 @@ * * ***************************************************************************/ -#if !CLR2 +using System.Collections.Generic; using System.Linq.Expressions; -#else -using Microsoft.Scripting.Ast; -#endif - using System.Runtime.CompilerServices; -using System.Threading; - -using System.Diagnostics; -using System.Collections.Generic; namespace System.Management.Automation.Interpreter { @@ -113,51 +105,5 @@ public void Run(InterpretedFrame frame) frame.InstructionIndex = index; } } - - /// - /// To get to the current AbortReason object on Thread.CurrentThread - /// we need to use ExceptionState property of any ThreadAbortException instance. - /// - [ThreadStatic] - internal static ThreadAbortException AnyAbortException = null; - - /// - /// If the target that 'Goto' jumps to is inside the current catch block or the subsequent finally block, - /// we delay the call to 'Abort' method, because we want to finish the catch/finally blocks. - /// - internal static void AbortThreadIfRequested(InterpretedFrame frame, int targetLabelIndex) - { - var abortHandler = frame.CurrentAbortHandler; - var targetInstrIndex = frame.Interpreter._labels[targetLabelIndex].Index; - if (abortHandler != null && - !abortHandler.IsInsideCatchBlock(targetInstrIndex) && - !abortHandler.IsInsideFinallyBlock(targetInstrIndex)) - { - frame.CurrentAbortHandler = null; - - var currentThread = Thread.CurrentThread; - if ((currentThread.ThreadState & System.Threading.ThreadState.AbortRequested) != 0) - { - Debug.Assert(AnyAbortException != null); - - // The current abort reason needs to be preserved. -#if SILVERLIGHT - currentThread.Abort(); -#else - currentThread.Abort(AnyAbortException.ExceptionState); -#endif - } - } - } - - internal int ReturnAndRethrowLabelIndex - { - get - { - // the last label is "return and rethrow" label: - Debug.Assert(_labels[_labels.Length - 1].Index == RethrowOnReturn); - return _labels.Length - 1; - } - } } } diff --git a/src/System.Management.Automation/engine/remoting/client/JobManager.cs b/src/System.Management.Automation/engine/remoting/client/JobManager.cs index 8006ed1dbdb..8585d296007 100644 --- a/src/System.Management.Automation/engine/remoting/client/JobManager.cs +++ b/src/System.Management.Automation/engine/remoting/client/JobManager.cs @@ -391,10 +391,6 @@ private JobSourceAdapter GetJobSourceAdapter(JobDefinition definition) { ex = e; } - catch (ThreadAbortException e) - { - ex = e; - } if (ex != null) {