Make debugger connection params customizable via registry; PY-76400

Merge-request: IJ-MR-148080
Merged-by: Aleksandr Sorotskii <aleksandr.sorotskii@jetbrains.com>

(cherry picked from commit 1ce2a48aebf3336199b12a79879f61af9e2bac7e)

IJ-MR-148080

GitOrigin-RevId: b443e0289420e4f208bc6d80c2c8f2e82a515a60
This commit is contained in:
Aleksandr Sorotskii
2024-10-30 13:16:12 +00:00
committed by intellij-monorepo-bot
parent 5e4006f0da
commit 1087c97645
3 changed files with 22 additions and 9 deletions

View File

@@ -460,6 +460,11 @@ The Python plug-in provides smart editing for Python scripts. The feature set of
<registryKey defaultValue="true" description="Use a single port for communication between PyCharm and the debugger"
restartRequired="false" key="python.debug.use.single.port"/>
<registryKey defaultValue="500" description="Timeout between attempts to connect to remote debugger server"
restartRequired="false" key="python.debugger.remote.connect.retry.timeout.ms"/>
<registryKey defaultValue="20" description="Max attempts to connect to remote debugger server"
restartRequired="false" key="python.debugger.remote.connect.max.attempts"/>
<!-- typing -->
<multiHostInjector implementation="com.jetbrains.python.codeInsight.typing.PyTypingAnnotationInjector"/>

View File

@@ -13,6 +13,7 @@ import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.util.Pair;
import com.intellij.openapi.util.SystemInfo;
import com.intellij.openapi.util.registry.Registry;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.util.containers.ContainerUtil;
import com.intellij.xdebugger.XSourcePosition;
@@ -33,6 +34,7 @@ import com.jetbrains.python.tables.TableCommandType;
import java.net.ServerSocket;
import java.security.SecureRandom;
import java.time.Duration;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CountDownLatch;
@@ -88,8 +90,11 @@ public class RemoteDebugger implements ProcessDebugger {
private final long myHandshakeTimeout;
public RemoteDebugger(@NotNull IPyDebugProcess debugProcess, @NotNull String host, int port) {
int connectRetryTimeout = Registry.intValue("python.debugger.remote.connect.retry.timeout.ms", 500);
int connectMaxAttempts = Registry.intValue("python.debugger.remote.connect.max.attempts", 20);
myDebugProcess = debugProcess;
myDebuggerTransport = new ClientModeDebuggerTransport(this, host, port);
myDebuggerTransport = new ClientModeDebuggerTransport(this, host, port, Duration.ofMillis(connectRetryTimeout), connectMaxAttempts);
myHandshakeTimeout = CLIENT_MODE_HANDSHAKE_TIMEOUT_IN_MILLIS;
}

View File

@@ -15,7 +15,7 @@ import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.TimeUnit;
import java.time.Duration;
import java.util.concurrent.atomic.AtomicBoolean;
/**
@@ -56,6 +56,8 @@ public class ClientModeDebuggerTransport extends BaseDebuggerTransport {
@NotNull private final String myHost;
private final int myPort;
private final Duration myRetryTimeout;
private final int myMaxRetries;
@NotNull private volatile State myState = State.INIT;
@@ -64,10 +66,14 @@ public class ClientModeDebuggerTransport extends BaseDebuggerTransport {
public ClientModeDebuggerTransport(@NotNull RemoteDebugger debugger,
@NotNull String host,
int port) {
int port,
Duration retryTimeout,
int maxRetries) {
super(debugger);
myHost = host;
myPort = port;
myRetryTimeout = retryTimeout;
myMaxRetries = maxRetries;
}
@Override
@@ -83,10 +89,7 @@ public class ClientModeDebuggerTransport extends BaseDebuggerTransport {
However, capturing stdout from the process at this point is not straightforward. Hence, we use this retry workaround.
*/
var attempts = 0;
var maxRetries = 10;
var retryInterval = TimeUnit.MILLISECONDS.toMillis(500);
boolean connected = false;
do {
try {
Socket clientSocket = new Socket();
@@ -127,14 +130,14 @@ public class ClientModeDebuggerTransport extends BaseDebuggerTransport {
catch (IOException e) {
attempts++;
try {
Thread.sleep(retryInterval);
Thread.sleep(myRetryTimeout.toMillis());
}
catch (InterruptedException ex) {
LOG.debug("Connection to the debugger thread is interrupted during the retry delay", e);
LOG.warn("Connection to the debugger thread is interrupted during the retry delay", e);
Thread.currentThread().interrupt();
}
}
} while (attempts < maxRetries && !connected);
} while (attempts < myMaxRetries && !connected);
if (!connected) {
myState = State.DISCONNECTED;