Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: prevent hanging by call backgroundResources.close() on stub.close() [ggj] #804

Merged
merged 23 commits into from
Aug 2, 2021
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
9ea4717
feat(ast): Add support for throwable causes
miraleung Aug 2, 2021
4ac2d79
fix: call backgroundResources.close() on stub.close()
miraleung Aug 2, 2021
01c0f1a
Merge branch 'master' of github.com:googleapis/gapic-generator-java i…
miraleung Jul 30, 2021
f15ec26
Merge branch 'dev/throw-cause' of github.com:googleapis/gapic-generat…
miraleung Jul 30, 2021
70b657b
Merge branch 'master' into dev/throw-cause
miraleung Jul 30, 2021
e72e2e9
Merge branch 'master' into dev/throw-cause
miraleung Jul 30, 2021
5ea11bf
fix: make Throwable a static final in TypeNode
miraleung Jul 30, 2021
897c881
Merge branch 'dev/throw-cause' of github.com:googleapis/gapic-generat…
miraleung Jul 30, 2021
3c85b99
Merge branch 'dev/throw-cause' of github.com:googleapis/gapic-generat…
miraleung Jul 30, 2021
fdd1f44
Merge branch 'dev/throw-cause' of github.com:googleapis/gapic-generat…
miraleung Jul 30, 2021
17a1440
Merge branch 'master' of github.com:googleapis/gapic-generator-java i…
miraleung Aug 2, 2021
bf55a60
Merge branch 'master' into dev/stub-close
miraleung Aug 2, 2021
7ea5032
fix: update goldens
miraleung Aug 2, 2021
f986360
Merge branch 'dev/stub-close' of github.com:googleapis/gapic-generato…
miraleung Aug 2, 2021
d5fc5cf
feat(ast): support throwing all kinds of expressions
miraleung Aug 2, 2021
68fb465
fix: call backgroundResources.close() on stub.close()
miraleung Aug 2, 2021
6de4e5e
fix: update goldens
miraleung Aug 2, 2021
1c57844
feat(ast): Add support for multi-catch blocks
miraleung Aug 2, 2021
12c8836
fix: add extra catch block
miraleung Aug 2, 2021
d683370
Merge branch 'dev/stub-close' of github.com:googleapis/gapic-generato…
miraleung Aug 2, 2021
552af72
fix: isolate stub.close change to another PR
miraleung Aug 2, 2021
741d685
Merge branch 'dev/try-catch-list' of github.com:googleapis/gapic-gene…
miraleung Aug 2, 2021
e959ac1
fix: merge branches
miraleung Aug 2, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions src/main/java/com/google/api/generator/engine/ast/ThrowExpr.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ public abstract class ThrowExpr implements Expr {
@Nullable
public abstract Expr messageExpr();

@Nullable
public abstract Expr causeExpr();

@Override
public void accept(AstNodeVisitor visitor) {
visitor.visit(this);
Expand All @@ -47,22 +50,36 @@ public Builder setMessageExpr(String message) {

public abstract Builder setMessageExpr(Expr expr);

public abstract Builder setCauseExpr(Expr expr);

// Private.
abstract TypeNode type();

abstract Expr messageExpr();

abstract Expr causeExpr();

abstract ThrowExpr autoBuild();

public ThrowExpr build() {
Preconditions.checkState(
TypeNode.isExceptionType(type()),
String.format("Type %s must be an exception type", type()));

if (messageExpr() != null) {
Preconditions.checkState(
messageExpr().type().equals(TypeNode.STRING),
String.format("Message expression type must be a string for exception %s", type()));
}

if (causeExpr() != null) {
Preconditions.checkState(
TypeNode.THROWABLE.reference().isSupertypeOrEquals(causeExpr().type().reference()),
String.format(
"Cause expression type must be a subclass of Throwable, but found %s",
causeExpr().type()));
}

return autoBuild();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@ public enum TypeKind {
public static final TypeNode STRING = withReference(ConcreteReference.withClazz(String.class));
public static final TypeNode VOID_OBJECT = withReference(ConcreteReference.withClazz(Void.class));

public static final TypeNode THROWABLE =
withReference(ConcreteReference.withClazz(Throwable.class));

public static final TypeNode DEPRECATED =
withReference(ConcreteReference.withClazz(Deprecated.class));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,9 @@ public void visit(ThrowExpr throwExpr) {
if (throwExpr.messageExpr() != null) {
throwExpr.messageExpr().accept(this);
}
if (throwExpr.causeExpr() != null) {
throwExpr.causeExpr().accept(this);
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,13 @@ public void visit(ThrowExpr throwExpr) {
if (throwExpr.messageExpr() != null) {
throwExpr.messageExpr().accept(this);
}
if (throwExpr.causeExpr() != null) {
if (throwExpr.messageExpr() != null) {
buffer.append(COMMA);
space();
}
throwExpr.causeExpr().accept(this);
}
rightParen();
}

Expand Down
Original file line number Diff line number Diff line change
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,23 @@ private List<MethodDefinition> createStubOverrideMethods(
.build())
.build();

// Generate the close() method:
// @Override
// public final void close() {
// try {
// backgroundResources.close();
// } catch (Exception e) {
// throw new IllegalStateException("Failed to close resource", e);
// }
// }
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 +818,27 @@ 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())))
.setCatchVariableExpr(
catchExceptionVarExpr.toBuilder().setIsDecl(true).build())
.setCatchBody(
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
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,65 @@ public void createThrowExpr_badMessageExpr() {
IllegalStateException.class,
() -> ThrowExpr.builder().setType(npeType).setMessageExpr(messageExpr).build());
}

@Test
public void createThrowExpr_causeExpr() {
TypeNode npeType =
TypeNode.withReference(ConcreteReference.withClazz(NullPointerException.class));
ThrowExpr.builder()
.setType(npeType)
.setCauseExpr(
NewObjectExpr.builder()
.setType(TypeNode.withReference(ConcreteReference.withClazz(Throwable.class)))
.build())
.build();
// Successfully created a ThrowExpr.
}

@Test
public void createThrowExpr_causeExpr_throwableSubtype() {
TypeNode npeType =
TypeNode.withReference(ConcreteReference.withClazz(NullPointerException.class));
ThrowExpr.builder()
.setType(npeType)
.setCauseExpr(
NewObjectExpr.builder()
.setType(TypeNode.withExceptionClazz(IllegalStateException.class))
.build())
.build();
// Successfully created a ThrowExpr.
}

@Test
public void createThrowExpr_causeExpr_onThrowableSubtype() {
TypeNode npeType =
TypeNode.withReference(ConcreteReference.withClazz(NullPointerException.class));
assertThrows(
IllegalStateException.class,
() ->
ThrowExpr.builder()
.setType(npeType)
.setCauseExpr(NewObjectExpr.builder().setType(TypeNode.STRING).build())
.build());
}

@Test
public void createThrowExpr_messageAndCauseExpr() {
TypeNode npeType =
TypeNode.withReference(ConcreteReference.withClazz(NullPointerException.class));
Expr messageExpr =
MethodInvocationExpr.builder()
.setMethodName("foobar")
.setReturnType(TypeNode.STRING)
.build();
ThrowExpr.builder()
.setType(npeType)
.setMessageExpr(messageExpr)
.setCauseExpr(
NewObjectExpr.builder()
.setType(TypeNode.withReference(ConcreteReference.withClazz(Throwable.class)))
.build())
.build();
// Successfully created a ThrowExpr.
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
import com.google.common.base.Function;
import com.google.common.base.Strings;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
Expand Down Expand Up @@ -475,6 +476,42 @@ public void writeThrowExprImports_messageExpr() {
writerVisitor.write());
}

@Test
public void writeThrowExprImports_messageAndCauseExpr() {
TypeNode npeType = TypeNode.withExceptionClazz(NullPointerException.class);
Expr messageExpr =
MethodInvocationExpr.builder()
.setStaticReferenceType(
TypeNode.withReference(ConcreteReference.withClazz(IfStatement.class)))
.setMethodName("conditionExpr")
.setReturnType(TypeNode.withReference(ConcreteReference.withClazz(Expr.class)))
.build();

messageExpr =
MethodInvocationExpr.builder()
.setExprReferenceExpr(messageExpr)
.setMethodName("foobar")
.setReturnType(TypeNode.STRING)
.build();
ThrowExpr throwExpr =
ThrowExpr.builder()
.setType(npeType)
.setMessageExpr(messageExpr)
.setCauseExpr(
NewObjectExpr.builder()
.setType(TypeNode.withExceptionClazz(FileNotFoundException.class))
.build())
.build();

throwExpr.accept(writerVisitor);
assertEquals(
LineFormatter.lines(
"import com.google.api.generator.engine.ast.Expr;\n",
"import com.google.api.generator.engine.ast.IfStatement;\n",
"import java.io.FileNotFoundException;\n\n"),
writerVisitor.write());
}

@Test
public void writeInstanceofExprImports_basic() {
TypeNode exprType = TypeNode.withReference(ConcreteReference.withClazz(Expr.class));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1041,6 +1041,22 @@ public void writeThrowExpr_basicWithMessage() {
assertEquals("throw new NullPointerException(\"Some message asdf\")", writerVisitor.write());
}

@Test
public void writeThrowExpr_basicWithCause() {
TypeNode npeType =
TypeNode.withReference(ConcreteReference.withClazz(NullPointerException.class));
ThrowExpr throwExpr =
ThrowExpr.builder()
.setType(npeType)
.setCauseExpr(
NewObjectExpr.builder()
.setType(TypeNode.withReference(ConcreteReference.withClazz(Throwable.class)))
.build())
.build();
throwExpr.accept(writerVisitor);
assertEquals("throw new NullPointerException(new Throwable())", writerVisitor.write());
}

@Test
public void writeThrowExpr_messageExpr() {
TypeNode npeType = TypeNode.withExceptionClazz(NullPointerException.class);
Expand All @@ -1055,6 +1071,29 @@ public void writeThrowExpr_messageExpr() {
assertEquals("throw new NullPointerException(foobar())", writerVisitor.write());
}

@Test
public void writeThrowExpr_messageAndCauseExpr() {
TypeNode npeType = TypeNode.withExceptionClazz(NullPointerException.class);
Expr messageExpr =
MethodInvocationExpr.builder()
.setMethodName("foobar")
.setReturnType(TypeNode.STRING)
.build();
ThrowExpr throwExpr =
ThrowExpr.builder()
.setType(npeType)
.setMessageExpr(messageExpr)
.setCauseExpr(
NewObjectExpr.builder()
.setType(TypeNode.withReference(ConcreteReference.withClazz(Throwable.class)))
.build())
.build();

throwExpr.accept(writerVisitor);
assertEquals(
"throw new NullPointerException(foobar(), new Throwable())", writerVisitor.write());
}

@Test
public void writeInstanceofExpr() {
Variable variable = Variable.builder().setName("x").setType(TypeNode.STRING).build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,11 @@ public class GrpcDeprecatedServiceStub extends DeprecatedServiceStub {

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

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,11 @@ public class GrpcEchoStub extends EchoStub {

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

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,11 @@ public class GrpcPublisherStub extends PublisherStub {

@Override
public final void close() {
shutdown();
try {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like this (exception wrapping) exists solely because backgroundResources.close() is checked (throws Exception), but the EchoStub.close()is declared as non-checked, so we need do something like that for things to compile. Either way it will be a breaking behavioral change. To mitigate it, I think we can wrap only what we must (checked exceptions), but throw as is all the unchecked exceptions, so please consider generating somethign like this instead:

   try {
      backgroundResources.close();
    } catch (RungimeException e) {
      thrown e;
    } catch (Exception e) {
      throw new IllegalStateException("Failed to close resource", e);
    }
  }

This will let us to avoid unnecessary exception wrapping, making this change differ less from the previous behavior.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

backgroundResources.close();
} catch (Exception e) {
throw new IllegalStateException("Failed to close resource", e);
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,11 @@ public class GrpcTestingStub extends TestingStub {

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

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -484,7 +484,11 @@ public class HttpJsonComplianceStub extends ComplianceStub {

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

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -598,7 +598,11 @@ public UnaryCallable<DeleteFeedRequest, Empty> deleteFeedCallable() {

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

@Override
Expand Down
Loading