Skip to content

Commit

Permalink
8201183: sjavac build failures: "Connection attempt failed: Connectio…
Browse files Browse the repository at this point in the history
…n refused"

Reviewed-by: erikj, ihse
  • Loading branch information
djelinski committed Apr 11, 2024
1 parent ff5c9a4 commit ecc603c
Showing 1 changed file with 24 additions and 20 deletions.
44 changes: 24 additions & 20 deletions make/langtools/tools/javacserver/shared/PortFile.java
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -89,17 +88,18 @@ private void initializeChannel() throws PortFileInaccessibleException {
}
// 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();
}

Expand All @@ -110,8 +110,7 @@ public void lock() throws IOException, InterruptedException {
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) {
Expand Down Expand Up @@ -156,6 +155,9 @@ public long getCookie() {
* 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);
Expand All @@ -169,19 +171,19 @@ public void setValues(int port, long cookie) throws IOException {
* 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);
}

/**
Expand Down Expand Up @@ -210,10 +212,12 @@ public boolean markedForStop() throws IOException {
*/
public void unlock() throws IOException {
if (lock == null) {
return;
throw new IllegalStateException("Not locked");
}
lock.release();
lock = null;
rwfile.close();
rwfile = null;
lockSem.release();
}

Expand Down

1 comment on commit ecc603c

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.