Fazecast / jSerialComm

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

How many LISTENING_EVENT_PORT_DISCONNECTED are expected on application stop? #408

Closed rusefillc closed 2 years ago

rusefillc commented 2 years ago

We've added

            @Override
            public void serialEvent(SerialPortEvent event) {

                if (event.getEventType() == SerialPort.LISTENING_EVENT_PORT_DISCONNECTED) {
                    System.out.println("got event SerialPort.LISTENING_EVENT_PORT_DISCONNECTED");
                    return;
                }
}

and now when I attempt to stop my application I get a few hundreds of these callbacks. just wanted to check if that's a feature or a bug

Wed Feb 16 00:38:40 EST 2022: onDataArrival alive for 3 second(s) textPull=false rusEFI 2022.02.16.proteus_h7.2184629533 
Wed Feb 16 00:38:40 EST 2022: onDataArrival alive for 3 second(s) textPull=false rusEFI 2022.02.16.proteus_h7.2184629533 
Wed Feb 16 00:38:40 EST 2022: onDataArrival alive for 3 second(s) textPull=false rusEFI 2022.02.16.proteus_h7.2184629533 
got event SerialPort.LISTENING_EVENT_PORT_DISCONNECTED
got event SerialPort.LISTENING_EVENT_PORT_DISCONNECTED
got event SerialPort.LISTENING_EVENT_PORT_DISCONNECTED
got event SerialPort.LISTENING_EVENT_PORT_DISCONNECTED
got event SerialPort.LISTENING_EVENT_PORT_DISCONNECTED
got event SerialPort.LISTENING_EVENT_PORT_DISCONNECTED
got event SerialPort.LISTENING_EVENT_PORT_DISCONNECTED
got event SerialPort.LISTENING_EVENT_PORT_DISCONNECTED
got event SerialPort.LISTENING_EVENT_PORT_DISCONNECTED
got event SerialPort.LISTENING_EVENT_PORT_DISCONNECTED
got event SerialPort.LISTENING_EVENT_PORT_DISCONNECTED
got event SerialPort.LISTENING_EVENT_PORT_DISCONNECTED
got event SerialPort.LISTENING_EVENT_PORT_DISCONNECTED
got event SerialPort.LISTENING_EVENT_PORT_DISCONNECTED
got event SerialPort.LISTENING_EVENT_PORT_DISCONNECTED
got event SerialPort.LISTENING_EVENT_PORT_DISCONNECTED
got event SerialPort.LISTENING_EVENT_PORT_DISCONNECTED
got event SerialPort.LISTENING_EVENT_PORT_DISCONNECTED
got event SerialPort.LISTENING_EVENT_PORT_DISCONNECTED
got event SerialPort.LISTENING_EVENT_PORT_DISCONNECTED
got event SerialPort.LISTENING_EVENT_PORT_DISCONNECTED
got event SerialPort.LISTENING_EVENT_PORT_DISCONNECTED
got event SerialPort.LISTENING_EVENT_PORT_DISCONNECTED
got event SerialPort.LISTENING_EVENT_PORT_DISCONNECTED
got event SerialPort.LISTENING_EVENT_PORT_DISCONNECTED
got event SerialPort.LISTENING_EVENT_PORT_DISCONNECTED
got event SerialPort.LISTENING_EVENT_PORT_DISCONNECTED
got event SerialPort.LISTENING_EVENT_PORT_DISCONNECTED
got event SerialPort.LISTENING_EVENT_PORT_DISCONNECTED
got event SerialPort.LISTENING_EVENT_PORT_DISCONNECTED
got event SerialPort.LISTENING_EVENT_PORT_DISCONNECTED
got event SerialPort.LISTENING_EVENT_PORT_DISCONNECTED
got event SerialPort.LISTENING_EVENT_PORT_DISCONNECTED
got event SerialPort.LISTENING_EVENT_PORT_DISCONNECTED
hedgecrw commented 2 years ago

Ah, I assume you're seeing this after trying to stop your application without first closing the serial ports? That's a bug, although a relatively harmless one. I will make sure the event listeners do not fire if the application is in a state of shutting down. (And luckily, this is one I can reproduce and test myself!)

hedgecrw commented 2 years ago

Oh, and the updated library I just posted in your other thread also has the fix for this issue included. So please test that update (https://www.dropbox.com/t/75SarQc7JO5C92Wm) for this issue.

rusefillc commented 2 years ago

test failed I guess?

on the one side LISTENING_EVENT_PORT_DISCONNECTED did not happen on application close on that above mentioned custom .jar on application close

on the other side LISTENING_EVENT_PORT_DISCONNECTED did not happen on cable removal from USB port with following loggin

E 220216 150330.878 [communication executor1] BinaryProtocol - output channels: executeCommand failed: java.io.IOException: write failed: wrote 0 but expected 11
writeFooter logs/rusEFI_trigger_log_2022-02-16_15_03_24_337.logicdata
I 220216 150330.884 [communication executor1] SerialIoStream - COM33: Closing port...
[COM?]: In closePortNative()
[\\.\COM33]: Purging all active port operations
[\\.\COM33]: Closing port
[\\.\COM33]: Entering closePortNative critical section #1
[\\.\COM33]: Leaving closePortNative critical section #1
I 220216 150330.885 [communication executor1] SerialIoStream - COM33: Closed port.
I 220216 150330.917 [text pull1] BinaryProtocol - Stopping text pull
I 220216 150335.779 [communication executor1] ConsoleUI - ConnectionWatchdog.reconnectTimer restarting: 5000
hedgecrw commented 2 years ago

Hmmm...I think this is okay. It looks like your code is detecting a problem based on an incorrect number of bytes written and then closing the port before the port has a chance to fire the disconnected event. The way it's set up now, the event won't fire if the port was already successfully closed.

Do you think it makes more sense to have the disconnected event fire even if your code has already handled the closing of the port? That's an easy change to make, but I'm not sure if it makes the most sense to have a closed port fire an event.

rusefillc commented 2 years ago

I do not have an opinion I have to trust your judgement on that. I can also poke @mck1117 :)

hedgecrw commented 2 years ago

I think I'm going to leave it as-is for now for correctness sake, but if it becomes something that people need in the future, I'll revisit it at that time. For now, I have created a beta2 version of the library that I'm going to stress test before making a new release. Feel free to test as well if you'd like:

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

hedgecrw commented 2 years ago

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