Azure / autorest.powershell

AutoRest PowerShell Generator
MIT License
110 stars 76 forks source link

Module will be created and inited many times in one session when `-Parallel` is used. #1291

Closed YanaXu closed 5 months ago

YanaXu commented 6 months ago

The Azure PowerShell Module is supposed to be created and initialized only once in one PS session. But when -Parallel is used, one module could be created and initialized many times. Autorest should handle this situation or it may cause errors.

Preparation for reproduce

The reproduce steps for case 1

PS azure-powershell\src\StreamAnalytics\StreamAnalytics.Autorest> ipmo .\Az.StreamAnalytics.psd1
[44432] [17] module constructor
[44432] [17] module init
PS azure-powershell\src\StreamAnalytics\StreamAnalytics.Autorest> 1..3 | % -Parallel { Get-AzStreamAnalyticsJob -ResourceGroupName 'test_rg' -Name 'sajob' }
[44432] [21] module init
[44432] [25] module init
[44432] [24] module init

******

The reproduce steps for case 2

PS azure-powershell\src\StreamAnalytics\StreamAnalytics.Autorest> 1..3 | % -Parallel { ipmo .\Az.StreamAnalytics.psd1; Get-AzStreamAnalyticsJob -ResourceGroupName 'test_rg' -Name 'sajob' }
[42592] [22] module constructor
[42592] [24] module constructor
[42592] [23] module constructor
[42592] [23] module init
[42592] [24] module init
[42592] [22] module init

******

The reproduce steps for case 3 - without Parallel but import the module with Force

PS azure-powershell\artifacts\Debug\Az.StreamAnalytics> ipmo ./Az.StreamAnalytics.psd1
[18520] [18] module constructor
[18520] [18] module Init
PS azure-powershell\artifacts\Debug\Az.StreamAnalytics> ipmo ./Az.StreamAnalytics.psd1
PS azure-powershell\artifacts\Debug\Az.StreamAnalytics> ipmo ./Az.StreamAnalytics.psd1 -Force
[18520] [18] module Init
PS azure-powershell\artifacts\Debug\Az.StreamAnalytics> ipmo ./Az.StreamAnalytics.psd1 -Force
[18520] [18] module Init
dolauli commented 6 months ago

Need to make following two calls only be executed once when executed in multiple threads.

https://github.com/Azure/azure-powershell/blob/5a43ae12cbaa8993b4bc690b33a681e2b36df829/src/Databricks/Databricks.Autorest/generated/Module.cs#L58

https://github.com/Azure/azure-powershell/blob/5a43ae12cbaa8993b4bc690b33a681e2b36df829/src/Databricks/Databricks.Autorest/generated/Module.cs#L118-L123

dolauli commented 5 months ago

Fixed in autorest.powershell v4.