diff --git a/packages/@jsii/runtime/lib/sync-stdio.ts b/packages/@jsii/runtime/lib/sync-stdio.ts index 9e7f5166a8..70753d3d02 100644 --- a/packages/@jsii/runtime/lib/sync-stdio.ts +++ b/packages/@jsii/runtime/lib/sync-stdio.ts @@ -18,45 +18,39 @@ export class SyncStdio { } public readLine(): string | undefined { - if (this.inputQueue.length > 0) { - return this.inputQueue.shift(); - } - - try { - const buff = Buffer.alloc(INPUT_BUFFER_SIZE); - const read = fs.readSync(STDIN_FD, buff, 0, buff.length, null); + const buff = Buffer.alloc(INPUT_BUFFER_SIZE); + while (this.inputQueue.length === 0) { + try { + const read = fs.readSync(STDIN_FD, buff, 0, buff.length, null); - if (read === 0) { - return undefined; - } + if (read === 0) { + return undefined; + } - const str = buff.slice(0, read).toString(); + const str = buff.slice(0, read).toString(); - for (const ch of str) { - if (ch === '\n') { - this.inputQueue.push(this.currentLine); - this.currentLine = ''; - } else { - this.currentLine += ch; + for (const ch of str) { + if (ch === '\n') { + this.inputQueue.push(this.currentLine); + this.currentLine = ''; + } else { + this.currentLine += ch; + } + } + } catch (e) { + // HACK: node opens STDIN with O_NONBLOCK, meaning attempts to synchrounously read from it may + // result in EAGAIN. In such cases, the call should be retried until it succeeds. This kind of + // polling may be terribly inefficient, and the "correct" way to address this is to stop + // relying on synchronous reads from STDIN. This is however a non-trivial endeavor, and the + // current state of things is very much broken in node >= 13.2, as can be see in + // https://github.com/aws/aws-cdk/issues/5187 + if (e.code !== 'EAGAIN') { + throw e; } - } - } catch (e) { - // HACK: node opens STDIN with O_NONBLOCK, meaning attempts to synchrounously read from it may - // result in EAGAIN. In such cases, the call should be retried until it succeeds. This kind of - // polling may be terribly inefficient, and the "correct" way to address this is to stop - // relying on synchronous reads from STDIN. This is however a non-trivial endeavor, and the - // current state of things is very much broken in node >= 13.2, as can be see in - // https://github.com/aws/aws-cdk/issues/5187 - if (e.code !== 'EAGAIN') { - throw e; } } const next = this.inputQueue.shift(); - if (next == null) { - return this.readLine(); - } - return next; }