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 11 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,11 @@ public UnaryCallable<ListAddressesRequest, ListPagedResponse> listPagedCallable(

@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 @@ -167,7 +167,11 @@ public UnaryCallable<GetRegionOperationRequest, Operation> getCallable() {

@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 @@ -239,7 +239,11 @@ public UnaryCallable<SignJwtRequest, SignJwtResponse> signJwtCallable() {

@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 @@ -197,7 +197,11 @@ public UnaryCallable<GetIamPolicyRequest, Policy> getIamPolicyCallable() {

@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 @@ -1119,7 +1119,11 @@ public UnaryCallable<GetLocationRequest, Location> getLocationCallable() {

@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 @@ -450,7 +450,11 @@ public UnaryCallable<MoveBookRequest, Book> moveBookCallable() {

@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 @@ -889,7 +889,11 @@ public UnaryCallable<UpdateCmekSettingsRequest, CmekSettings> updateCmekSettings

@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 @@ -328,7 +328,11 @@ public UnaryCallable<ListLogsRequest, ListLogsPagedResponse> listLogsPagedCallab

@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 @@ -279,7 +279,11 @@ public UnaryCallable<DeleteLogMetricRequest, Empty> deleteLogMetricCallable() {

@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 @@ -549,7 +549,11 @@ public UnaryCallable<GetIamPolicyRequest, Policy> getIamPolicyCallable() {

@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 @@ -412,7 +412,11 @@ public UnaryCallable<GetIamPolicyRequest, Policy> getIamPolicyCallable() {

@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 @@ -762,7 +762,11 @@ public UnaryCallable<GetIamPolicyRequest, Policy> getIamPolicyCallable() {

@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 @@ -505,7 +505,11 @@ public OperationCallable<DeleteInstanceRequest, Empty, Any> deleteInstanceOperat

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

@Override
Expand Down