From 2cc25e22d6dadabee329a3aa8df483aa9845db11 Mon Sep 17 00:00:00 2001 From: Chris Rybicki Date: Thu, 7 Sep 2023 23:31:01 +0300 Subject: [PATCH] fix(sdk): cloud.Service hangs on long-running inflight functions (#4089) Fixes #4086 ## Checklist - [x] Title matches [Winglang's style guide](https://www.winglang.io/contributing/start-here/pull_requests#how-are-pull-request-titles-formatted) - [x] Description explains motivation and solution - [x] Tests added (always) - [ ] Docs updated (only required for features) - [ ] Added `pr/e2e-full` label if this feature requires end-to-end testing *By submitting this pull request, I confirm that my contribution is made under the terms of the [Wing Cloud Contribution License](https://github.com/winglang/wing/blob/main/CONTRIBUTION_LICENSE.md)*. --- .../src/target-sim/service.inflight.ts | 3 +- libs/wingsdk/test/target-sim/service.test.ts | 31 +++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/libs/wingsdk/src/target-sim/service.inflight.ts b/libs/wingsdk/src/target-sim/service.inflight.ts index 92f2c4893f3..23eff460ad4 100644 --- a/libs/wingsdk/src/target-sim/service.inflight.ts +++ b/libs/wingsdk/src/target-sim/service.inflight.ts @@ -55,7 +55,8 @@ export class Service implements IServiceClient, ISimulatorResourceInstance { sourceType: SERVICE_TYPE, timestamp: new Date().toISOString(), }); - await fnClient.invoke(""); + // eslint-disable-next-line @typescript-eslint/no-floating-promises + fnClient.invoke(""); this.running = true; } diff --git a/libs/wingsdk/test/target-sim/service.test.ts b/libs/wingsdk/test/target-sim/service.test.ts index 395b4d3eebe..4749916b06a 100644 --- a/libs/wingsdk/test/target-sim/service.test.ts +++ b/libs/wingsdk/test/target-sim/service.test.ts @@ -9,6 +9,14 @@ async handle(message) { console.log("Service Started"); }`; +const INFLIGHT_ON_START_WITH_LOOP = ` +async handle(message) { + console.log("Service Started"); + while (true) { + await new Promise((resolve) => setTimeout(resolve, 1000)); + } +}`; + const INFLIGHT_ON_STOP = ` async handle(message) { console.log("Service Stopped"); @@ -42,6 +50,29 @@ test("create a service with on start method", async () => { expect(app.snapshot()).toMatchSnapshot(); }); +test( + "on start method does not block other resources from deploying", + async () => { + // GIVEN + const app = new SimApp(); + const handler = Testing.makeHandler( + app, + "OnStartHandler", + INFLIGHT_ON_START_WITH_LOOP + ); + cloud.Service._newService(app, "my_service", { + onStart: handler, + }); + + // WHEN + const s = await app.startSimulator(); + + // THEN + await s.stop(); + }, + { timeout: 10000 } +); + test("create a service with a on stop method", async () => { // Given const app = new SimApp();