Skip to content

Instantly share code, notes, and snippets.

@MrDOS
Last active January 29, 2021 00:45
Show Gist options
  • Save MrDOS/4c048f52d3c6cd1ff7a96bd6c9883907 to your computer and use it in GitHub Desktop.
Save MrDOS/4c048f52d3c6cd1ff7a96bd6c9883907 to your computer and use it in GitHub Desktop.
import java.io.IOException;
import java.io.InputStream;
import gnu.io.CommPortIdentifier;
import gnu.io.SerialPort;
public class DisconnectTest
{
private static final int SLEEP_AFTER_DISCONNECT = 2_000;
public static void main(String[] args) throws Exception
{
String portName = args[0];
// The bug is equally-reproducible with the NRSerialPort wrapper. To
// try, uncomment this, comment out the `CommPortIdentifier` lines...
// NRSerialPort port = new NRSerialPort(portName, 115200);
CommPortIdentifier identifier = CommPortIdentifier.getPortIdentifier(portName);
SerialPort port = identifier.open("disconnect-test", 0);
// ...uncomment this...
// port.connect();
InputStream in = port.getInputStream();
System.out.printf(
"Opened port %s and beginning to read. Trigger a " +
"hardware error now.\n",
portName);
try
{
while (true)
{
in.available();
}
}
catch (IOException e)
{
System.err.printf("Caught an exception: %s\n", e.getMessage());
}
System.out.printf("Going to disconnect the port...\n");
// ...uncomment this, and comment out the call to `port.close()`.
// port.disconnect();
port.close();
System.out.printf(
"Disconnected. Now going to sleep for %.1fs to give the monitor " +
"thread a chance to SIGSEGV.\n",
SLEEP_AFTER_DISCONNECT / 1_000.0);
try
{
Thread.sleep(SLEEP_AFTER_DISCONNECT);
}
catch (InterruptedException e)
{
System.err.printf("Sleep was interrupted. Test abandoned.\n");
System.exit(1);
}
System.out.printf(
"If you're seeing this, that means the monitor thread didn't " +
"segfault, and bug reproduction has failed.\n");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment