Skip to content

Commit 428db97

Browse files
authored
fix: prevent hanging by call backgroundResources.close() on stub.close() [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
1 parent 55ef1a6 commit 428db97

21 files changed

Lines changed: 197 additions & 22 deletions

File tree

src/main/java/com/google/api/generator/gapic/composer/common/AbstractServiceStubClassComposer.java

Lines changed: 57 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@
4040
import com.google.api.generator.engine.ast.ScopeNode;
4141
import com.google.api.generator.engine.ast.Statement;
4242
import com.google.api.generator.engine.ast.ThisObjectValue;
43+
import com.google.api.generator.engine.ast.ThrowExpr;
44+
import com.google.api.generator.engine.ast.TryCatchStatement;
4345
import com.google.api.generator.engine.ast.TypeNode;
4446
import com.google.api.generator.engine.ast.ValueExpr;
4547
import com.google.api.generator.engine.ast.Variable;
@@ -791,6 +793,34 @@ private List<MethodDefinition> createStubOverrideMethods(
791793
.build())
792794
.build();
793795

796+
// Generate the close() method:
797+
// @Override
798+
// public final void close() {
799+
// try {
800+
// backgroundResources.close();
801+
// } catch (RuntimeException e) {
802+
// throw e;
803+
// } catch (Exception e) {
804+
// throw new IllegalStateException("Failed to close resource", e);
805+
// }
806+
// }
807+
808+
VariableExpr catchRuntimeExceptionVarExpr =
809+
VariableExpr.builder()
810+
.setVariable(
811+
Variable.builder()
812+
.setType(TypeNode.withExceptionClazz(RuntimeException.class))
813+
.setName("e")
814+
.build())
815+
.build();
816+
VariableExpr catchExceptionVarExpr =
817+
VariableExpr.builder()
818+
.setVariable(
819+
Variable.builder()
820+
.setType(TypeNode.withExceptionClazz(Exception.class))
821+
.setName("e")
822+
.build())
823+
.build();
794824
List<MethodDefinition> javaMethods = new ArrayList<>();
795825
javaMethods.add(
796826
methodMakerStarterFn
@@ -799,8 +829,33 @@ private List<MethodDefinition> createStubOverrideMethods(
799829
.setReturnType(TypeNode.VOID)
800830
.setBody(
801831
Arrays.asList(
802-
ExprStatement.withExpr(
803-
MethodInvocationExpr.builder().setMethodName("shutdown").build())))
832+
TryCatchStatement.builder()
833+
.setTryBody(
834+
Arrays.asList(
835+
ExprStatement.withExpr(
836+
MethodInvocationExpr.builder()
837+
.setExprReferenceExpr(backgroundResourcesVarExpr)
838+
.setMethodName("close")
839+
.build())))
840+
.addCatch(
841+
catchRuntimeExceptionVarExpr.toBuilder().setIsDecl(true).build(),
842+
Arrays.asList(
843+
ExprStatement.withExpr(
844+
ThrowExpr.builder()
845+
.setThrowExpr(catchRuntimeExceptionVarExpr)
846+
.build())))
847+
.addCatch(
848+
catchExceptionVarExpr.toBuilder().setIsDecl(true).build(),
849+
Arrays.asList(
850+
ExprStatement.withExpr(
851+
ThrowExpr.builder()
852+
.setType(
853+
TypeNode.withExceptionClazz(
854+
IllegalStateException.class))
855+
.setMessageExpr(String.format("Failed to close resource"))
856+
.setCauseExpr(catchExceptionVarExpr)
857+
.build())))
858+
.build()))
804859
.build());
805860
javaMethods.add(voidMethodMakerFn.apply("shutdown"));
806861
javaMethods.add(booleanMethodMakerFn.apply("isShutdown"));

