Skip to content

Commit eae1176

Browse files
authored
Merge pull request #84 from bonsai-rx/transport-dispose
Ensure transport is closed on shutdown IO errors
2 parents 76a05d7 + a71c997 commit eae1176

File tree

2 files changed

+25
-22
lines changed

2 files changed

+25
-22
lines changed

Bonsai.Harp/Bonsai.Harp.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
<GenerateDocumentationFile>true</GenerateDocumentationFile>
88
<TargetFrameworks>net462;netstandard2.0</TargetFrameworks>
99
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
10-
<VersionPrefix>3.5.1</VersionPrefix>
10+
<VersionPrefix>3.5.2</VersionPrefix>
11+
<VersionSuffix></VersionSuffix>
1112
</PropertyGroup>
1213

1314
<ItemGroup>

Bonsai.Harp/Device.cs

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using System.Xml.Serialization;
88
using System.Threading.Tasks;
99
using System.Threading;
10+
using System.IO;
1011

1112
namespace Bonsai.Harp
1213
{
@@ -255,6 +256,26 @@ static IObservable<string> GetDeviceName(string portName, LedState ledState, Led
255256
.FirstAsync();
256257
}
257258

259+
private void CloseTransport(SerialTransport transport)
260+
{
261+
try
262+
{
263+
var writeOpCtrl = OperationControl.FromPayload(MessageType.Write, new OperationControlPayload(
264+
OperationMode.Standby,
265+
dumpRegisters: false,
266+
MuteReplies,
267+
VisualIndicators,
268+
OperationLed,
269+
Heartbeat));
270+
transport.Write(writeOpCtrl);
271+
}
272+
catch (Exception ex) when (ex is IOException || ex is InvalidOperationException || ex is ObjectDisposedException)
273+
{
274+
// ignore port IO errors
275+
}
276+
finally { transport.Close(); }
277+
}
278+
258279
/// <summary>
259280
/// Connects to the specified serial port and returns an observable sequence of Harp messages
260281
/// coming from the device.
@@ -265,18 +286,7 @@ public override IObservable<HarpMessage> Generate()
265286
return Observable.Create<HarpMessage>(async (observer, cancellationToken) =>
266287
{
267288
var transport = await CreateTransportAsync(observer, cancellationToken);
268-
return Disposable.Create(() =>
269-
{
270-
var writeOpCtrl = OperationControl.FromPayload(MessageType.Write, new OperationControlPayload(
271-
OperationMode.Standby,
272-
dumpRegisters: false,
273-
MuteReplies,
274-
VisualIndicators,
275-
OperationLed,
276-
Heartbeat));
277-
transport.Write(writeOpCtrl);
278-
transport.Close();
279-
});
289+
return Disposable.Create(() => CloseTransport(transport));
280290
});
281291
}
282292

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

300310
return Disposable.Create(() =>
301311
{
302-
var writeOpCtrl = OperationControl.FromPayload(MessageType.Write, new OperationControlPayload(
303-
OperationMode.Standby,
304-
dumpRegisters: false,
305-
MuteReplies,
306-
VisualIndicators,
307-
OperationLed,
308-
Heartbeat));
309-
transport.Write(writeOpCtrl);
310312
sourceDisposable.Dispose();
311-
transport.Close();
313+
CloseTransport(transport);
312314
});
313315
});
314316
}

0 commit comments

Comments
 (0)