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

Ensure transport is closed on shutdown IO errors #84

Merged
merged 1 commit into from
Mar 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Bonsai.Harp/Bonsai.Harp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<TargetFrameworks>net462;netstandard2.0</TargetFrameworks>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<VersionPrefix>3.5.1</VersionPrefix>
<VersionPrefix>3.5.2</VersionPrefix>
<VersionSuffix></VersionSuffix>
</PropertyGroup>

<ItemGroup>
Expand Down
44 changes: 23 additions & 21 deletions Bonsai.Harp/Device.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Xml.Serialization;
using System.Threading.Tasks;
using System.Threading;
using System.IO;

namespace Bonsai.Harp
{
Expand Down Expand Up @@ -255,6 +256,26 @@ static IObservable<string> GetDeviceName(string portName, LedState ledState, Led
.FirstAsync();
}

private void CloseTransport(SerialTransport transport)
{
try
{
var writeOpCtrl = OperationControl.FromPayload(MessageType.Write, new OperationControlPayload(
OperationMode.Standby,
dumpRegisters: false,
MuteReplies,
VisualIndicators,
OperationLed,
Heartbeat));
transport.Write(writeOpCtrl);
}
catch (Exception ex) when (ex is IOException || ex is InvalidOperationException || ex is ObjectDisposedException)
{
// ignore port IO errors
}
finally { transport.Close(); }
}

/// <summary>
/// Connects to the specified serial port and returns an observable sequence of Harp messages
/// coming from the device.
Expand All @@ -265,18 +286,7 @@ public override IObservable<HarpMessage> Generate()
return Observable.Create<HarpMessage>(async (observer, cancellationToken) =>
{
var transport = await CreateTransportAsync(observer, cancellationToken);
return Disposable.Create(() =>
{
var writeOpCtrl = OperationControl.FromPayload(MessageType.Write, new OperationControlPayload(
OperationMode.Standby,
dumpRegisters: false,
MuteReplies,
VisualIndicators,
OperationLed,
Heartbeat));
transport.Write(writeOpCtrl);
transport.Close();
});
return Disposable.Create(() => CloseTransport(transport));
});
}

Expand All @@ -299,16 +309,8 @@ public IObservable<HarpMessage> Generate(IObservable<HarpMessage> source)

return Disposable.Create(() =>
{
var writeOpCtrl = OperationControl.FromPayload(MessageType.Write, new OperationControlPayload(
OperationMode.Standby,
dumpRegisters: false,
MuteReplies,
VisualIndicators,
OperationLed,
Heartbeat));
transport.Write(writeOpCtrl);
sourceDisposable.Dispose();
transport.Close();
CloseTransport(transport);
});
});
}
Expand Down