Skip to content

Commit 8e196fe

Browse files
committed
The waitWhileListening did not work
- It declared the debug port free, but immediately 300 ms later it was blocked. Signed-off-by: David Matějček <[email protected]>
1 parent 0025518 commit 8e196fe

File tree

3 files changed

+10
-53
lines changed

3 files changed

+10
-53
lines changed

nucleus/admin/server-mgmt/src/main/java/com/sun/enterprise/admin/servermgmt/cli/LocalServerCommand.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,9 @@
4949
import static com.sun.enterprise.admin.cli.CLIConstants.DEFAULT_HOSTNAME;
5050
import static com.sun.enterprise.admin.cli.ProgramOptions.PasswordLocation.LOCAL_PASSWORD;
5151
import static com.sun.enterprise.admin.servermgmt.cli.ServerLifeSignChecker.step;
52+
import static com.sun.enterprise.universal.process.ProcessUtils.isListening;
5253
import static com.sun.enterprise.universal.process.ProcessUtils.loadPid;
54+
import static com.sun.enterprise.universal.process.ProcessUtils.waitFor;
5355
import static com.sun.enterprise.universal.process.ProcessUtils.waitForNewPid;
5456
import static com.sun.enterprise.universal.process.ProcessUtils.waitWhileIsAlive;
5557
import static com.sun.enterprise.util.SystemPropertyConstants.KEYSTORE_PASSWORD_DEFAULT;
@@ -372,9 +374,9 @@ protected final void waitForStop(final Long pid, final HostAndPort adminAddress,
372374
return;
373375
}
374376
LOG.log(INFO, "Waiting until admin endpoint {0} is free.", adminAddress);
375-
final boolean stopped = ProcessUtils.waitWhileListening(adminAddress,
376-
portTimeout == null ? Duration.ofHours(1L) : portTimeout, printDots);
377-
if (stopped) {
377+
final boolean portIsFree = waitFor(() -> !isListening(adminAddress), portTimeout, printDots);
378+
LOG.log(INFO, "Admin port is {0}.", portIsFree ? "free" : "blocked");
379+
if (portIsFree) {
378380
return;
379381
}
380382
throw new CommandException("Timed out waiting for the server to stop.");

nucleus/admin/server-mgmt/src/main/java/com/sun/enterprise/admin/servermgmt/cli/StartServerHelper.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@
5151
import static com.sun.enterprise.admin.cli.CLIConstants.RESTART_DEBUG_ON;
5252
import static com.sun.enterprise.admin.cli.CLIConstants.RESTART_NORMAL;
5353
import static com.sun.enterprise.admin.cli.CLIConstants.WALL_CLOCK_START_PROP;
54-
import static com.sun.enterprise.universal.process.ProcessUtils.waitWhileListening;
54+
import static com.sun.enterprise.universal.process.ProcessUtils.isListening;
55+
import static com.sun.enterprise.universal.process.ProcessUtils.waitFor;
5556
import static java.lang.System.Logger.Level.DEBUG;
5657
import static java.lang.System.Logger.Level.INFO;
5758
import static org.glassfish.main.jdke.props.SystemProperties.setProperty;
@@ -96,7 +97,9 @@ public StartServerHelper(boolean terse, Duration timeout, ServerDirs serverDirs,
9697
final Integer debugPort = launcher.getDebugPort();
9798
if (debugPort != null) {
9899
LOG.log(INFO, "Waiting few seconds until debug port {0} is free.", debugPort);
99-
waitWhileListening(new HostAndPort("localhost", debugPort, true), Duration.ofSeconds(10), terse);
100+
final HostAndPort debugEndpoint = new HostAndPort("localhost", debugPort, false);
101+
final boolean portIsFree = waitFor(() -> !isListening(debugEndpoint), timeout, terse);
102+
LOG.log(INFO, "Debug port is {0}.", portIsFree ? "free" : "blocked");
100103
}
101104
}
102105
checkFreeAdminPorts(info.getAdminAddresses());

nucleus/common/common-util/src/main/java/com/sun/enterprise/universal/process/ProcessUtils.java

Lines changed: 0 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,6 @@
2828
import java.lang.System.Logger;
2929
import java.net.InetSocketAddress;
3030
import java.net.Socket;
31-
import java.net.SocketException;
32-
import java.net.SocketTimeoutException;
3331
import java.text.MessageFormat;
3432
import java.time.Duration;
3533
import java.time.Instant;
@@ -38,10 +36,8 @@
3836

3937
import static com.sun.enterprise.util.StringUtils.ok;
4038
import static java.lang.System.Logger.Level.DEBUG;
41-
import static java.lang.System.Logger.Level.ERROR;
4239
import static java.lang.System.Logger.Level.INFO;
4340
import static java.lang.System.Logger.Level.TRACE;
44-
import static java.lang.System.Logger.Level.WARNING;
4541
import static java.nio.charset.StandardCharsets.ISO_8859_1;
4642

4743
/**
@@ -144,50 +140,6 @@ public static boolean isAlive(final ProcessHandle process) {
144140
return true;
145141
}
146142

147-
/**
148-
* Blocks until the endpoint closes the connection or timeout comes first.
149-
*
150-
* @param endpoint endpoint host and port to use.
151-
* @param timeout must not be null
152-
* @param printDots true to print dots to STDOUT while waiting. One dot per second.
153-
* @return true if the connection was closed before timeout. False otherwise.
154-
*/
155-
public static boolean waitWhileListening(HostAndPort endpoint, Duration timeout, boolean printDots) {
156-
final DotPrinter dotPrinter = DotPrinter.startWaiting(printDots);
157-
try (Socket server = new Socket()) {
158-
server.setSoTimeout((int) timeout.toMillis());
159-
// Max 5 seconds to connect. It is an extreme value for local endpoint.
160-
try {
161-
server.connect(new InetSocketAddress(endpoint.getHost(), endpoint.getPort()), SOCKET_TIMEOUT);
162-
} catch (IOException e) {
163-
LOG.log(TRACE, "Unable to connect - server is probably down.!", e);
164-
return true;
165-
}
166-
try {
167-
int result = server.getInputStream().read();
168-
if (result == -1) {
169-
LOG.log(TRACE, "Input stream closed - server probably stopped!");
170-
return true;
171-
}
172-
LOG.log(ERROR, "We were able to read something: {0}. Returning false.", result);
173-
return false;
174-
} catch (SocketTimeoutException e) {
175-
LOG.log(TRACE, "Timeout while reading. Returning false.", e);
176-
return false;
177-
} catch (SocketException e) {
178-
LOG.log(TRACE, "Socket read failed. Returning true.", e);
179-
return true;
180-
}
181-
} catch (Exception ex) {
182-
LOG.log(WARNING, "An attempt to open a socket to " + endpoint
183-
+ " resulted in exception. Therefore we assume the server has stopped.", ex);
184-
return true;
185-
} finally {
186-
DotPrinter.stopWaiting(dotPrinter);
187-
}
188-
}
189-
190-
191143
/**
192144
* @param endpoint endpoint host and port to use.
193145
* @return true if the endpoint is listening on socket

0 commit comments

Comments
 (0)