Skip to content

Commit

Permalink
rmi: AFNaming: Fix potential race for rmiService
Browse files Browse the repository at this point in the history
  • Loading branch information
kohlschuetter committed Jul 27, 2023
1 parent 2252188 commit 2bef075
Showing 1 changed file with 11 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public abstract class AFNaming extends AFRegistryAccess {
private final int registryPort;
private final int servicePort;
AFRMISocketFactory socketFactory;
private boolean remoteShutdownAllowed = true;
private final AtomicBoolean remoteShutdownAllowed = new AtomicBoolean(true);
private final AtomicBoolean shutdownInProgress = new AtomicBoolean(false);
private final AtomicBoolean addedShutdownHook = new AtomicBoolean(false);

Expand Down Expand Up @@ -142,7 +142,8 @@ AFRMIService getRMIService() throws RemoteException, NotBoundException {
return getRMIService(getRegistry());
}

AFRMIService getRMIService(AFRegistry reg) throws RemoteException, NotBoundException {
synchronized AFRMIService getRMIService(AFRegistry reg) throws RemoteException,
NotBoundException {
if (rmiService == null) {
this.rmiService = getRMIServiceFromRegistry(reg);
}
Expand All @@ -152,7 +153,7 @@ AFRMIService getRMIService(AFRegistry reg) throws RemoteException, NotBoundExcep
AFRMIService getRMIServiceFromRegistry(AFRegistry reg) throws RemoteException, NotBoundException {
AFRMIService service;
service = (AFRMIService) reg.lookup(RMI_SERVICE_NAME, 5, TimeUnit.SECONDS);
this.remoteShutdownAllowed = service.isShutdownAllowed();
this.remoteShutdownAllowed.set(service.isShutdownAllowed());
return service;
}

Expand All @@ -172,7 +173,7 @@ public void onRuntimeShutdown(Thread thread) throws IOException {
}
}

private void rebindRMIService(final AFRMIService assigner) throws RemoteException {
private synchronized void rebindRMIService(final AFRMIService assigner) throws RemoteException {
rmiService = assigner;
getRegistry().rebind(RMI_SERVICE_NAME, assigner);
}
Expand Down Expand Up @@ -265,7 +266,7 @@ private AFRegistry locateRegistry() throws RemoteException {
*
* @throws RemoteException if the operation fails.
*/
public void shutdownRegistry() throws RemoteException {
public synchronized void shutdownRegistry() throws RemoteException {
synchronized (AFNaming.class) {
if (registry == null) {
return;
Expand Down Expand Up @@ -309,8 +310,8 @@ public void shutdownRegistry() throws RemoteException {
*/
protected abstract void shutdownRegistryFinishingTouches();

private void unexportRMIService(AFRegistry reg, AFRMIServiceImpl serv) throws AccessException,
RemoteException {
private synchronized void unexportRMIService(AFRegistry reg, AFRMIServiceImpl serv)
throws AccessException, RemoteException {
if (serv != null) {
serv.shutdownRegisteredCloseables();
}
Expand Down Expand Up @@ -418,7 +419,7 @@ public AFRegistry createRegistry() throws RemoteException {
* @return {@code true} if remote shutdown is allowed.
*/
public boolean isRemoteShutdownAllowed() {
return remoteShutdownAllowed;
return remoteShutdownAllowed.get();
}

/**
Expand All @@ -427,7 +428,7 @@ public boolean isRemoteShutdownAllowed() {
* @param remoteShutdownAllowed {@code true} if remote shutdown is allowed.
*/
public void setRemoteShutdownAllowed(boolean remoteShutdownAllowed) {
this.remoteShutdownAllowed = remoteShutdownAllowed;
this.remoteShutdownAllowed.set(remoteShutdownAllowed);
}

/**
Expand Down Expand Up @@ -507,7 +508,7 @@ public static void unexportObject(Remote obj) {
}
}

private void setRegistry(AFRegistry registry) {
private synchronized void setRegistry(AFRegistry registry) {
synchronized (AFNaming.class) {
this.registry = registry;
if (registry == null) {
Expand Down

0 comments on commit 2bef075

Please sign in to comment.