powershell: [BUG] Azure Function PowerShell: Random "The object is used in the context different from the one associated with the object." errors

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: https://github.com/pnp/powershell/pull/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?

  • Windows
  • Linux
  • MacOS
  • Azure Cloud Shell
  • Azure Functions
  • Other : please specify

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Comments: 19 (2 by maintainers)

Most upvoted comments

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.

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.

Never mind @heinrich-ulbricht, adding it to the code already 😃

A fixed version is running fine in our environment.

Those fixes were needed:

  • PnP.PowerShell: for PnPWebCmdlet replace every PnPConnection.Current with Connection
  • PnP.PowerShell: currently this fix for Remove-PnPListItem is needed: 1959
  • my PowerShell script: do one Connect-PnPOnline without -ReturnConnection to set PnPConnection.Current so that PnPGraphCmdlet can work

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 -Connection parameter. This kind of error now immediately throws when you don’t have a default connection. So I assume some PnP PowerShell users that use explicit connections are up for a surprise as missing Connection parameters are not masked anymore. Since now there really is no default connection.

fyi @KoenZomers @gautamdsheth

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.

@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: image

😄 Or my assumption is wrong as to which branch the nightly build is based on.