Skip to content

Commit 00e7f98

Browse files
authored
Merge pull request #287 from open-ephys/shutdown-behavior
Improve the shutdown behavior
2 parents 5cd4f83 + e69f184 commit 00e7f98

File tree

2 files changed

+55
-14
lines changed

2 files changed

+55
-14
lines changed

OpenEphys.Onix1/ConfigurePortController.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,9 @@ public override IObservable<ContextTask> Process(IObservable<ContextTask> source
5151
var portShift = ((int)deviceAddress - 1) * 2;
5252
var passthroughState = (hubConfiguration == HubConfiguration.Passthrough ? 1 : 0) << portShift;
5353
context.HubState = (PassthroughState)(((int)context.HubState & ~(1 << portShift)) | passthroughState);
54-
return Disposable.Empty;
54+
55+
// leave in standard mode when finished
56+
return Disposable.Create(() => context.HubState = (PassthroughState)((int)context.HubState & ~(1 << portShift)));
5557
})
5658
.ConfigureLink(context =>
5759
{

OpenEphys.Onix1/ContextTask.cs

Lines changed: 52 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -289,25 +289,64 @@ internal Task StartAsync(int blockReadSize, int blockWriteSize, CancellationToke
289289
if (!acquisition.IsCompleted)
290290
throw new InvalidOperationException("Acquisition already running in the current context.");
291291

292-
// NB: Configure context before starting acquisition
292+
293+
// NB: Configure context before starting acquisition or the the settings (e.g. Block read
294+
// and write sizes) will not be respected
293295
var contextConfiguration = ConfigureContext();
294-
ctx.BlockReadSize = blockReadSize;
295-
ctx.BlockWriteSize = blockWriteSize;
296-
297-
// TODO: Stuff related to sync mode is 100% ONIX, not ONI. Therefore, in the long term,
298-
// another place to do this separation might be needed
299-
int address = ctx.HardwareAddress;
300-
int mode = (address & 0x00FF0000) >> 16;
301-
if (mode == 0) // Standalone mode
296+
297+
try
298+
{
299+
// set block read and write size
300+
ctx.BlockReadSize = blockReadSize;
301+
ctx.BlockWriteSize = blockWriteSize;
302+
303+
// TODO: Stuff related to sync mode is 100% ONIX, not ONI. Therefore, in the long term,
304+
// another place to do this separation might be needed
305+
int address = ctx.HardwareAddress;
306+
int mode = (address & 0x00FF0000) >> 16;
307+
if (mode == 0) // Standalone mode
308+
{
309+
ctx.Start(true);
310+
}
311+
else // If synchronized mode, reset counter independently
312+
{
313+
ctx.ResetFrameClock();
314+
ctx.Start(false);
315+
}
316+
317+
}
318+
catch (oni.ONIException ex) when (ex.Number == -20)
319+
{
320+
lock (regLock)
321+
{
322+
ctx.Stop();
323+
contextConfiguration.Dispose();
324+
}
325+
throw new InvalidOperationException($"The requested read size of {blockReadSize} bytes is too small for the current " +
326+
$"hardware configuration, which requires at least {ctx.MaxReadFrameSize} bytes.", ex);
327+
}
328+
catch (oni.ONIException ex) when (ex.Number == -24)
302329
{
303-
ctx.Start(true);
330+
lock (regLock)
331+
{
332+
ctx.Stop();
333+
contextConfiguration.Dispose();
334+
}
335+
throw new InvalidOperationException($"The requested write size of {blockWriteSize} bytes is too small for the current " +
336+
$"hardware configuration, which requires at least {ctx.MaxWriteFrameSize} bytes.", ex);
304337
}
305-
else // If synchronized mode, reset counter independently
338+
catch
306339
{
307-
ctx.ResetFrameClock();
308-
ctx.Start(false);
340+
lock (regLock)
341+
{
342+
ctx.Stop();
343+
contextConfiguration.Dispose();
344+
}
345+
throw;
309346
}
310347

348+
// TODO: If during the creation of of collectFramesCancellation, collectFramesToken, frameQueue, readFrames, or distributeFrames
349+
// an exception is thrown, contextConfiguration will not be disposed. The process will need to be restarted to get out of deadlock
311350
collectFramesCancellation = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
312351
var collectFramesToken = collectFramesCancellation.Token;
313352
var frameQueue = new BlockingCollection<oni.Frame>(MaxQueuedFrames);

0 commit comments

Comments
 (0)