src/test/java/com/google/api/generator/gapic/composer/grpc/goldens/GrpcDeprecatedServiceStub.golden

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,13 @@ public class GrpcDeprecatedServiceStub extends DeprecatedServiceStub {
125125

126126
@Override
127127
public final void close() {
128-
shutdown();
128+
try {
129+
backgroundResources.close();
130+
} catch (RuntimeException e) {
131+
throw e;
132+
} catch (Exception e) {
133+
throw new IllegalStateException("Failed to close resource", e);
134+
}
129135
}
130136

131137
@Override

src/test/java/com/google/api/generator/gapic/composer/grpc/goldens/GrpcEchoStub.golden

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,13 @@ public class GrpcEchoStub extends EchoStub {
341341

342342
@Override
343343
public final void close() {
344-
shutdown();
344+
try {
345+
backgroundResources.close();
346+
} catch (RuntimeException e) {
347+
throw e;
348+
} catch (Exception e) {
349+
throw new IllegalStateException("Failed to close resource", e);
350+
}
345351
}
346352

347353
@Override

src/test/java/com/google/api/generator/gapic/composer/grpc/goldens/GrpcPublisherStub.golden

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -430,7 +430,13 @@ public class GrpcPublisherStub extends PublisherStub {
430430

431431
@Override
432432
public final void close() {
433-
shutdown();
433+
try {
434+
backgroundResources.close();
435+
} catch (RuntimeException e) {
436+
throw e;
437+
} catch (Exception e) {
438+
throw new IllegalStateException("Failed to close resource", e);
439+
}
434440
}
435441

436442
@Override

src/test/java/com/google/api/generator/gapic/composer/grpc/goldens/GrpcTestingStub.golden

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -382,7 +382,13 @@ public class GrpcTestingStub extends TestingStub {
382382

383383
@Override
384384
public final void close() {
385-
shutdown();
385+
try {
386+
backgroundResources.close();
387+
} catch (RuntimeException e) {
388+
throw e;
389+
} catch (Exception e) {
390+
throw new IllegalStateException("Failed to close resource", e);
391+
}
386392
}
387393

388394
@Override

src/test/java/com/google/api/generator/gapic/composer/rest/goldens/HttpJsonComplianceStub.golden

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -484,7 +484,13 @@ public class HttpJsonComplianceStub extends ComplianceStub {
484484

485485
@Override
486486
public final void close() {
487-
shutdown();
487+
try {
488+
backgroundResources.close();
489+
} catch (RuntimeException e) {
490+
throw e;
491+
} catch (Exception e) {
492+
throw new IllegalStateException("Failed to close resource", e);
493+
}
488494
}
489495

490496
@Override

test/integration/goldens/asset/com/google/cloud/asset/v1/stub/GrpcAssetServiceStub.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -598,7 +598,13 @@ public UnaryCallable<DeleteFeedRequest, Empty> deleteFeedCallable() {
598598

599599
@Override
600600
public final void close() {
601-
shutdown();
601+
try {
602+
backgroundResources.close();
603+
} catch (RuntimeException e) {
604+
throw e;
605+
} catch (Exception e) {
606+
throw new IllegalStateException("Failed to close resource", e);
607+
}
602608
}
603609

604610
@Override

test/integration/goldens/compute/com/google/cloud/compute/v1/stub/HttpJsonAddressesStub.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -403,7 +403,13 @@ public UnaryCallable<ListAddressesRequest, ListPagedResponse> listPagedCallable(
403403

404404
@Override
405405
public final void close() {
406-
shutdown();
406+
try {
407+
backgroundResources.close();
408+
} catch (RuntimeException e) {
409+
throw e;
410+
} catch (Exception e) {
411+
throw new IllegalStateException("Failed to close resource", e);
412+
}
407413
}
408414

409415
@Override

test/integration/goldens/compute/com/google/cloud/compute/v1/stub/HttpJsonRegionOperationsStub.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,13 @@ public UnaryCallable<GetRegionOperationRequest, Operation> getCallable() {
167167

168168
@Override
169169
public final void close() {
170-
shutdown();
170+
try {
171+
backgroundResources.close();
172+
} catch (RuntimeException e) {
173+
throw e;
174+
} catch (Exception e) {
175+
throw new IllegalStateException("Failed to close resource", e);
176+
}
171177
}
172178

173179
@Override

test/integration/goldens/credentials/com/google/cloud/iam/credentials/v1/stub/GrpcIamCredentialsStub.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,13 @@ public UnaryCallable<SignJwtRequest, SignJwtResponse> signJwtCallable() {
239239

240240
@Override
241241
public final void close() {
242-
shutdown();
242+
try {
243+
backgroundResources.close();
244+
} catch (RuntimeException e) {
245+
throw e;
246+
} catch (Exception e) {
247+
throw new IllegalStateException("Failed to close resource", e);
248+
}
243249
}
244250

245251
@Override

0 commit comments

Comments
 (0)