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

File tree

21 files changed

+197
-22
lines changed

21 files changed

+197
-22
lines changed

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

+57-2
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

+7-1
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

+7-1
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

+7-1
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

+7-1
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

+7-1
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

+7-1
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

+7-1
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

+7-1
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

+7-1
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

test/integration/goldens/iam/com/google/iam/v1/stub/GrpcIAMPolicyStub.java

+7-1
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,13 @@ public UnaryCallable<GetIamPolicyRequest, Policy> getIamPolicyCallable() {
197197

198198
@Override
199199
public final void close() {
200-
shutdown();
200+
try {
201+
backgroundResources.close();
202+
} catch (RuntimeException e) {
203+
throw e;
204+
} catch (Exception e) {
205+
throw new IllegalStateException("Failed to close resource", e);
206+
}
201207
}
202208

203209
@Override

test/integration/goldens/kms/com/google/cloud/kms/v1/stub/GrpcKeyManagementServiceStub.java

+7-1
Original file line numberDiff line numberDiff line change
@@ -1119,7 +1119,13 @@ public UnaryCallable<GetLocationRequest, Location> getLocationCallable() {
11191119

11201120
@Override
11211121
public final void close() {
1122-
shutdown();
1122+
try {
1123+
backgroundResources.close();
1124+
} catch (RuntimeException e) {
1125+
throw e;
1126+
} catch (Exception e) {
1127+
throw new IllegalStateException("Failed to close resource", e);
1128+
}
11231129
}
11241130

11251131
@Override

test/integration/goldens/library/com/google/cloud/example/library/v1/stub/GrpcLibraryServiceStub.java

+7-1
Original file line numberDiff line numberDiff line change
@@ -450,7 +450,13 @@ public UnaryCallable<MoveBookRequest, Book> moveBookCallable() {
450450

451451
@Override
452452
public final void close() {
453-
shutdown();
453+
try {
454+
backgroundResources.close();
455+
} catch (RuntimeException e) {
456+
throw e;
457+
} catch (Exception e) {
458+
throw new IllegalStateException("Failed to close resource", e);
459+
}
454460
}
455461

456462
@Override

test/integration/goldens/logging/com/google/cloud/logging/v2/stub/GrpcConfigServiceV2Stub.java

+7-1
Original file line numberDiff line numberDiff line change
@@ -889,7 +889,13 @@ public UnaryCallable<UpdateCmekSettingsRequest, CmekSettings> updateCmekSettings
889889

890890
@Override
891891
public final void close() {
892-
shutdown();
892+
try {
893+
backgroundResources.close();
894+
} catch (RuntimeException e) {
895+
throw e;
896+
} catch (Exception e) {
897+
throw new IllegalStateException("Failed to close resource", e);
898+
}
893899
}
894900

895901
@Override

test/integration/goldens/logging/com/google/cloud/logging/v2/stub/GrpcLoggingServiceV2Stub.java

+7-1
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,13 @@ public UnaryCallable<ListLogsRequest, ListLogsPagedResponse> listLogsPagedCallab
328328

329329
@Override
330330
public final void close() {
331-
shutdown();
331+
try {
332+
backgroundResources.close();
333+
} catch (RuntimeException e) {
334+
throw e;
335+
} catch (Exception e) {
336+
throw new IllegalStateException("Failed to close resource", e);
337+
}
332338
}
333339

334340
@Override

test/integration/goldens/logging/com/google/cloud/logging/v2/stub/GrpcMetricsServiceV2Stub.java

+7-1
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,13 @@ public UnaryCallable<DeleteLogMetricRequest, Empty> deleteLogMetricCallable() {
279279

280280
@Override
281281
public final void close() {
282-
shutdown();
282+
try {
283+
backgroundResources.close();
284+
} catch (RuntimeException e) {
285+
throw e;
286+
} catch (Exception e) {
287+
throw new IllegalStateException("Failed to close resource", e);
288+
}
283289
}
284290

285291
@Override

test/integration/goldens/pubsub/com/google/cloud/pubsub/v1/stub/GrpcPublisherStub.java

+7-1
Original file line numberDiff line numberDiff line change
@@ -549,7 +549,13 @@ public UnaryCallable<GetIamPolicyRequest, Policy> getIamPolicyCallable() {
549549

550550
@Override
551551
public final void close() {
552-
shutdown();
552+
try {
553+
backgroundResources.close();
554+
} catch (RuntimeException e) {
555+
throw e;
556+
} catch (Exception e) {
557+
throw new IllegalStateException("Failed to close resource", e);
558+
}
553559
}
554560

555561
@Override

test/integration/goldens/pubsub/com/google/cloud/pubsub/v1/stub/GrpcSchemaServiceStub.java

+7-1
Original file line numberDiff line numberDiff line change
@@ -412,7 +412,13 @@ public UnaryCallable<GetIamPolicyRequest, Policy> getIamPolicyCallable() {
412412

413413
@Override
414414
public final void close() {
415-
shutdown();
415+
try {
416+
backgroundResources.close();
417+
} catch (RuntimeException e) {
418+
throw e;
419+
} catch (Exception e) {
420+
throw new IllegalStateException("Failed to close resource", e);
421+
}
416422
}
417423

418424
@Override

test/integration/goldens/pubsub/com/google/cloud/pubsub/v1/stub/GrpcSubscriberStub.java

+7-1
Original file line numberDiff line numberDiff line change
@@ -762,7 +762,13 @@ public UnaryCallable<GetIamPolicyRequest, Policy> getIamPolicyCallable() {
762762

763763
@Override
764764
public final void close() {
765-
shutdown();
765+
try {
766+
backgroundResources.close();
767+
} catch (RuntimeException e) {
768+
throw e;
769+
} catch (Exception e) {
770+
throw new IllegalStateException("Failed to close resource", e);
771+
}
766772
}
767773

768774
@Override

test/integration/goldens/redis/com/google/cloud/redis/v1beta1/stub/GrpcCloudRedisStub.java

+7-1
Original file line numberDiff line numberDiff line change
@@ -505,7 +505,13 @@ public OperationCallable<DeleteInstanceRequest, Empty, Any> deleteInstanceOperat
505505

506506
@Override
507507
public final void close() {
508-
shutdown();
508+
try {
509+
backgroundResources.close();
510+
} catch (RuntimeException e) {
511+
throw e;
512+
} catch (Exception e) {
513+
throw new IllegalStateException("Failed to close resource", e);
514+
}
509515
}
510516

511517
@Override

test/integration/goldens/storage/com/google/storage/v2/stub/GrpcStorageStub.java

+7-1
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,13 @@ public ClientStreamingCallable<WriteObjectRequest, WriteObjectResponse> writeObj
201201

202202
@Override
203203
public final void close() {
204-
shutdown();
204+
try {
205+
backgroundResources.close();
206+
} catch (RuntimeException e) {
207+
throw e;
208+
} catch (Exception e) {
209+
throw new IllegalStateException("Failed to close resource", e);
210+
}
205211
}
206212

207213
@Override

0 commit comments

Comments
 (0)