-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
[Bug]: Please re-initilize "_terminatedTool" in class Microsoft.Build.Utilities.ToolTask #8541
Comments
The problem found in MSBuild version 17.4.1.60106, but it's the same in the latest version. |
I think that would be a reasonable change, though |
Thanks for considering the change. I'll submit a PR. Some background: I need to execute an external command repeatedly (predefined number of maximum repeats) with timeout until it succeeds. Not sure if I can execute another MSBuild task from within a task, which would look better. I called base.Execute() multiple times as a workaround. I know it's probably not a good idea after read through the code. Really appreciate if you can suggest an alternative. |
ToolTask uses a private flag "_terminatedTool" to indicate the task execution timed out or cancelled. That flag should be reset on each execution, otherwise the return value of all following executions could be changed. Fixes dotnet#8541
I don't know there are Linux/Mac tests. Sorry for the unit test break in the first commit. I'm more than happy to make those work. The second commit should do the trick. |
It is possible to do this, by manually calling the constructor and property accessors of the task, including setting msbuild/src/Framework/ITask.cs Lines 14 to 18 in 1a6d753
and then calling |
Thanks for the information! It's good to know I can do that as well. |
ToolTask uses a private flag _terminatedTool to indicate the task execution timed out or cancelled. That flag should be reset on each execution, otherwise the return value of all following executions could be changed. Fixes #8541 Context When the same ToolTask instance being executed multiple times (by derived classes, for example), the return status of all following executions might be wrong (false) if the first execution timed-out or cancelled. Such case may arise if user try to run an external tool with retry and timeout. The problem is the internal _terminatedTool flag has not been reset on each execution. Reset that flag on each execution solved the problem. Changes Made Resets the internal flag ToolTask._terminatedTool on each task execution. Testing The following unit test has been added: Microsoft.Build.UnitTests.ToolTaskThatTimeoutAndRetry. It has 3 cases that verify no repeated execution, repeated execution with or without timeout. It's the last case that has been fixed, the rest is for regression. All ToolTask unit tests passed. Notes The Timeout setting in the unit test might be tricky. On slow hardware you may want to set that to a larger value; The unit test needs PowerShell to run, only Windows platform considered.
Issue Description
In the following code:
https://github.com/dotnet/msbuild/blob/main/src/Utilities/ToolTask.cs
The private field
_terminatedTool
has not be initialized in methodExecuteTool()
. As a result, in rare cases where:ToolTask.Execute()
being called multiple times from derived classes, and;Such timed-out state will be incorrectly permanent, impacting all following
ToolTask.Execute()
calls, and forcing the method to returnfalse
even if the execution succeeded.The problem can be easily addressed by reinitializing
_terminatedTool
field tofalse
in methodExecuteTool()
, along with other private fields.Thanks!
Steps to Reproduce
ToolTask
derived class to execute that tool;Execute()
, callingbase.Execute()
multiple times to execute the tool multiple times;Timeout
of the task to 5s, so the first call will timed out, and subsequent ones won't;base.Execute()
returnsfalse
, so the task never succeeds.Expected Behavior
The first execution timed out (and fail), but subsequent execution should succeed.
Actual Behavior
After the first execution timed out, all subsequent execution failed even if they actually succeeded.
Analysis
Private field
_terminatedTool
has not been reinitialized correctly inToolTask.ExecuteTool()
method.Versions & Configurations
The latest version (17.5).
The text was updated successfully, but these errors were encountered: