Skip to content
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

Fatal error. Internal CLR error. (0x80131506) when read data #84

Closed
wankey opened this issue Feb 19, 2024 · 9 comments
Closed

Fatal error. Internal CLR error. (0x80131506) when read data #84

wankey opened this issue Feb 19, 2024 · 9 comments

Comments

@wankey
Copy link

wankey commented Feb 19, 2024

  while (controller.isConnected)
        {
            try
            {
                var data = controller.HidDevice.Read(32);
                Thread.Sleep(1);
            }
            catch (HidException)
            {
                controller.isConnected = false;
            }
        }

after long time running, Internal CLR error. occurred.

Fatal error. Internal CLR error. (0x80131506)
at HidApi.Device.Read(Int32)
at ControllerSDK.hardware.HidManager._readDataFromDevice(System.Object)
at System.Threading.Thread.StartCallback()

.net version: 8.0.101 and 6.0.321 BOTH Tried

@badcel
Copy link
Owner

badcel commented Feb 19, 2024

Hey,

Which operating system are you using?

Can you try to run this code in a minimal console app without using any threads and show the code here?

If the problem persists in your minimal sample I would recommend writing a minimal C application as I would assume the same problem is present there.

@wankey
Copy link
Author

wankey commented Feb 20, 2024

MacOS 14.3.1 and Windows 11 (22H2) both occurred problem.

run without thread also occurred.

I will try writing a c application, but it's seems maybe a dot net runtime error caused by MemoryMarshal.GetReference(data) in Read METHOD, i'm not sure.

dotnet/runtime#83027

UPDATE:
I recreate a minimal sample project just running Read from certain device, It works fine. I don't know what's happen and why.

@badcel
Copy link
Owner

badcel commented Feb 20, 2024

I have one idea which could be verified. Please do the following:

  • Clone the HidApi.Net repository on your development machine
  • Update the Internal/NativeMethods.cs file with the following starting in line 49:
public static unsafe int Read(DeviceSafeHandle device, ReadOnlySpan<byte> data)
{
    fixed (byte* ptr = data)
    {
        return Read(device, ptr, (nuint) data.Length);   
    }
}
  • Update the Internal/NativeMethods.cs file with the following in Line 123/124
[LibraryImport(Library, EntryPoint = "hid_read")]
private static unsafe partial int Read(DeviceSafeHandle device, byte* data, nuint length);
  • Update the Internal/NativeMethods.Net6.cs file with the following in Line 32/33
[DllImport(Library, EntryPoint = "hid_read")]
private static extern unsafe int Read(DeviceSafeHandle device, byte* data, nuint length);
  • Compile the HidApi.Net project
  • Include the HidApi.Net project as a ProjectRefernece in your application csproj file
  • Verify if the problem persists

@wankey
Copy link
Author

wankey commented Feb 21, 2024

I have one idea which could be verified. Please do the following:

  • Clone the HidApi.Net repository on your development machine
  • Update the Internal/NativeMethods.cs file with the following starting in line 49:
public static unsafe int Read(DeviceSafeHandle device, ReadOnlySpan<byte> data)
{
    fixed (byte* ptr = data)
    {
        return Read(device, ptr, (nuint) data.Length);   
    }
}
  • Update the Internal/NativeMethods.cs file with the following in Line 123/124
[LibraryImport(Library, EntryPoint = "hid_read")]
private static unsafe partial int Read(DeviceSafeHandle device, byte* data, nuint length);
  • Update the Internal/NativeMethods.Net6.cs file with the following in Line 32/33
[DllImport(Library, EntryPoint = "hid_read")]
private static extern unsafe int Read(DeviceSafeHandle device, byte* data, nuint length);
  • Compile the HidApi.Net project
  • Include the HidApi.Net project as a ProjectRefernece in your application csproj file
  • Verify if the problem persists

I have make a sample to try this, but the problem persists.

after some test, maybe the problem is not caused by HidApi.Net.

    private void UpdateDataCommunicationThread(Device device)
    {
        var childThread = new Thread(_readDataFromDevice);
        childThread.Start(device);
    }

the call method as below works fine. but if uncomment these code,first code especially, the problem persists again.
I'm not familiar with c#, so don't known what's happened here.

var device = hid.ConnectToDevice();
// var controller = new Controller(device)
// {
//     isConnected = true
// };
// _connectedControllers.Add(controller);
// OnHidChangedListener?.OnHidChanged(true, controller);
UpdateDataCommunicationThread(device);

@badcel
Copy link
Owner

badcel commented Feb 21, 2024

To help you I need some minimal compileable sample code. Some snippets don't help.

The lines you mentioned look like some custom code from your own project so the problem seems to be caused by your own code.

@wankey
Copy link
Author

wankey commented Feb 21, 2024

because it's not a library fault, so the issue closed.

thanks for you help

@wankey wankey closed this as completed Feb 21, 2024
@AdrianEggenberger
Copy link

I'm seeing this issue also in my project when we have a situation where we have parallel read/write operation with the HID device. As we had no issues with another HID library I think it's not an application fault in my case. As it is exactly the same issue as above I decided to add my post to this already closed isse.

I wan't to change to this library in the future becuase our currently used HID library has some issues and the project is abandond. In addition I like the approach of using the hidapi library in behind as this works also on .NET on Linux (tested already witha Raspberry Pi).

I'm aware that debugging such an issue that happens on other systems is nearly impossible. So I'll investigate the issue myself or try to provide a demo project that allows reproduction.

The issue occured with the latest 1.0.3 release of the library. After I've found this issue here I did the steps you proposed above. I replaced the NuGet package with the code from the repository and changed the pointer handling as proposed. The issue did not disappear by this change.

Another thing I've tried is to block the read/write calls to the library so that no threding issue should occur.

My next step would be to simplify the case where it happens, as by now it requires our full application and a specific HID device that is not commercialy available.

Question: Do you know a good HID device that allows testing things? What devices are you using for tests? That will allow me to use the same devices. Any ideas or hints what to try are very welcome.

@badcel
Copy link
Owner

badcel commented Jun 10, 2024

@AdrianEggenberger It is unlikely (but not impossible) that the issue is based in this library as I just call the native methods: No threading is involved by HidApi.Net.

Please be aware that HIDAPI is not thread safe.

So if you experience issues please create a minimal sample calling the C HIDAPI directly via PInvoke where the issue is not occurring to be sure that there is some issue which can be fixed as part of this project.

If needed I can try to assist you with my PInvoke knowledge but I cant write all the code for you or do the testing.

Then I can check what is different between your working sample and this project to fix the problem.

I don't have a specific test device.
During my search for an app I found that there was no modern C# cross platform library available. So I wrote this library to be able to communicate with my Mouse under Linux. So my testing is extremely limited.

Any actionable feedback is very welcome to improve this library.

If you want to work on this I kindly ask you to open a new discussion and we can try to narrow down the problem there.

@AdrianEggenberger
Copy link

AdrianEggenberger commented Jun 11, 2024 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants