Browse Source

fix(core): Increase resilience of app close when worker in main thread

When the worker is run on the main thread (as in populating test data or in @vendure/create), there have been intermittent issues with segfaults upon closing. This change attempts to solve this issue.
Michael Bromley 5 years ago
parent
commit
3ce74ff0e3
2 changed files with 5 additions and 6 deletions
  1. 1 2
      packages/core/src/bootstrap.ts
  2. 4 4
      packages/core/src/worker/worker.service.ts

+ 1 - 2
packages/core/src/bootstrap.ts

@@ -272,8 +272,7 @@ function closeWorkerOnAppClose(app: INestApplication, worker: INestMicroservice)
     const appPrototype = Object.getPrototypeOf(app);
     const appClose = appPrototype.close.bind(app);
     appPrototype.close = async () => {
-        await worker.close();
-        await appClose();
+        return Promise.all([appClose(), worker.close()]);
     };
 }
 

+ 4 - 4
packages/core/src/worker/worker.service.ts

@@ -28,9 +28,9 @@ export class WorkerService implements OnModuleDestroy {
         // The rather convoluted logic here is required in order to prevent more than
         // one connection being opened in the event that the `send` method is called multiple
         // times in the same event loop tick.
-        // On the first invokation, the first path is taken, which establishes the single
-        // connection (implicit in the first call to ClientProxt.send()). All subsequent
-        // invokations take the second code path.
+        // On the first invocation, the first path is taken, which establishes the single
+        // connection (implicit in the first call to ClientProxy.send()). All subsequent
+        // invocations take the second code path.
         if (!this.pendingConnection && this.initialConnection.value === false) {
             this.pendingConnection = true;
             return this.client
@@ -43,7 +43,7 @@ export class WorkerService implements OnModuleDestroy {
                 );
         } else {
             return this.initialConnection.pipe(
-                filter(val => val),
+                filter((val) => val),
                 take(1),
                 mergeMap(() => {
                     return this.client.send<R, T>(