Skip to content

Commit

Permalink
fix(openocd): fixed the port handler behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
alirana01 committed Oct 22, 2024
1 parent 2ac0d8a commit e0fa1a5
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package com.espressif.idf.core.util;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

import com.espressif.idf.core.logging.Logger;
Expand All @@ -25,45 +26,16 @@ private PortChecker()

public static boolean isPortAvailable(int port)
{
int attempts = 0;
int retryCount = 3;
long retryDelayMillis = 200;

while (attempts <= retryCount)
try (ServerSocket serverSocket = new ServerSocket(port))
{
try (Socket ignored = new Socket("localhost", port)) //$NON-NLS-1$
{
// If the socket opens, the port is in use
return false;
}
catch (IOException e)
{
// Port is unavailable, retrying if there are attempts left
if (attempts == retryCount)
{
// After exhausting all retries, return false
Logger.log("Port " + port + " is not available after " + retryCount + " retries."); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
return false; // Failure, port is still not available
}

attempts++;

// Log retry attempt
Logger.log("Attempt " + attempts + " failed, retrying in " + retryDelayMillis + " ms..."); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$

try
{
Thread.sleep(retryDelayMillis);
}
catch (InterruptedException interruptedException)
{
Thread.currentThread().interrupt(); // Restore interrupt status
Logger.log("Port availability check interrupted."); //$NON-NLS-1$
return false; // If interrupted, assume port unavailable and stop
}
}
serverSocket.setReuseAddress(true);
return true;
}
catch (Exception e)

Check warning on line 34 in bundles/com.espressif.idf.core/src/com/espressif/idf/core/util/PortChecker.java

View workflow job for this annotation

GitHub Actions / spotbugs

REC_CATCH_EXCEPTION

Exception is caught when Exception is not thrown in com.espressif.idf.core.util.PortChecker.isPortAvailable(int)
Raw output
This method uses a try-catch block that catches Exception objects, but Exception is not thrown within the try block, and RuntimeException is not explicitly caught. It is a common bug pattern to say try { ... } catch (Exception e) { something } as a shorthand for catching a number of types of exception each of whose catch blocks is identical, but this construct also accidentally catches RuntimeException as well, masking potential bugs.

A better approach is to either explicitly catch the specific exceptions that are thrown, or to explicitly catch RuntimeException exception, rethrow it, and then catch all non-Runtime Exceptions, as shown below:

try {
    ...
} catch (RuntimeException e) {
    throw e;
} catch (Exception e) {
    ... deal with all non-runtime exceptions ...
}
{
Logger.log("Port: " + port + " is not available"); //$NON-NLS-1$ //$NON-NLS-2$
return false;
}
return true; //Fallback not reachable
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,7 @@ public static String[] getGdbServerCommandLineArray(ILaunchConfiguration configu
lst.add(executable);

int port = PortChecker
.getAvailablePort(configuration.getAttribute(ConfigurationAttributes.GDB_SERVER_GDB_PORT_NUMBER,
DefaultPreferences.GDB_SERVER_GDB_PORT_NUMBER_DEFAULT));
.getAvailablePort(DefaultPreferences.GDB_SERVER_GDB_PORT_NUMBER_DEFAULT);

lst.add("-c"); //$NON-NLS-1$
lst.add("gdb_port " + port); //$NON-NLS-1$
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,9 +149,7 @@ protected void provideDefaults(ILaunchConfigurationWorkingCopy config) throws Co

if (Configuration.getDoStartGdbServer(config))
{
int availableRemotePort = PortChecker.getAvailablePort(config.getAttribute(IGDBJtagConstants.ATTR_PORT_NUMBER,
DefaultPreferences.GDB_SERVER_GDB_PORT_NUMBER_DEFAULT));
config.setAttribute(IGDBJtagConstants.ATTR_PORT_NUMBER, availableRemotePort);
config.setAttribute(IGDBJtagConstants.ATTR_PORT_NUMBER, DefaultPreferences.GDB_SERVER_GDB_PORT_NUMBER_DEFAULT);
}

config.setAttribute(DebugPlugin.ATTR_PROCESS_FACTORY_ID, CustomIdfProcessFactory.ID);
Expand Down

0 comments on commit e0fa1a5

Please sign in to comment.