dotMorten / NmeaParser

Library for handling NMEA message in Windows Desktop, Store, Phone, Universal, and Xamarin (Android + iOS), coming from files, bluetooth, serial port or any stream
https://dotmorten.github.io/NmeaParser/
Apache License 2.0
262 stars 89 forks source link

CloseAsync() hangs on an idle NmeaDevice #111

Closed mclifford1 closed 2 weeks ago

mclifford1 commented 1 year ago

Hello again,

I have noticed an issue (similar to #88) when calling CloseAsync() on an NmeaDevice (in my case a SerialPortDevice) in that it never returns. In my case the serial port is idle when this error is observed.

Looking at the source code I believe it is caused by:

image

in that the stream does not correctly react to the cancellation token internally (that is, inside the Stream.ReadAsync() itself) and terminate as it should.

This only hangs on an inactive serial port because an active port would be periodically checking the cancellation token and exit the read loop appropriately.

I found this thread discussing the issue and possible work-arounds. https://stackoverflow.com/questions/12421989/networkstream-readasync-with-a-cancellation-token-never-cancels

Best regards, Mark

dotMorten commented 2 weeks ago

See comment here: https://github.com/dotMorten/NmeaParser/issues/113#issuecomment-2487465292 CancellationToken appears to be broken for SerialPort's ReadAsync method.

dotMorten commented 2 weeks ago

Use this device instead: https://dotmorten.github.io/NmeaParser/api/netwin/NmeaParser.WinRTSerialDevice.html