Skip to content

Commit 5302cca

Browse files
committed
Fix bug causing process to hang upon quitting a REPL
1 parent aa1b398 commit 5302cca

File tree

1 file changed

+22
-16
lines changed
  • lib/node_modules/@stdlib/repl/presentation/bin

1 file changed

+22
-16
lines changed

lib/node_modules/@stdlib/repl/presentation/bin/cli

+22-16
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@
2424

2525
var resolve = require( 'path' ).resolve;
2626
var ReadStream = require( 'tty' ).ReadStream;
27-
var open = require( '@stdlib/fs/open' ); // eslint-disable-line stdlib/no-redeclare
27+
var openSync = require( '@stdlib/fs/open' ).sync;
28+
var closeSync = require( '@stdlib/fs/close' ).sync;
2829
var readFileSync = require( '@stdlib/fs/read-file' ).sync;
2930
var CLI = require( '@stdlib/tools/cli' );
3031
var stdin = require( '@stdlib/process/read-stdin' );
@@ -176,30 +177,35 @@ function main() {
176177
* @returns {void}
177178
*/
178179
function onRead( error, text ) {
180+
var repl;
181+
var pres;
182+
var fd;
179183
if ( error ) {
180184
return cli.error( error );
181185
}
182186
// Because we received input over `stdin`, we need to create a new TTY `stdin` read stream for receiving interactive user input...
183-
open( '/dev/tty', 'r', onOpen );
187+
fd = openSync( '/dev/tty', 'r' );
188+
if ( fd instanceof Error ) {
189+
return cli.error( error );
190+
}
191+
opts1.input = new ReadStream( fd ); // note: instance of net.Socket
192+
193+
// Create a new REPL instance:
194+
repl = new REPL( opts1 );
195+
repl.once( 'exit', onExit );
196+
197+
// Create a new REPL presentation:
198+
pres = new Presentation( text.toString(), repl, opts2 );
199+
pres.show();
184200

185201
/**
186-
* Callback invoked upon opening a file.
202+
* Callback invoked upon exiting a REPL.
187203
*
188204
* @private
189-
* @param {(Error|null)} error - error object
190-
* @param {integer} fd - file descriptor
191-
* @returns {void}
192205
*/
193-
function onOpen( error, fd ) {
194-
var repl;
195-
var pres;
196-
if ( error ) {
197-
return cli.error( error );
198-
}
199-
opts1.input = new ReadStream( fd );
200-
repl = new REPL( opts1 );
201-
pres = new Presentation( text.toString(), repl, opts2 );
202-
pres.show();
206+
function onExit() {
207+
opts1.input.end(); // close socket to prevent the process from hanging (see https://github.com/nodejs/node-v0.x-archive/issues/7101)
208+
closeSync( fd );
203209
}
204210
}
205211
}

0 commit comments

Comments
 (0)