genielabs / serialport-lib-dotnet

Serial Port libray for .Net / Mono
https://www.nuget.org/packages/SerialPortLib/
Apache License 2.0
157 stars 65 forks source link

Closing Port COM while receiving data often creates a Deadlock in application #20

Closed gitjsdr26 closed 1 year ago

gitjsdr26 commented 4 years ago

Hi,

I found an issue when closing Port COM while sending data. It creates a Deadlock in the application. This is the stack trace below :

[En attente du verrou détenu par le thread 4156]    
WindowsBase.dll!System.Windows.Threading.DispatcherSynchronizationContext.Wait(System.IntPtr[] waitHandles, bool waitAll, int millisecondsTimeout)  
[Transition Natif à Managé] 
[Transition Managé à Natif] 
System.dll!System.IO.Ports.SerialStream.Dispose(bool disposing)
mscorlib.dll!System.IO.Stream.Close()
System.dll!System.IO.Ports.SerialPort.Dispose(bool disposing)
System.dll!System.IO.Ports.SerialPort.Close()

The execution pointer is blocked in method

private void Close()

when executing

_serialPort.Close();

I think that this deadlock occurs in native Framework SerialPort. I've no idea how to solve this, beside waiting the end of transmission...

See this thread : https://social.msdn.microsoft.com/Forums/en-US/ce8ce1a3-64ed-4f26-b9ad-e2ff1d3be0a5/serial-port-hangs-whilst-closing?forum=Vsexpressvcs

Regards

genemars commented 4 years ago

yes this might be a problem in the core libraries, so we can ignore it.

gitjsdr26 commented 4 years ago

But can we try to find a working workaround and then put it into the library ?

genemars commented 4 years ago

Yes if the work-around can be done in serial-port-lib.

genemars commented 4 years ago

but better do this in a different pull-request

gitjsdr26 commented 4 years ago

Actually I didn't found a stable workaround. If I do, I'll post a pull request. Regards.

genemars commented 1 year ago

Fixed by #26