From 34940632a36ec6e7ae8fd34f659f242466311661 Mon Sep 17 00:00:00 2001 From: Malte Bitter Date: Thu, 16 Sep 2021 11:39:34 +0200 Subject: [PATCH] Fix deadlock on connection lost --- OBD.NET/OBD.NET.Common/Devices/SerialDevice.cs | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/OBD.NET/OBD.NET.Common/Devices/SerialDevice.cs b/OBD.NET/OBD.NET.Common/Devices/SerialDevice.cs index 60a4520..021b1bf 100644 --- a/OBD.NET/OBD.NET.Common/Devices/SerialDevice.cs +++ b/OBD.NET/OBD.NET.Common/Devices/SerialDevice.cs @@ -217,11 +217,23 @@ namespace OBD.NET.Common.Devices else Connection.Write(Encoding.ASCII.GetBytes(CurrentCommand.CommandText)); - //wait for command to finish - _commandFinishedEvent.WaitOne(); + //wait for command to finish or command canceled + while (!(_commandFinishedEvent.WaitOne(50) || _commandCancellationToken.IsCancellationRequested)) + { + } } } catch (OperationCanceledException) { /*ignore, because it is thrown when the cancellation token is canceled*/} + + // if canceled set all commands as completed (with null result) + if (_commandCancellationToken.IsCancellationRequested) + { + CurrentCommand?.CommandResult.WaitHandle.Set(); + foreach (var cmd in _commandQueue) + { + cmd.CommandResult.WaitHandle.Set(); + } + } } }