Connection disposal (C#) hangs if the connection was recently used
If you have an Connection
object and perform an async
request on it (at least some of them) but then attempt to Dispose()
of the Connection
immediately thereafter, then that disposal will hang indefinitely.
I can't say how many functions behave this way. DetectDevices
was the first I tried (per the example code) and I ran into this issue.
Steps to replicate:
- in VisualStudio make a new
.Net 8
console program - add the
Zaber.Motion
library from NuGet - Use the code below.
Commenting / uncommenting out the delay will cause the disposal to hang / not-hang respectively.
class Program
{
static async Task Main(string[] _)
{
using( Zaber.Motion.Ascii.Connection connection = Zaber.Motion.Ascii.Connection.OpenSerialPort("COM4"))
{
Console.WriteLine("Made ZaberAscii serial connection");
var num = await connection.DetectDevicesAsync();
Console.WriteLine($"Found {num.Length} devices");
// this will hang at disposal, unless we add a delay
//await Task.Delay(1);
}
}
}