Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Process exits when reopening port of removed USB stick without rediscovery #180

Closed
wborn opened this issue May 16, 2020 · 6 comments
Closed

Comments

@wborn
Copy link
Contributor

wborn commented May 16, 2020

When removing a USB stick the process exits when a port is reopened without first calling CommPortIdentifier.getPortIdentifiers();

Here is a simple example to reproduce the issue with nrjavaserial 5.1.1 on Ubuntu 18.04:

package example;

import gnu.io.CommPortIdentifier;
import gnu.io.NoSuchPortException;
import gnu.io.PortInUseException;
import gnu.io.RXTXPort;

public class Main {

    private static final String PORT = "/dev/ttyUSB0";

    public static void main(String[] args) {
        try {
            while (!Thread.currentThread().isInterrupted()) {
                try {
                    // Workaround
                    // CommPortIdentifier.getPortIdentifiers();

                    System.out.println("getting identifier for: " + PORT);
                    CommPortIdentifier identifier = CommPortIdentifier.getPortIdentifier(PORT);

                    System.out.println("opening: " + PORT);

                    RXTXPort port = identifier.open("main", 2000);

                    System.out.println("opened: " + PORT);

                    System.out.println("sleeping...");
                    Thread.sleep(5000);

                    System.out.println("closing: " + PORT);
                    port.close();
                    System.out.println("closed: " + PORT);
                } catch (NoSuchPortException e) {
                    System.err.println("NoSuchPortException: " + e.getMessage());
                } catch (PortInUseException e) {
                    System.err.println("PortInUseException: " + e.getMessage());
                }

                System.out.println("sleeping...");
                Thread.sleep(2000);
            }
        } catch (InterruptedException e) {
            System.err.println("InterruptedException: " + e.getMessage());
        }
    }

}

If I don't uncomment the workaround it exits with the following output after unplugging the USB stick:

getting identifier for: /dev/ttyUSB0
opening: /dev/ttyUSB0
opened: /dev/ttyUSB0
sleeping...
closing: /dev/ttyUSB0
closed: /dev/ttyUSB0
sleeping...
getting identifier for: /dev/ttyUSB0
opening: /dev/ttyUSB0/dev/ttyUSB0: No such file or directory

It works fine when uncommenting the workaround.

@wborn
Copy link
Contributor Author

wborn commented May 22, 2020

I just retested this issue with 5.2.0 @madhephaestus and it now throws the PortInUseException: Unknown Owner exception:

getting identifier for: /dev/ttyUSB0
opening: /dev/ttyUSB0
opened: /dev/ttyUSB0
sleeping...
closing: /dev/ttyUSB0
closed: /dev/ttyUSB0
sleeping...
getting identifier for: /dev/ttyUSB0
opening: /dev/ttyUSB0
sleeping...
PortInUseException: Unknown Owner
getting identifier for: /dev/ttyUSB0

open: locking has failed for /dev/ttyUSB0
opening: /dev/ttyUSB0
sleeping...
PortInUseException: Unknown Owner
getting identifier for: /dev/ttyUSB0
opening: /dev/ttyUSB0
open: locking has failed for /dev/ttyUSB0

Looks like the port isn't properly released.

madhephaestus added a commit that referenced this issue May 22, 2020
Since the workaround works reliably and does not have side effects, we
can simply move it into the offending function.
@madhephaestus
Copy link
Member

Rather than hunt down where it should have been released, we could just move the workaround into the function?

It seems the get identifiers refreshes the state of the known ports list so that the new method can complete.

@madhephaestus
Copy link
Member

check with release 5.2.1 please?

@wborn
Copy link
Contributor Author

wborn commented May 22, 2020

It seems to resolve this issue. 👍

I do notice if I repeat inserting/removing the USB stick, eventually issue #111 occurs and it is no longer possible to use the serial port unless I restart the application.

@madhephaestus
Copy link
Member

madhephaestus commented May 22, 2020

lol, and the issue whack-a-mole continues, closing this one, reopening that one...

@wborn
Copy link
Contributor Author

wborn commented May 22, 2020

Yes but it is certainly improving now the whole application doesn't exit and most of the time you can reinsert your USB stick and use it. 😉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants