SQLServerPSModule icon indicating copy to clipboard operation
SQLServerPSModule copied to clipboard

Using Invoke-SqlCmd in ForEach-Object -Parallel loop - not threadsafe?

Open jplee75 opened this issue 1 year ago • 3 comments

It appears that Invoke-SqlCmd is not threadsafe - is this correct? Can it be made to be so?

Got this error while executing in a ForEach-Object -Parallel type loop

Operations that change non-concurrent collections must have exclusive access. A concurrent update was performed on this collection and corrupted its state. The collection's state is no longer correct. ##[error]PowerShell exited with code '1'.

Version Info Name Value


PSVersion 7.4.0 PSEdition Core GitCommitId 7.4.0 OS Ubuntu 22.04.3 LTS Platform Unix PSCompatibleVersions {1.0, 2.0, 3.0, 4.0…} PSRemotingProtocolVersion 2.3 SerializationVersion 1.1.0.1 WSManStackVersion 3.0

Name : SqlServer Path : /home/vsts/.local/share/powershell/Modules/SqlServer/22.1.1 /SqlServer.psd1 Version : 22.1.1

jplee75 avatar Dec 26 '23 15:12 jplee75

I know I posted this over the end of the year - is there more information I could provide to be helpful or possible to get an answer if Invoke-SqlCmd should be safe or not to use with For-Each Object -Parallel ?

jplee75 avatar Jan 08 '24 19:01 jplee75

Hi @jplee75 - I believe you are correct and this is a fairly old issue (it predates having this repo where we collect the issues).

For now and until I can take a look and fix it, I would recomment to stay away from it.

Matteo-T avatar Jan 16 '24 08:01 Matteo-T

I'm able to reproduce the issue with version 22.2.0 of the SQLServer module on PowerShell version 7.4.2 and Microsoft Windows 10.0.19045.

ZianAtFirstWatch avatar May 15 '24 16:05 ZianAtFirstWatch