-
Notifications
You must be signed in to change notification settings - Fork 359
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] Azure Function PowerShell: Random "The object is used in the context different from the one associated with the object." errors #1949
Comments
Maybe you can use the nightly builds and test that out ? |
@gautamdsheth Will do! But have to wait for #1919 (comment) |
@heinrich-ulbricht #1919 has already been merged last week. Please download the latest nightly and upload it to your Azure Function and test it against this. Fair chance this issue will be resolved in the nightly build already. |
@KoenZomers Unfortunately I need #1958 - I will test the nightly as soon as this is merged. Eagerly waiting like on heiße Kohlen. |
I'm confused :) #1958 has also been merged last night, so should be in the current prerelease already. |
@KoenZomers At the time of writing the pull request was display as "Open" here, thus my assumption it still has to be merged. It now shows as merged. Strange. But cool! Thank you so much! I think I'll wait for another nightly to be sure and will put it together over the weekend. This is perfect timing since our provisioning solution (that heavily heavily depends on PnP PowerShell) will go into full force the coming week. Edit: not so strange looking at the history: :D Or my assumption is wrong as to which branch the nightly build is based on. |
Curious to hear about your findings. Sounds like it will be a nice test case for PnP PowerShell to see if everything still works well. The changes I've done through the two PRs mentioned in this thread can have a potential high impact. I think in the end it makes things much more reliable and stable, but it might be a bit bumpy road to get to that point with scripts that are currently running. Let's see how things go. |
@KoenZomers Something is not ok. The script that used to work chokes on the first use of a connection. I pulled the current source from dev, built it, attached a debugger and it fails here:
Connection is done via:
PnP Call that triggers the exception shown in the screenshot:
That's it. PowerShell 7.2 Edit: Edit2: |
Would need to test it with the exact connect you're using. The if has been placed there deliberately. The idea is that if you would use ReturnConnection that it no longer also has an impact on the current connection which you can make without using -ReturnConnection. Why else would you use the parameter anyway if that's the desired result? Only the returned variable would be that connection. It surprises me though that in your sample you do seem to use it correctly. So it can either be something odd with specifically that connection method or with those type of cmdlets. |
With the latest nightly it just fails with the first call to
Above code triggers the use of @KoenZomers Is Edit: But there is more use of Edit2: Edit3: But now I'm speculating and rather wait for your take on this. Edit4: (or not) Edit5: |
A fixed version is running fine in our environment. Those fixes were needed:
Two "wrong context" errors and connection-related errors that I experienced in the past seem gone since using this new version. One of the "wrong context" errors was in my script, that was missing one |
@heinrich-ulbricht also thanks for sharing your findings here. Point 1 and 3 sound perfect. Would you mind creating a PR for number 1? Happily merge it. |
Never mind @heinrich-ulbricht, adding it to the code already :) |
For point 3, trying to see if I can make it so that every PnP cmdlet supports passing in an optional -Connection so things become really flexible and consistent. |
I think I have succeeded in tackling point 3 with PR #1982. I would appreciate it if everyone can give the latest nightly an extensive test once this PR has been merged to see if there will not be side effects to this major refactoring. |
@KoenZomers Thank you for the pull request! This was faster and more comprehensive than I could've done it since the project where I need this fixed is consuming all time at the moment. The bug costs time but there is no time to properly fix the bug. As always, the circle. That's also why I won't test the nightly right away in that project, but with the next PowerShell project that comes up, which happens regularly. Thanks, and wow that is a lot of changes. Hoisting the connection one level up feels right. |
The unexpected behavior of Disconnect-PnPOnline -Connection has great impact on scripts that work with multiple connections and on hosts that run multiple scripts instances like Azure Automation. |
@jackpoz You're right, this should be given more visibility. Let us discuss options for doing so. Note that specifically the |
This is exactly the part that I was referring too, as it affects any PnP PowerShell version. The instructions at https://pnp.github.io/powershell/cmdlets/Disconnect-PnPOnline.html#description are now very clear about how to disconnect, what's left to do is to update all developers who have used Btw nice job with the connection cleanup work you did 👍 |
Reporting an Issue or Missing Feature
Issue
Expected behavior
I expect working with Connections returned via
-ReturnConnection:true
and set via-Connection
to work reliably.Actual behavior
I've got an Azure Function running PowerShell where I use PnP PowerShell extensively. Parallel PowerShell worker processes are possible, if that matters.
In my script I get sporadic errors:
System.InvalidOperationException: The object is used in the context different from the one associated with the object.
Where the errors happen they don't make sense.Steps to reproduce behavior
Unfortunately I cannot reproduce this reliably and it's happening in production. Errors go to the log which reliably shows the line number where the error is happening. It might be something mundane like:
Get-PnPListItem -List "ListName" -Connection $Connection -Query "[....]"
The script uses multiple connections but should only have one active at the time the error happens. That makes it even stranger.
So far I have not good theory about what's causing this. It seems random. The Azure function is configured with FUNCTIONS_WORKER_PROCESS_COUNT==2, but processes are isolated, right? If not, parallelism might be a cause.
While searching for solutions I came across those:
Maybe this would even be solved by this: #1919 ?
What is the version of the Cmdlet module you are running?
PnP.PowerShell 1.10.0
Which operating system/environment are you running PnP PowerShell on?
The text was updated successfully, but these errors were encountered: