Skip to content

Commit

Permalink
8302845: Replace finalizer usage in JNDI DNS provider with Cleaner
Browse files Browse the repository at this point in the history
Reviewed-by: alanb, dfuchs, djelinski
  • Loading branch information
AlekseiEfimov committed May 10, 2023
1 parent 540c706 commit da2c930
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 17 deletions.
3 changes: 2 additions & 1 deletion src/java.base/share/classes/module-info.java
Expand Up @@ -246,7 +246,8 @@
jdk.jfr;
exports jdk.internal.ref to
java.desktop,
java.net.http;
java.net.http,
jdk.naming.dns;
exports jdk.internal.reflect to
java.logging,
java.sql,
Expand Down
25 changes: 15 additions & 10 deletions src/jdk.naming.dns/share/classes/com/sun/jndi/dns/DnsClient.java
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2000, 2022, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2000, 2023, 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 @@ -27,6 +27,7 @@

import java.io.IOException;
import java.io.UncheckedIOException;
import java.lang.ref.Cleaner;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.PortUnreachableException;
Expand All @@ -51,6 +52,7 @@
import java.util.HashMap;
import java.util.concurrent.locks.ReentrantLock;

import jdk.internal.ref.CleanerFactory;
import sun.security.jca.JCAUtil;

// Some of this code began life as part of sun.javaos.net.DnsClient
Expand Down Expand Up @@ -103,6 +105,7 @@ public class DnsClient {
private final ReentrantLock udpChannelLock = new ReentrantLock();

private final Selector udpChannelSelector;
private final Cleaner.Cleanable selectorCleanup;

private static final DNSDatagramChannelFactory factory =
new DNSDatagramChannelFactory(random);
Expand Down Expand Up @@ -158,6 +161,16 @@ public DnsClient(String[] servers, int timeout, int retries)
ne.setRootCause(e);
throw ne;
}
// register a cleaning action to close the Selector
// when this DNS client becomes phantom reachable
Selector sel = udpChannelSelector;
selectorCleanup = CleanerFactory.cleaner().register(this, () -> {
try {
sel.close();
} catch (IOException ignore) {
}
});

reqs = Collections.synchronizedMap(
new HashMap<Integer, ResourceRecord>());
resps = Collections.synchronizedMap(new HashMap<Integer, byte[]>());
Expand All @@ -173,19 +186,11 @@ DatagramChannel getDatagramChannel() throws NamingException {
}
}

@SuppressWarnings("removal")
protected void finalize() {
close();
}

// A lock to access the request and response queues in tandem.
private Object queuesLock = new Object();

public void close() {
try {
udpChannelSelector.close();
} catch (IOException ioException) {
}
selectorCleanup.clean();
synchronized (queuesLock) {
reqs.clear();
resps.clear();
Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2000, 2023, 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 @@ -1050,11 +1050,6 @@ final class BindingEnumeration extends BaseNameClassPairEnumeration<Binding>
super(ctx, nodes);
}

// Finalizer not needed since it's safe to leave ctx unclosed.
// protected void finalize() {
// close();
// }

@Override
public Binding next() throws NamingException {
if (!hasMore()) {
Expand Down

1 comment on commit da2c930

@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.