Update PowerShell telemetry to respect the diagnostics and feedback setting on Windows by daxian-dbw · Pull Request #27328 · PowerShell/PowerShell · GitHub
Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions src/System.Management.Automation/CoreCLR/CorePsPlatform.cs
Original file line number Diff line number Diff line change
Expand Up @@ -179,9 +179,12 @@ public static bool IsStaSupported
{
int result = Interop.Windows.CoInitializeEx(IntPtr.Zero, Interop.Windows.COINIT_APARTMENTTHREADED);

// If 0 is returned the thread has been initialized for the first time
// as an STA and thus supported and needs to be uninitialized.
if (result > 0)
// Per COM documentation: Each successful call to CoInitializeEx (including S_FALSE)
// must be balanced by a corresponding call to CoUninitialize.
// - S_OK (0) means we initialized for the first time.
// - S_FALSE (1) means already initialized, but still increments the reference count.
// Both require CoUninitialize to decrement the reference count.
if (result >= 0)
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This fix is not related to the feature. I found it when reading the code. The requirement for balancing the call of CoInitializeEx is documented in https://learn.microsoft.com/en-us/windows/win32/api/combaseapi/nf-combaseapi-coinitializeex#remarks. I quote it below:

To uninitialize the COM library gracefully on a thread, each successful call to CoInitialize or CoInitializeEx, including any call that returns S_FALSE, must be balanced by a corresponding call to CoUninitialize.

{
Interop.Windows.CoUninitialize();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,9 @@ internal enum PSEventId : int
ExperimentalFeature_InvalidName = 0x3001,
ExperimentalFeature_ReadConfig_Error = 0x3002,

// Windows Diagnostics And Usage Data Settings
Telemetry_Setting_Error = 0x3011,

// Scheduled Jobs
ScheduledJob_Start = 0xD001,
ScheduledJob_Complete = 0xD002,
Expand Down Expand Up @@ -240,6 +243,7 @@ internal enum PSTask : int
ProviderStop = 0x69,
ExecutePipeline = 0x6A,
ExperimentalFeature = 0x6B,
Telemetry = 0x6C,
ScheduledJob = 0x6E,
NamedPipe = 0x6F,
ISEOperation = 0x78,
Expand Down
10 changes: 9 additions & 1 deletion src/System.Management.Automation/utils/Telemetry.cs
Loading
Loading