Fazecast / jSerialComm

Platform-independent serial port access for Java
GNU Lesser General Public License v3.0
1.34k stars 287 forks source link

Closing port takes a long time to return on MacOS #409

Closed hedgecrw closed 2 years ago

hedgecrw commented 2 years ago

This is in reference to the last post on Issue #406. A user is experiencing a delay of 5-7 seconds when closing their application.

@sprut666666, is this 5-7 second delay occurring when you call closePort(), or does this occur at some other time? Also, please test using the 2.9.1-beta2 version and see if this has improved (I added a fix to ensure that a thread does not try to wait on itself):

https://www.dropbox.com/t/eHCqJ4xDQeeAHDBF

sprut666666 commented 2 years ago

the problem remains, a little later describe in detail what is happening.

sprut666666 commented 2 years ago

@hedgecrw There are no problems with delays, the problem is in the incorrect completion of the work at killall -2 java

jSerialComm-2.9.1-beta2 - I lose the opportunity to work with the port. But I need to send and get a few bytes for the correct completion of work, after which I myself closes the port, call closePort()

jSerialComm:2.7.0 - allowed to read and write bytes, and after calling closePort() - closed

hedgecrw commented 2 years ago

@sprut666666, the behavior you are describing/relying on in v2.7.0 is actually undefined, incorrect behavior. In that and earlier library versions, the library didn't take pains to clean up after itself in terms of managing open resources, which caused a large number of issues for users who didn't properly close their own ports before shutting down their application.

To give you and other users a way to properly mimic that old behavior, I've added a new static library method called SerialPort.addShutdownHook(Thread hook) which you can use to register a Thread object that will be run right as your application is shutting down before the serial ports have been automatically cleaned up. Please try adding your port cleanup functionality using this method in the following beta library version, and let me know if it works as expected for you:

https://www.dropbox.com/t/ywoycnP7qRGjZDCG

(By the way, this function exactly mimics the Java addShutdownHook(Thread hook) method except that anything you register here is guaranteed to run in-order and before the library has cleaned itself up.)

sprut666666 commented 2 years ago

@hedgecrw nice work, everything works. But the solution is not very pretty. SerialPort.addShutdownHook(new Thread(() -> Utils.threadSleep(1000)));

hedgecrw commented 2 years ago

Great! Closing this as resolved with today's release v2.9.1.