-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Thread-Safe Tasks spec #12111
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
Merged
Merged
Thread-Safe Tasks spec #12111
Changes from 12 commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
e762eae
Add api reference document 1st version.
AR-May 2f31d7b
Address review comments, fix typos
AR-May 0914b4e
Add first draft for IThreadSafeTask interface.
AR-May 6415ea9
address comments; introduce AbsolutePath classes
AR-May a2df50f
Process API changes
AR-May 0697dd9
Add more options, address review comments.
AR-May 69dfc1d
Add some questions, minor refactoring
AR-May cab9556
small fixes
AR-May 0dba237
Rename directory
AR-May 349e849
Merge branch 'main' into dev/AR-May/thread-safe-tasks-spec
AR-May 6c2b55e
Add more details about APIs
AR-May ac5fffa
small change of wordings
AR-May 52243b2
Make absolute and relative structs read-only and prohibit ToAbsoluteP…
AR-May 9e0a088
Apply review suggestions
AR-May 03e40af
naming changes and small wording changes
AR-May 4135f9d
small wording changes
AR-May 0a6c100
rename ProjectDirectory -> ProjectCurrentDirectory
AR-May 9170036
mark API reference a draft
AR-May 5b154f3
rename ProjectCurrentDirectory -> ProjectDirectory and update access …
AR-May b23daf7
Merge branch 'main' into dev/AR-May/thread-safe-tasks-spec
AR-May File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
154 changes: 154 additions & 0 deletions
154
documentation/specs/multithreading/thread-safe-tasks-api-analysis.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,154 @@ | ||
| # Thread-Safe Tasks: API Analysis Reference | ||
|
|
||
| This document provides a list of .NET APIs that should not be used or should be used with caution in thread-safe tasks. These APIs are problematic because they either rely on or modify process-level state, which can cause race conditions in multithreaded execution. | ||
|
|
||
| The APIs listed in this document will be detected by Roslyn analyzers and/or MSBuild BuildCheck to help identify potential threading issues in tasks that implement `IThreadSafeTask`. | ||
|
|
||
| **Note**: The analyzers rely on **static code analysis** and may not catch all dynamic scenarios (such as reflection-based API calls). | ||
|
|
||
| ## API Issues Categories | ||
|
|
||
| Categories of threading issues with .NET API usage in thread-safe tasks to be aware of: | ||
|
|
||
| 1. **Working Directory Modifications and Usage**, such as file system operations with relative paths. | ||
| 1. **Environment Variables Modification and Usage** | ||
| 1. **Process Culture Modification and Usage**, which can affect data formatting. | ||
| 1. **Assembly Loading** | ||
| 1. **Static Fields** | ||
|
|
||
| **TODO**: Consider other possible causes of issues: | ||
| 1. Creating new AppDomain. | ||
| 1. App config changes. | ||
|
|
||
| ### Best Practices | ||
|
|
||
| Instead of the problematic APIs listed below, thread-safe tasks should: | ||
|
|
||
| 1. **Use `ITaskExecutionContext`** for all file system operations, environment variable changes, and working directory changes. | ||
| 1. **Always use absolute paths** when still using some standard .NET file system APIs. | ||
| 1. **Explicitly configure external processes** with working directory and environment variables. | ||
| 1. **Never modify process culture**: Avoid modifying culture defaults. | ||
| ### Additional Considerations | ||
|
|
||
| ## Detailed API Reference | ||
|
|
||
| The following tables list specific .NET APIs and their threading safety classification: | ||
|
|
||
| ### System.IO.Path Class | ||
|
|
||
| | API | Level | Short Reason | Recommendation | | ||
| |-----|-------|--------------|-------| | ||
| | `Path.GetFullPath(string path)` | ERROR | Uses current working directory | Use MSBuild API | | ||
|
|
||
| **TODO**: Check other methods and exclude any indirect dependency on working directory. | ||
|
|
||
| ### System.IO.File Class | ||
|
|
||
| | API | Level | Short Reason | Recommendation | | ||
| |-----|-------|--------------|-------| | ||
| | All methods | ERROR | Uses current working directory | Use absolute paths | | ||
|
|
||
| ### System.IO.Directory Class | ||
|
|
||
| | API | Level | Short Reason | Recommendation | | ||
| |-----|-------|--------------|-------| | ||
| | All methods | ERROR | Uses current working directory | Use absolute paths | | ||
|
|
||
| ### System.Environment Class | ||
|
|
||
| | API | Level | Short Reason | Recommendation | | ||
| |-----|-------|--------------|-------| | ||
| | All properties setters | ERROR | Modifies process-level state | Use MSBuild API | | ||
| | `Environment.CurrentDirectory` (getter, setter) | ERROR | Accesses process-level state | Use MSBuild API | | ||
| | `Environment.Exit(int exitCode)` | ERROR | Terminates entire process | Return false from task or throw exception | | ||
| | `Environment.FailFast` all overloads | ERROR | Terminates entire process | Return false from task or throw exception | | ||
| | All other methods | ERROR | Modifies process-level state | Use MSBuild API | | ||
|
|
||
| ### System.IO.FileInfo Class | ||
|
|
||
| | API | Level | Short Reason | Recommendation | | ||
| |-----|-------|--------------|-------| | ||
| | Constructor `FileInfo(string fileName)` | WARNING | Uses current working directory | Use absolute paths | | ||
| | `CopyTo` all overloads | WARNING | Destination path relative to current directory | Use absolute paths | | ||
| | `MoveTo` all overloads | WARNING | Destination path relative to current directory | Use absolute paths | | ||
| | `Replace` all overloads | WARNING | Paths relative to current directory | Use absolute paths | | ||
|
|
||
| ### System.IO.DirectoryInfo Class | ||
|
|
||
| | API | Level | Short Reason | Recommendation | | ||
| |-----|-------|--------------|-------| | ||
| | Constructor `DirectoryInfo(string path)` | WARNING | Uses current working directory | Use absolute paths | | ||
| | `MoveTo(string destDirName)` | WARNING | Destination path relative to current directory | Use absolute paths | | ||
|
|
||
| ### System.IO.FileStream Class | ||
|
|
||
| | API | Level | Short Reason | Recommendation | | ||
| |-----|-------|--------------|-------| | ||
| | Constructor `FileStream` all overloads | WARNING | Uses current working directory | Use absolute paths | | ||
|
|
||
| ### System.IO Stream Classes | ||
|
|
||
| | API | Level | Short Reason | Recommendation | | ||
| |-----|-------|--------------|-------| | ||
| | Constructor `StreamReader` all overloads | WARNING | Uses current working directory | Use absolute paths | | ||
|
AR-May marked this conversation as resolved.
|
||
|
|
||
| ### System.Diagnostics.Process Class | ||
|
|
||
| | API | Level | Short Reason | Recommendation | | ||
| |-----|-------|--------------|-------| | ||
| | All properties setters | ERROR | Modifies process-level state | Avoid | | ||
| | `Process.GetCurrentProcess().Kill()` | ERROR | Terminates entire process | Avoid | | ||
| | `Process.GetCurrentProcess().Kill(bool entireProcessTree)` | ERROR | Terminates entire process | Avoid | | ||
| | `Process.Start` all overloads | ERROR | May inherit process state | Use MSBuild API | | ||
|
|
||
| ### System.Diagnostics.ProcessStartInfo Class | ||
|
|
||
| | API | Level | Short Reason | Recommendation | | ||
| |-----|-------|--------------|-------| | ||
| | Constructor `ProcessStartInfo()` all overloads | ERROR | May inherit process state | Use MSBuild API | | ||
|
|
||
| ### System.Threading.ThreadPool Class | ||
|
|
||
| | API | Level | Short Reason | Recommendation | | ||
| |-----|-------|--------------|-------| | ||
| | `ThreadPool.SetMinThreads(int workerThreads, int completionPortThreads)` | ERROR | Modifies process-wide settings | Avoid | | ||
| | `ThreadPool.SetMaxThreads(int workerThreads, int completionPortThreads)` | ERROR | Modifies process-wide settings | Avoid | | ||
|
|
||
| ### System.Globalization.CultureInfo Class | ||
|
|
||
| | API | Level | Short Reason | Recommendation | | ||
| |-----|-------|--------------|-------| | ||
| | `CultureInfo.DefaultThreadCurrentCulture` (setter) | ERROR | Affects new threads | Modify the thread culture instead | | ||
| | `CultureInfo.DefaultThreadCurrentUICulture` (setter) | ERROR | Affects new threads | Modify the thread culture instead | | ||
|
|
||
| ### Static | ||
|
|
||
| | API | Level | Short Reason | Recommendation | | ||
| |-----|-------|--------------|-------| | ||
| | Static fields | WARNING | Shared across threads, can cause race conditions | Avoid | | ||
|
|
||
| ### Assembly Loading (System.Reflection.Assembly class, System.Activator class) | ||
| Tasks that load assemblies dynamically in the task host may cause version conflicts. Version conflicts in task assemblies will cause build failures (previously these might have been sporadic). Both dynamically loaded dependencies and static dependencies can cause issues. | ||
|
|
||
| | API | Level | Short Reason | Recommendation | | ||
| |-----|-------|--------------|-------| | ||
| | `Assembly.LoadFrom(string assemblyFile)` | WARNING | May cause version conflicts | Be aware of potential conflicts, use absolute paths | | ||
| | `Assembly.LoadFile(string path)` | WARNING | May cause version conflicts | Be aware of potential conflicts | | ||
| | `Assembly.Load` all overloads | WARNING | May cause version conflicts | Be aware of potential conflicts | | ||
| | `Assembly.LoadWithPartialName(string partialName)` | WARNING | May cause version conflicts | Be aware of potential conflicts | | ||
| | `Activator.CreateInstanceFrom(string assemblyFile, string typeName)` | WARNING | May cause version conflicts | Be aware of potential conflicts | | ||
| | `Activator.CreateInstance(string assemblyName, string typeName)` | WARNING | May cause version conflicts | Be aware of potential conflicts | | ||
| | `AppDomain.Load` all overloads | WARNING | May cause version conflicts | Be aware of potential conflicts | | ||
| | `AppDomain.CreateInstanceFrom(string assemblyFile, string typeName)` | WARNING | May cause version conflicts | Be aware of potential conflicts | | ||
| | `AppDomain.CreateInstance(string assemblyName, string typeName)` | WARNING | May cause version conflicts | Be aware of potential conflicts | | ||
|
|
||
| ### P/Invoke | ||
|
|
||
| **Concerns**: | ||
| - P/Invoke calls may use process-level state like current working directory | ||
| - Native code may not be thread-safe | ||
| - Native APIs may modify global process state | ||
|
|
||
| | API | Level | Short Reason | Recommendation | | ||
| |-----|-------|--------------|-------| | ||
| | `[DllImport]` attribute | WARNING | Not covered by analyzers | Review for thread safety, use absolute paths | | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.