From b4fe423690a23441673a103514a58a71b1f22d0d Mon Sep 17 00:00:00 2001 From: Daniel Dinu Date: Thu, 12 Sep 2019 22:48:00 -0700 Subject: [PATCH] Fix stack overflow in KernelHost.run() This commit fixes issue #778. --- packages/jsii-runtime/lib/host.ts | 2 +- packages/jsii-runtime/package.json | 2 +- packages/jsii-runtime/test/stress-test.ts | 26 +++++++++++++++++++++++ 3 files changed, 28 insertions(+), 2 deletions(-) create mode 100755 packages/jsii-runtime/test/stress-test.ts diff --git a/packages/jsii-runtime/lib/host.ts b/packages/jsii-runtime/lib/host.ts index 0f64ec06ae..6c57d80071 100644 --- a/packages/jsii-runtime/lib/host.ts +++ b/packages/jsii-runtime/lib/host.ts @@ -14,7 +14,7 @@ export class KernelHost { return; // done } - this.processRequest(req, () => this.run()); + this.processRequest(req, () => setImmediate(() => this.run())); } private callbackHandler(callback: api.Callback) { diff --git a/packages/jsii-runtime/package.json b/packages/jsii-runtime/package.json index 0fcfb4df55..69d37d6740 100644 --- a/packages/jsii-runtime/package.json +++ b/packages/jsii-runtime/package.json @@ -10,7 +10,7 @@ "scripts": { "build": "tsc --build && chmod +x bin/jsii-runtime && /bin/bash ./bundle.sh", "watch": "tsc --build -w", - "test": "/bin/bash test/playback-test.sh", + "test": "/bin/bash test/playback-test.sh && node test/stress-test.js", "package": "package-js" }, "devDependencies": { diff --git a/packages/jsii-runtime/test/stress-test.ts b/packages/jsii-runtime/test/stress-test.ts new file mode 100755 index 0000000000..3af65107ec --- /dev/null +++ b/packages/jsii-runtime/test/stress-test.ts @@ -0,0 +1,26 @@ +import { InputOutput, Input, Output } from "../lib/in-out"; +import { KernelHost } from "../lib/host"; + +const requestCount = 250000; + +class FakeInputOutput extends InputOutput { + debug = false; + private count: number = 0; + + write(_: Output) { } + + read(): Input | undefined { + if(this.count == requestCount) { + return undefined; + } + + ++this.count; + return { api: 'stats' }; + } +} + +const inout = new FakeInputOutput(); +const host = new KernelHost(inout, { debug: false, noStack: false }); +host.run(); + +console.info("jsii-runtime stress test succeeded");