Skip to content

Commit

Permalink
Do not pass static zero IntPtr to epoll wait. Dispose driver in tests…
Browse files Browse the repository at this point in the history
… in all cases. (#2291)
  • Loading branch information
huesla authored Mar 18, 2024
1 parent 0b46056 commit ab3f910
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 11 deletions.
8 changes: 4 additions & 4 deletions src/System.Device.Gpio.Tests/GpioControllerTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ protected GpioControllerTestBase(ITestOutputHelper testOutputHelper)
[InlineData(false)]
public void PinValueStaysSameAfterDispose(bool closeAsHigh)
{
var driver = GetTestDriver();
using var driver = GetTestDriver();
if (driver is SysFsDriver)
{
// This check fails on the SysFsDriver, because it always sets the value to 0 when the pin is opened (but on close, the value does stay high)
Expand Down Expand Up @@ -181,7 +181,7 @@ public void ThrowsIfWritingClosedPin()
[Trait("SkipOnTestRun", "Windows_NT")] // Currently, the Windows Driver is defaulting to InputPullDown, and it seems this cannot be changed
public void OpenPinDefaultsModeToLastMode(PinMode modeToTest)
{
var driver = GetTestDriver();
using var driver = GetTestDriver();
if (driver is SysFsDriver)
{
// See issue #1581. There seems to be a library-version issue or some other random cause for this test to act differently on different hardware.
Expand Down Expand Up @@ -230,7 +230,7 @@ void Callback(object sender, PinValueChangedEventArgs pinValueChangedEventArgs)
[Fact]
public void AddCallbackFallingEdgeNotDetectedTest()
{
var driver = GetTestDriver();
using var driver = GetTestDriver();
if (driver is SysFsDriver)
{
// This test is unreliable (flaky) with SysFs.
Expand Down Expand Up @@ -317,7 +317,7 @@ void Callback(object sender, PinValueChangedEventArgs e)
[Fact]
public void AddCallbackRemoveAllCallbackTest()
{
GpioDriver testDriver = GetTestDriver();
using GpioDriver testDriver = GetTestDriver();
// Skipping the test for now when using the SysFsDriver or the RaspberryPi3Driver given that this test is flaky for those drivers.
// Issue tracking this problem is https://github.com/dotnet/iot/issues/629
if (testDriver is SysFsDriver || testDriver is RaspberryPi3Driver)
Expand Down
5 changes: 1 addition & 4 deletions src/System.Device.Gpio/Interop/UnmanagedArray.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ internal sealed class UnmanagedArray<T> : SafeHandle
private readonly int _arrayLength;
private readonly int _typeSize;

public static readonly UnmanagedArray<T> Empty = new(0);

public UnmanagedArray(int arrayLength)
: base(IntPtr.Zero, true)
{
Expand All @@ -21,7 +19,7 @@ public UnmanagedArray(int arrayLength)
}

_arrayLength = arrayLength;
_typeSize = Marshal.SizeOf(typeof(T));
_typeSize = Marshal.SizeOf<T>();
SetHandle(Marshal.AllocHGlobal(_typeSize * arrayLength));
}

Expand Down Expand Up @@ -58,4 +56,3 @@ public static implicit operator IntPtr(UnmanagedArray<T> unmanagedArray)
return !unmanagedArray.IsInvalid ? unmanagedArray.handle : throw new InvalidOperationException("Invalid handle");
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -456,7 +456,8 @@ private void AddPinToPoll(int pinNumber, ref int valueFileDescriptor, ref int po
}

// Ignore first time because it will always return the current state.
while (Interop.epoll_wait(pollFileDescriptor, UnmanagedArray<epoll_event>.Empty, 1, 0) == -1)
using var eventBuffer = new UnmanagedArray<epoll_event>(1);
while (Interop.epoll_wait(pollFileDescriptor, eventBuffer, 1, 0) == -1)
{
var errorCode = Marshal.GetLastWin32Error();
if (errorCode != ERROR_CODE_EINTR)
Expand All @@ -467,14 +468,15 @@ private void AddPinToPoll(int pinNumber, ref int valueFileDescriptor, ref int po
}
}

private unsafe bool WasEventDetected(int pollFileDescriptor, int valueFileDescriptor, out int pinNumber, CancellationToken cancellationToken)
private bool WasEventDetected(int pollFileDescriptor, int valueFileDescriptor, out int pinNumber, CancellationToken cancellationToken)
{
pinNumber = -1;

using var eventBuffer = new UnmanagedArray<epoll_event>(1);

while (!cancellationToken.IsCancellationRequested)
{
// Wait until something happens
using var eventBuffer = new UnmanagedArray<epoll_event>(1);
int waitResult = Interop.epoll_wait(pollFileDescriptor, eventBuffer, 1, PollingTimeout);
if (waitResult == -1)
{
Expand Down

0 comments on commit ab3f910

Please sign in to comment.