teensy_gateway going crazy

tni

Well-known member
I just had teensy_gateway.exe going crazy on me. It decided it needed company and I got 1850 instances of teensy_gateway.exe running, each with a conhost instance. At that point, Windows was running out of handles (300000 allocated) and wasn't even able to bring up a task manager or login screen.

Platform is Win7 x64, teensy_gateway.exe is from Arduino 1.6.12 / Teensyduino 1.31beta. Teensy 3 was configured with Raw HID. The Arduino IDE was in use (with serial monitor). TyQt was used earlier but not running at the time teensy_gateway went crazy.

I was doing some dtostrf experiments at the time and Teensy was occasionally hanging / unresponsive.
 
Last edited:
I had it happen again, after a fresh reboot. After killing a few hundred teensy_gateways, they stopped appearing.

Looking at the Teensyduino code this looks bad:
Code:
    reopener = new Thread() {
      public void run() {
        int initial_delay = 800;
        // trying too early on Windows risks triggering a horrible
        // Windows driver bug, so be extra careful on Windows
        if (OSUtils.isWindows()) initial_delay += 1000;
        try {
          sleep(initial_delay);
        } catch (InterruptedException e) {
          return;
        }
        int attempt = 0;
        while (true) {  // keep trying as long as the window is visible
          attempt++;
          //if (debug) System.out.println("reopener attempt # " + attempt);
          try {
            sleep((attempt < 50) ? 100 : 500);
          } catch (InterruptedException e) {
            return;
          }
          try {
            open();
            return;
          } catch (Exception e) {
            // open throws exception if unable to open
          }
	  //if (debug && attempt > 60) return;
        }
      }
    };
    reopener.start();
}
If there is an exception in 'open()', it retries in an infinite loop. How about limiting attempts to some sane number and/or checking if there are hundreds of teensy_gateway zombies trying to take down the machine?
 
It happened again. I was running an instrumented version of the IDE. For some reason, "gateway_connect()" fails. FakeSerial() will throw a SerialException("no connection") which causes the infinite loop I suspected in the previous post. If I kill enough teensy_gateway processes, eventually the connect will will be successful and the loop stops.

What should fix gateway instances piling up, is calling "dispose_gateway()" before the exec:
Code:
	private boolean gateway_start(String cmd) {
		String cmdline = BaseNoGui.getHardwarePath() + File.separator
			+ "tools" + File.separator + cmd;
		try {
			gateway = Runtime.getRuntime().exec(new String[] {cmdline});
			if (!gateway_shutdown_scheduled) {

"dispose_gateway()" should also be called in the gateway_start() exception handler (not just assigning null).
 
Back
Top