Skip to content

Commit

Permalink
fix: prevent hanging by call backgroundResources.close() on stub.clos…
Browse files Browse the repository at this point in the history
…e() [ggj] (#804)

* feat(ast): Add support for throwable causes

* fix: call backgroundResources.close() on stub.close()

* fix: make Throwable a static final in TypeNode

* fix: update goldens

* feat(ast): support throwing all kinds of expressions

* fix: call backgroundResources.close() on stub.close()

* fix: update goldens

* feat(ast): Add support for multi-catch blocks

* fix: add extra catch block

* fix: isolate stub.close change to another PR

* fix: merge branches
  • Loading branch information
miraleung committed Aug 2, 2021
1 parent 55ef1a6 commit 428db97
Show file tree
Hide file tree
Showing 21 changed files with 197 additions and 22 deletions.
Expand Up @@ -40,6 +40,8 @@
import com.google.api.generator.engine.ast.ScopeNode;
import com.google.api.generator.engine.ast.Statement;
import com.google.api.generator.engine.ast.ThisObjectValue;
import com.google.api.generator.engine.ast.ThrowExpr;
import com.google.api.generator.engine.ast.TryCatchStatement;
import com.google.api.generator.engine.ast.TypeNode;
import com.google.api.generator.engine.ast.ValueExpr;
import com.google.api.generator.engine.ast.Variable;
Expand Down Expand Up @@ -791,6 +793,34 @@ private List<MethodDefinition> createStubOverrideMethods(
.build())
.build();

// Generate the close() method:
// @Override
// public final void close() {
// try {
// backgroundResources.close();
// } catch (RuntimeException e) {
// throw e;
// } catch (Exception e) {
// throw new IllegalStateException("Failed to close resource", e);
// }
// }

VariableExpr catchRuntimeExceptionVarExpr =
VariableExpr.builder()
.setVariable(
Variable.builder()
.setType(TypeNode.withExceptionClazz(RuntimeException.class))
.setName("e")
.build())
.build();
VariableExpr catchExceptionVarExpr =
VariableExpr.builder()
.setVariable(
Variable.builder()
.setType(TypeNode.withExceptionClazz(Exception.class))
.setName("e")
.build())
.build();
List<MethodDefinition> javaMethods = new ArrayList<>();
javaMethods.add(
methodMakerStarterFn
Expand All @@ -799,8 +829,33 @@ private List<MethodDefinition> createStubOverrideMethods(
.setReturnType(TypeNode.VOID)
.setBody(
Arrays.asList(
ExprStatement.withExpr(
MethodInvocationExpr.builder().setMethodName("shutdown").build())))
TryCatchStatement.builder()
.setTryBody(
Arrays.asList(
ExprStatement.withExpr(
MethodInvocationExpr.builder()
.setExprReferenceExpr(backgroundResourcesVarExpr)
.setMethodName("close")
.build())))
.addCatch(
catchRuntimeExceptionVarExpr.toBuilder().setIsDecl(true).build(),
Arrays.asList(
ExprStatement.withExpr(
ThrowExpr.builder()
.setThrowExpr(catchRuntimeExceptionVarExpr)
.build())))
.addCatch(
catchExceptionVarExpr.toBuilder().setIsDecl(true).build(),
Arrays.asList(
ExprStatement.withExpr(
ThrowExpr.builder()
.setType(
TypeNode.withExceptionClazz(
IllegalStateException.class))
.setMessageExpr(String.format("Failed to close resource"))
.setCauseExpr(catchExceptionVarExpr)
.build())))
.build()))
.build());
javaMethods.add(voidMethodMakerFn.apply("shutdown"));
javaMethods.add(booleanMethodMakerFn.apply("isShutdown"));
Expand Down
Expand Up @@ -125,7 +125,13 @@ public class GrpcDeprecatedServiceStub extends DeprecatedServiceStub {

@Override
public final void close() {
shutdown();
try {
backgroundResources.close();
} catch (RuntimeException e) {
throw e;
} catch (Exception e) {
throw new IllegalStateException("Failed to close resource", e);
}
}

@Override
Expand Down
Expand Up @@ -341,7 +341,13 @@ public class GrpcEchoStub extends EchoStub {

@Override
public final void close() {
shutdown();
try {
backgroundResources.close();
} catch (RuntimeException e) {
throw e;
} catch (Exception e) {
throw new IllegalStateException("Failed to close resource", e);
}
}

@Override
Expand Down
Expand Up @@ -430,7 +430,13 @@ public class GrpcPublisherStub extends PublisherStub {

@Override
public final void close() {
shutdown();
try {
backgroundResources.close();
} catch (RuntimeException e) {
throw e;
} catch (Exception e) {
throw new IllegalStateException("Failed to close resource", e);
}
}

@Override
Expand Down
Expand Up @@ -382,7 +382,13 @@ public class GrpcTestingStub extends TestingStub {

@Override
public final void close() {
shutdown();
try {
backgroundResources.close();
} catch (RuntimeException e) {
throw e;
} catch (Exception e) {
throw new IllegalStateException("Failed to close resource", e);
}
}

@Override
Expand Down
Expand Up @@ -484,7 +484,13 @@ public class HttpJsonComplianceStub extends ComplianceStub {

@Override
public final void close() {
shutdown();
try {
backgroundResources.close();
} catch (RuntimeException e) {
throw e;
} catch (Exception e) {
throw new IllegalStateException("Failed to close resource", e);
}
}

@Override
Expand Down
Expand Up @@ -598,7 +598,13 @@ public UnaryCallable<DeleteFeedRequest, Empty> deleteFeedCallable() {

@Override
public final void close() {
shutdown();
try {
backgroundResources.close();
} catch (RuntimeException e) {
throw e;
} catch (Exception e) {
throw new IllegalStateException("Failed to close resource", e);
}
}

@Override
Expand Down
Expand Up @@ -403,7 +403,13 @@ public UnaryCallable<ListAddressesRequest, ListPagedResponse> listPagedCallable(

@Override
public final void close() {
shutdown();
try {
backgroundResources.close();
} catch (RuntimeException e) {
throw e;
} catch (Exception e) {
throw new IllegalStateException("Failed to close resource", e);
}
}

@Override
Expand Down
Expand Up @@ -167,7 +167,13 @@ public UnaryCallable<GetRegionOperationRequest, Operation> getCallable() {

@Override
public final void close() {
shutdown();
try {
backgroundResources.close();
} catch (RuntimeException e) {
throw e;
} catch (Exception e) {
throw new IllegalStateException("Failed to close resource", e);
}
}

@Override
Expand Down
Expand Up @@ -239,7 +239,13 @@ public UnaryCallable<SignJwtRequest, SignJwtResponse> signJwtCallable() {

@Override
public final void close() {
shutdown();
try {
backgroundResources.close();
} catch (RuntimeException e) {
throw e;
} catch (Exception e) {
throw new IllegalStateException("Failed to close resource", e);
}
}

@Override
Expand Down
Expand Up @@ -197,7 +197,13 @@ public UnaryCallable<GetIamPolicyRequest, Policy> getIamPolicyCallable() {

@Override
public final void close() {
shutdown();
try {
backgroundResources.close();
} catch (RuntimeException e) {
throw e;
} catch (Exception e) {
throw new IllegalStateException("Failed to close resource", e);
}
}

@Override
Expand Down
Expand Up @@ -1119,7 +1119,13 @@ public UnaryCallable<GetLocationRequest, Location> getLocationCallable() {

@Override
public final void close() {
shutdown();
try {
backgroundResources.close();
} catch (RuntimeException e) {
throw e;
} catch (Exception e) {
throw new IllegalStateException("Failed to close resource", e);
}
}

@Override
Expand Down
Expand Up @@ -450,7 +450,13 @@ public UnaryCallable<MoveBookRequest, Book> moveBookCallable() {

@Override
public final void close() {
shutdown();
try {
backgroundResources.close();
} catch (RuntimeException e) {
throw e;
} catch (Exception e) {
throw new IllegalStateException("Failed to close resource", e);
}
}

@Override
Expand Down
Expand Up @@ -889,7 +889,13 @@ public UnaryCallable<UpdateCmekSettingsRequest, CmekSettings> updateCmekSettings

@Override
public final void close() {
shutdown();
try {
backgroundResources.close();
} catch (RuntimeException e) {
throw e;
} catch (Exception e) {
throw new IllegalStateException("Failed to close resource", e);
}
}

@Override
Expand Down
Expand Up @@ -328,7 +328,13 @@ public UnaryCallable<ListLogsRequest, ListLogsPagedResponse> listLogsPagedCallab

@Override
public final void close() {
shutdown();
try {
backgroundResources.close();
} catch (RuntimeException e) {
throw e;
} catch (Exception e) {
throw new IllegalStateException("Failed to close resource", e);
}
}

@Override
Expand Down
Expand Up @@ -279,7 +279,13 @@ public UnaryCallable<DeleteLogMetricRequest, Empty> deleteLogMetricCallable() {

@Override
public final void close() {
shutdown();
try {
backgroundResources.close();
} catch (RuntimeException e) {
throw e;
} catch (Exception e) {
throw new IllegalStateException("Failed to close resource", e);
}
}

@Override
Expand Down
Expand Up @@ -549,7 +549,13 @@ public UnaryCallable<GetIamPolicyRequest, Policy> getIamPolicyCallable() {

@Override
public final void close() {
shutdown();
try {
backgroundResources.close();
} catch (RuntimeException e) {
throw e;
} catch (Exception e) {
throw new IllegalStateException("Failed to close resource", e);
}
}

@Override
Expand Down
Expand Up @@ -412,7 +412,13 @@ public UnaryCallable<GetIamPolicyRequest, Policy> getIamPolicyCallable() {

@Override
public final void close() {
shutdown();
try {
backgroundResources.close();
} catch (RuntimeException e) {
throw e;
} catch (Exception e) {
throw new IllegalStateException("Failed to close resource", e);
}
}

@Override
Expand Down
Expand Up @@ -762,7 +762,13 @@ public UnaryCallable<GetIamPolicyRequest, Policy> getIamPolicyCallable() {

@Override
public final void close() {
shutdown();
try {
backgroundResources.close();
} catch (RuntimeException e) {
throw e;
} catch (Exception e) {
throw new IllegalStateException("Failed to close resource", e);
}
}

@Override
Expand Down
Expand Up @@ -505,7 +505,13 @@ public OperationCallable<DeleteInstanceRequest, Empty, Any> deleteInstanceOperat

@Override
public final void close() {
shutdown();
try {
backgroundResources.close();
} catch (RuntimeException e) {
throw e;
} catch (Exception e) {
throw new IllegalStateException("Failed to close resource", e);
}
}

@Override
Expand Down
Expand Up @@ -201,7 +201,13 @@ public ClientStreamingCallable<WriteObjectRequest, WriteObjectResponse> writeObj

@Override
public final void close() {
shutdown();
try {
backgroundResources.close();
} catch (RuntimeException e) {
throw e;
} catch (Exception e) {
throw new IllegalStateException("Failed to close resource", e);
}
}

@Override
Expand Down

0 comments on commit 428db97

Please sign in to comment.