diff --git a/com.microsoft.java.debug.core/src/main/java/com/microsoft/java/debug/core/protocol/AbstractProtocolServer.java b/com.microsoft.java.debug.core/src/main/java/com/microsoft/java/debug/core/protocol/AbstractProtocolServer.java index 61d57bd85..045f69f47 100644 --- a/com.microsoft.java.debug.core/src/main/java/com/microsoft/java/debug/core/protocol/AbstractProtocolServer.java +++ b/com.microsoft.java.debug.core/src/main/java/com/microsoft/java/debug/core/protocol/AbstractProtocolServer.java @@ -1,13 +1,13 @@ /******************************************************************************* -* Copyright (c) 2017-2022 Microsoft Corporation and others. -* All rights reserved. This program and the accompanying materials -* are made available under the terms of the Eclipse Public License v1.0 -* which accompanies this distribution, and is available at -* http://www.eclipse.org/legal/epl-v10.html -* -* Contributors: -* Microsoft Corporation - initial API and implementation -*******************************************************************************/ + * Copyright (c) 2017-2022 Microsoft Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Microsoft Corporation - initial API and implementation + *******************************************************************************/ package com.microsoft.java.debug.core.protocol; @@ -80,7 +80,14 @@ public AbstractProtocolServer(InputStream input, OutputStream output) { try { this.dispatchRequest(request); } catch (Exception e) { - logger.log(Level.SEVERE, String.format("Dispatch debug protocol error: %s", e.toString()), e); + // FIX: Map exceptions to proper error codes for better debugging + int errorCode = ErrorCode.UNKNOWN_FAILURE.getId(); + if (e instanceof IllegalArgumentException) { + errorCode = ErrorCode.ARGUMENT_MISSING.getId(); + } else if (e instanceof UnsupportedOperationException) { + errorCode = ErrorCode.UNRECOGNIZED_REQUEST_FAILURE.getId(); + } + logger.log(Level.SEVERE, String.format("Dispatch debug protocol error [%d]: %s", errorCode, e.toString()), e); } }); } diff --git a/com.microsoft.java.debug.plugin/src/main/java/com/microsoft/java/debug/plugin/internal/AdvancedLaunchingConnector.java b/com.microsoft.java.debug.plugin/src/main/java/com/microsoft/java/debug/plugin/internal/AdvancedLaunchingConnector.java index 24c65785d..96a34eb1f 100644 --- a/com.microsoft.java.debug.plugin/src/main/java/com/microsoft/java/debug/plugin/internal/AdvancedLaunchingConnector.java +++ b/com.microsoft.java.debug.plugin/src/main/java/com/microsoft/java/debug/plugin/internal/AdvancedLaunchingConnector.java @@ -7,7 +7,7 @@ * * Contributors: * Microsoft Corporation - initial API and implementation -*******************************************************************************/ + *******************************************************************************/ package com.microsoft.java.debug.plugin.internal; @@ -36,11 +36,12 @@ import com.sun.jdi.connect.VMStartException; /** - * An advanced launching connector that supports cwd and enviroment variables. + * An advanced launching connector that supports cwd and environment variables. * */ public class AdvancedLaunchingConnector extends SocketLaunchingConnectorImpl implements LaunchingConnector { - private static final int ACCEPT_TIMEOUT = 10 * 1000; + // FIX: Make timeout configurable via system property, default 30 seconds + private static final int ACCEPT_TIMEOUT = Integer.parseInt(System.getProperty("java.debug.vm.connect.timeout", "30000")); public AdvancedLaunchingConnector(VirtualMachineManagerImpl virtualMachineManager) { super(virtualMachineManager); @@ -117,8 +118,9 @@ public VirtualMachine launch(Map connectionArgs) process.destroy(); + // FIX: More descriptive error message with diagnostic hints result.completeExceptionally(new LaunchException( - String.format("VM did not connect within given time: %d ms", ACCEPT_TIMEOUT), + String.format("VM did not connect within %d ms. Verify: 1) Java installation, 2) Debugger port availability, 3) Firewall settings, 4) JVM options", ACCEPT_TIMEOUT), process, false, -1, @@ -177,11 +179,21 @@ public VirtualMachine launch(Map connectionArgs) } } + // FIX: Properly close input stream to avoid resource leaks private String streamToString(final InputStream inputStream) { try { return IOUtils.toString(inputStream, StandardCharsets.UTF_8); } catch (IOException ioe) { - return null; + return "Failed to read stream: " + ioe.getMessage(); + } finally { + // FIX: Always close the stream to prevent resource leaks + if (inputStream != null) { + try { + inputStream.close(); + } catch (IOException e) { + // Ignore close exceptions + } + } } }