1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
|
/*
* Copyright (c) 2001, 2016, 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
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/* @test
* @bug 4017232 8046339
* @summary If, after returning a reference to a remote object in the current
* VM (which gets implicitly converted to a remote stub), the client fails to
* both send a DGC dirty call and to send a "DGC acknowledgment", the RMI
* runtime should eventually allow the remote object to be garbage collected,
* rather than pinning it indefinitely.
* @author Peter Jones
*
* @modules java.rmi/sun.rmi.transport:+open
* @build DGCAckFailure DGCAckFailure_Stub
* @run main/othervm DGCAckFailure
*/
import java.io.*;
import java.net.*;
import java.lang.reflect.Field;
import java.lang.ref.*;
import java.rmi.*;
import java.rmi.server.*;
import java.util.Map;
import sun.rmi.transport.DGCAckHandler;
interface ReturnRemote extends Remote {
Object returnRemote() throws RemoteException;
}
public class DGCAckFailure implements ReturnRemote {
private static final long TIMEOUT = 20000;
private static final long ACK_TIMEOUT = TIMEOUT / 2;
public Object returnRemote() {
return new Wrapper(this);
}
public static void main(String[] args) throws Exception {
System.setProperty("sun.rmi.dgc.ackTimeout",
Long.toString(ACK_TIMEOUT));
/*
* Set a socket factory that has a hook for shutting down all client
* output (writes from client-created sockets and new connection
* attempts). We then use this hook right before a remote stub gets
* deserialized, so that the client will not be able to send a DGC
* dirty call, or a DGC acknowledgment. Without the DGC ack, we
* hope that the RMI runtime will still eventually allow the remote
* object to be garbage collected.
*/
RMISocketFactory.setSocketFactory(new TestSF());
System.err.println("test socket factory set");
Remote impl = new DGCAckFailure();
ReferenceQueue refQueue = new ReferenceQueue();
Reference weakRef = new WeakReference(impl, refQueue);
ReturnRemote stub =
(ReturnRemote) UnicastRemoteObject.exportObject(impl);
System.err.println("remote object exported; stub = " + stub);
try {
Object wrappedStub = stub.returnRemote();
System.err.println("invocation returned: " + wrappedStub);
impl = null;
stub = null; // in case 4114579 ever gets fixed
System.err.println("strong references to impl cleared");
System.err.println("waiting for weak reference notification:");
Reference ref = null;
for (int i = 0; i < 6; i++) {
System.gc();
ref = refQueue.remove(TIMEOUT / 5);
if (ref != null) {
break;
}
}
if (ref != weakRef) {
throw new RuntimeException("TEST FAILED: " +
"timed out, remote object not garbage collected");
}
// 8046339
// All DGCAckHandlers must be properly released after timeout
Thread.sleep(ACK_TIMEOUT + 100);
try {
Field field =
DGCAckHandler.class.getDeclaredField("idTable");
field.setAccessible(true);
Object obj = field.get(null);
Map<?,?> idTable = (Map<?,?>)obj;
if (!idTable.isEmpty()) {
throw new RuntimeException("TEST FAILED: " +
"DGCAckHandler.idTable isn't empty");
}
} catch (ReflectiveOperationException roe) {
throw new RuntimeException(roe);
}
System.err.println("TEST PASSED");
} finally {
try {
UnicastRemoteObject.unexportObject((Remote) weakRef.get(),
true);
} catch (Exception e) {
}
}
}
private static class Wrapper implements Serializable {
private final Remote obj;
Wrapper(Remote obj) { this.obj = obj; }
private void readObject(ObjectInputStream in)
throws IOException, ClassNotFoundException
{
TestSF.shutdownClientOutput();
System.err.println(
"Wrapper.readObject: SHUTTING DOWN CLIENT OUTPUT");
in.defaultReadObject();
}
public String toString() { return "Wrapper[" + obj + "]"; }
}
private static class TestSF extends RMISocketFactory {
private static volatile boolean shutdown = false;
static void shutdownClientOutput() { shutdown = true; }
public Socket createSocket(String host, int port) throws IOException {
if (shutdown) {
IOException e = new java.net.ConnectException(
"test socket factory rejecting client connection");
System.err.println(e);
// e.printStackTrace();
throw e;
} else {
return new TestSocket(host, port);
}
}
public ServerSocket createServerSocket(int port) throws IOException {
return new ServerSocket(port);
}
private static class TestSocket extends Socket {
TestSocket(String host, int port) throws IOException {
super(host, port);
}
public OutputStream getOutputStream() throws IOException {
return new TestOutputStream(super.getOutputStream());
}
}
private static class TestOutputStream extends FilterOutputStream {
TestOutputStream(OutputStream out) { super(out); }
public void write(int b) throws IOException {
if (shutdown) {
IOException e = new IOException(
"connection broken by test socket factory");
System.err.println(e);
// e.printStackTrace();
throw e;
} else {
super.write(b);
}
}
}
}
}
|