Finally blocks run when the try block is cancelled

1.2k views Asked by At

In my TFS2012 build workflow, I have a Parallel activity, and inside some of the branches I have try-finally activities. If one branch fails (say, compilation with CodeAnalysis finds CodeAnalysis errors), then the other branches are cancelled. However, if a branch is inside a try block that has a finally block, the finally block runs, and if it takes more than 30 seconds then the cancellation fails with:

BuildWarning: The workflow instance accepted a stop request but did not complete within 00:00:29.9922015. This may indicate an AsyncCodeActivity is active that does not support cancellation.

I'd like to avoid such messages, so:

  1. Can I somehow have a try-finally activity where the finally runs when an exception is raised inside the try, but not when the whole thing is cancelled?
  2. If not, I want to add an if inside the finally block that the workflow had been cancelled, and not do long things. How can I know that the workflow has been cancelled?

Edit: This pattern appears even in DefaultTemplate.11.1.xaml which comes with TFS2012 - CopyDirectory can take a long time, but it's inside a TryCatch.Finally:

<TryCatch DisplayName="Try Compile, Test, and Associate Changesets and Work Items">
  <TryCatch.Finally>
    <Sequence DisplayName="Revert Workspace and Copy Files to Drop Location">
      <mtbwa:InvokeForReason DisplayName="Revert Workspace for Shelveset Builds" Reason="CheckInShelveset, ValidateShelveset">
        <mtbwa:RevertWorkspace DisplayName="Revert Workspace" Workspace="[Workspace]" />
      </mtbwa:InvokeForReason>
      <If  Condition="[Not String.IsNullOrEmpty(DropLocation)]" DisplayName="If DropLocation is Set">
        <If.Then>
          <mtbwa:CopyDirectory DisplayName="Drop Files to Drop Location" Source="[BinariesDirectory]" Destination="[DropLocation]" />
        </If.Then>
      </If>
    </Sequence>
  </TryCatch.Finally>
  <TryCatch.Try>
   (...)
  </TryCatch.Try>
</TryCatch>
0

There are 0 answers