PowerShell / PowerShell-IoT

Interact with I2C, SPI & GPIO devices using PowerShell Core!
https://www.powershellgallery.com/packages/Microsoft.PowerShell.IoT
MIT License
129 stars 28 forks source link

Get-I2CRegister or Set-I2CRegister crash PowerShell if device is unreachable #29

Open jnury opened 6 years ago

jnury commented 6 years ago

Hi,

If the device provided to Get or Set-I2CRegister commands is not reachable, PowerShell crash with message:

*** Error in `pwsh': free(): invalid pointer: 0x76d35694 ***
Aborted

To reproduce (do not connect anything on I2C bus):

$Device = Get-I2CDevice -ID 0x54 -FriendlyName 'accelerometer'
Get-I2CRegister -Device $Device -Register 0x00

PowerShell version:

Name                           Value
----                           -----
PSVersion                      6.0.2
PSEdition                      Core
GitCommitId                    v6.0.2
OS                             Linux 4.14.30-v7+ #1102 SMP Mon Mar 26 16:45:49 BST 2018
Platform                       Unix
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0
TylerLeonhardt commented 6 years ago

Yep, this looks like a PowerShell bug. PowerShell should never crash. Can you supply the exception that is spit out? Or is the first message all you get?

jnury commented 6 years ago

The first message is all I get.

TylerLeonhardt commented 6 years ago

Interesting. Ok! Let me see if I can get a little bit more info during a repro and then we'll move this over to PowerShell/PowerShell as I doubt we'll be able to catch the exception.

Once we're able to do so, we can handle a NoRegisterFoundException

TylerLeonhardt commented 6 years ago

This could also be aproblem with .NET Core itself.

thomasrayner commented 6 years ago

I can apparently replicate this (or at least a very similar) issue on PowerShell 6.1.0-preview.1, and version 0.1.1 of the Microsoft.PowerShell.IoT module. The same error described above by @jnury happens if I try to get the register, but I get a different error just getting the device.

Import-Module Microsoft.PowerShell.IoT
Get-I2CDevice -Id 0x46 #where 0x46 is the correct ID of my device

pwsh will crash and throw the error Unable to select I2C device: Device or resource busy.

Interestingly, if I do a Get-I2CDevice for an ID that is just made up, it will simply return an I2CDevice object with whatever FriendlyName and Id values you pass it.

Also, you can crash all the way out of pwsh another way that I found out by accident while verifying the last point.

Import-Module Microsoft.PowerShell.IoT
Get-I2CDevice -Id 911 #Or some other value that isn't valid

pwsh will crash, throwing the error Unable to select I2C device: Invalid argument.

TylerLeonhardt commented 6 years ago

Thanks for the info @ThmsRynr!

anmenaga commented 6 years ago

Original PS crash with free(): invalid pointer error is coming from Unosquare.RaspberryIO.dll The line 26 ExtendedMessage = Standard.StrError(errorCode); is calling 'strerror' function from 'libc' library passing 0 (that it gets from Marshal.GetLastWin32Error()) as an argument. This should be fixed in Unosquare.RaspberryIO code (I've tried passing -1 as argument as well as commenting out the problem line - both fixed the crash). I'm not sure how we can workaround this at our layer.

TylerLeonhardt commented 6 years ago

@anmenaga Can we open an issue on them? Honestly, it sounds like an easy enough fix that we could even contribute back 👍

DanielSSilva commented 6 years ago

as stated on #37 , this code is causing that very same error, although I can't find a pattern for that (aka, the exception is random)