mirror of https://github.com/grpc/grpc-java.git
This commit is contained in:
parent
c5317e4935
commit
539f0400b6
|
|
@ -128,8 +128,14 @@ public final class SharedResourceHolder {
|
||||||
// AppEngine must immediately release shared resources, particularly executors
|
// AppEngine must immediately release shared resources, particularly executors
|
||||||
// which could retain request-scoped threads which become zombies after the request
|
// which could retain request-scoped threads which become zombies after the request
|
||||||
// completes.
|
// completes.
|
||||||
resource.close(instance);
|
// We do not encourage exceptions to be thrown during close, but we would like it to
|
||||||
instances.remove(resource);
|
// be able to recover eventually and do not want future resource fetches reuse the broken
|
||||||
|
// one.
|
||||||
|
try {
|
||||||
|
resource.close(instance);
|
||||||
|
} finally {
|
||||||
|
instances.remove(resource);
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
Preconditions.checkState(cached.destroyTask == null, "Destroy task already scheduled");
|
Preconditions.checkState(cached.destroyTask == null, "Destroy task already scheduled");
|
||||||
// Schedule a delayed task to destroy the resource.
|
// Schedule a delayed task to destroy the resource.
|
||||||
|
|
@ -142,11 +148,14 @@ public final class SharedResourceHolder {
|
||||||
synchronized (SharedResourceHolder.this) {
|
synchronized (SharedResourceHolder.this) {
|
||||||
// Refcount may have gone up since the task was scheduled. Re-check it.
|
// Refcount may have gone up since the task was scheduled. Re-check it.
|
||||||
if (cached.refcount == 0) {
|
if (cached.refcount == 0) {
|
||||||
resource.close(instance);
|
try {
|
||||||
instances.remove(resource);
|
resource.close(instance);
|
||||||
if (instances.isEmpty()) {
|
} finally {
|
||||||
destroyer.shutdown();
|
instances.remove(resource);
|
||||||
destroyer = null;
|
if (instances.isEmpty()) {
|
||||||
|
destroyer.shutdown();
|
||||||
|
destroyer = null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -173,6 +173,34 @@ public class SharedResourceHolderTest {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test public void handleInstanceCloseError() {
|
||||||
|
class ExceptionOnCloseResource implements Resource<ResourceInstance> {
|
||||||
|
@Override
|
||||||
|
public ResourceInstance create() {
|
||||||
|
return new ResourceInstance();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void close(ResourceInstance instance) {
|
||||||
|
throw new RuntimeException();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Resource<ResourceInstance> resource = new ExceptionOnCloseResource();
|
||||||
|
ResourceInstance instance = holder.getInternal(resource);
|
||||||
|
holder.releaseInternal(resource, instance);
|
||||||
|
MockScheduledFuture<?> scheduledDestroyTask = scheduledDestroyTasks.poll();
|
||||||
|
try {
|
||||||
|
scheduledDestroyTask.runTask();
|
||||||
|
fail("Should throw RuntimeException");
|
||||||
|
} catch (RuntimeException e) {
|
||||||
|
// expected
|
||||||
|
}
|
||||||
|
|
||||||
|
// Future resource fetches should not get the partially-closed one.
|
||||||
|
assertNotSame(instance, holder.getInternal(resource));
|
||||||
|
}
|
||||||
|
|
||||||
private class MockExecutorFactory implements
|
private class MockExecutorFactory implements
|
||||||
SharedResourceHolder.ScheduledExecutorFactory {
|
SharedResourceHolder.ScheduledExecutorFactory {
|
||||||
@Override
|
@Override
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue