From ecc603ca9b441cbb7ad27fbc2529fcb0b1da1992 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Jeli=C5=84ski?= Date: Thu, 11 Apr 2024 12:42:24 +0000 Subject: [PATCH] 8201183: sjavac build failures: "Connection attempt failed: Connection refused" Reviewed-by: erikj, ihse --- .../tools/javacserver/shared/PortFile.java | 44 ++++++++++--------- 1 file changed, 24 insertions(+), 20 deletions(-) diff --git a/make/langtools/tools/javacserver/shared/PortFile.java b/make/langtools/tools/javacserver/shared/PortFile.java index 2e4283a22b3..b31e97cfeea 100644 --- a/make/langtools/tools/javacserver/shared/PortFile.java +++ b/make/langtools/tools/javacserver/shared/PortFile.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2012, 2022, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2012, 2024, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -50,17 +50,16 @@ public class PortFile { // Followed by a 4 byte int, with the port nr. // Followed by a 8 byte long, with cookie nr. - private String filename; - private File file; - private File stopFile; + private final String filename; + private final File file; + private final File stopFile; private RandomAccessFile rwfile; - private FileChannel channel; // FileLock used to solve inter JVM synchronization, lockSem used to avoid // JVM internal OverlappingFileLockExceptions. // Class invariant: lock.isValid() <-> lockSem.availablePermits() == 0 private FileLock lock; - private Semaphore lockSem = new Semaphore(1); + private final Semaphore lockSem = new Semaphore(1); private boolean containsPortInfo; private int serverPort; @@ -89,17 +88,18 @@ public class PortFile { } // The rwfile should only be readable by the owner of the process // and no other! How do we do that on a RandomAccessFile? - channel = rwfile.getChannel(); } /** * Lock the port file. */ public void lock() throws IOException, InterruptedException { - if (channel == null) { - initializeChannel(); - } lockSem.acquire(); + if (rwfile != null) { + throw new IllegalStateException("rwfile not null"); + } + initializeChannel(); + FileChannel channel = rwfile.getChannel(); lock = channel.lock(); } @@ -110,8 +110,7 @@ public class PortFile { public void getValues() { containsPortInfo = false; if (lock == null) { - // Not locked, remain ignorant about port file contents. - return; + throw new IllegalStateException("Must lock before calling getValues"); } try { if (rwfile.length()>0) { @@ -156,6 +155,9 @@ public class PortFile { * Store the values into the locked port file. */ public void setValues(int port, long cookie) throws IOException { + if (lock == null) { + throw new IllegalStateException("Must lock before calling setValues"); + } rwfile.seek(0); // Write the magic nr that identifies a port file. rwfile.writeInt(magicNr); @@ -169,19 +171,19 @@ public class PortFile { * Delete the port file. */ public void delete() throws IOException, InterruptedException { - // Access to file must be closed before deleting. - rwfile.close(); - - file.delete(); - - // Wait until file has been deleted (deletes are asynchronous on Windows!) otherwise we + if (!file.exists()) { // file deleted already + return; + } + // Keep trying until file has been deleted, otherwise we // might shutdown the server and prevent another one from starting. - for (int i = 0; i < 10 && file.exists(); i++) { + for (int i = 0; i < 10 && file.exists() && !file.delete(); i++) { Thread.sleep(1000); } if (file.exists()) { throw new IOException("Failed to delete file."); } + // allow some time for late clients to connect + Thread.sleep(1000); } /** @@ -210,10 +212,12 @@ public class PortFile { */ public void unlock() throws IOException { if (lock == null) { - return; + throw new IllegalStateException("Not locked"); } lock.release(); lock = null; + rwfile.close(); + rwfile = null; lockSem.release(); }