GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — repl: fix freeze if util.inspect throws & clean up error handling a bit by monsanto · Pull Request #1968 · nodejs/node

Conversation

@monsanto

@monsanto monsanto commented Jun 13, 2015

Copy link
Copy Markdown
Contributor

If an error is thrown during the readline keypress event, the state of the streaming escape sequence decoder (added in #1601) will become corrupted. This will render the REPL (and possibly your whole terminal) completely unusable. This can be triggered by an object with a custom inspect method that throws.

Originally I modified the escape sequence decoder to avoid corruption but it proved difficult to test because the synchronous throw in keypress would end up corrupting the state of the mock streams I was using to test (although for whatever reason it would work with the real TTY streams). Felt too fragile to commit.

Instead, I decided that none of the eval machinery should ever throw. This PR adds an extra try/catch around self.writer (which is util.inspect by default) and makes some minor cleanups to make error handling more consistent.

Note: our test suite currently forbids running any of the REPL machinery in a different tick, which would sidestep most, if not all, of these problems.

cc @rlidwka

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
monsanto added 2 commits June 13, 2015 00:05
@monsanto
repl: ensure eval serialization does not throw
2380c93
If an error is thrown during the readline `keypress` event, the state of
the streaming escape sequence decoder (added in
#1601) will become corrupted. This
will render the REPL (and possibly your whole terminal) completely
unusable. This can be triggered by an object with a custom `inspect`
method that throws.

This commit adds an extra try/catch around `self.writer` (which is
`util.inspect` by default).
@monsanto
repl: cleanup error handling
03aa4a0
monsanto
monsanto reviewed Jun 13, 2015
View reviewed changes
Comment thread lib/repl.js Outdated

@monsanto monsanto Jun 13, 2015

Copy link
Copy Markdown
Contributor Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This logic is moved to finish so all of the error handling can be in one place. It doesn't seem safe to do this here anyway since the evaluation function can be overriden. The explicit process.domain.exit is not carried over as 1) I don't understand what use it serves & I don't like to blindly c/p, 2) the other existing domain emit didn't come with an exit and 3) the repl+domain test passes without it. @isaacs you added this 2 years ago, do you remember?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@monsanto monsanto added the repl Issues and PRs related to the REPL subsystem. label Jun 13, 2015
@rlidwka

rlidwka commented Jun 13, 2015

Copy link
Copy Markdown
Contributor

keypress event in readline is considered a public api. If readline is not usable after that throws, it's a very bad thing. Suggested fix is something like this (I could do a separate pull request):

diff --git a/lib/readline.js b/lib/readline.js
index 4107c99..b8f52ee 100644
--- a/lib/readline.js
+++ b/lib/readline.js
@@ -910,7 +910,15 @@ function emitKeypressEvents(stream) {
       var r = stream[KEYPRESS_DECODER].write(b);
       if (r) {
         for (var i = 0; i < r.length; i++) {
-          stream[ESCAPE_DECODER].next(r[i]);
+          try {
+            stream[ESCAPE_DECODER].next(r[i]);
+          } catch (err) {
+            // if the generator throws (it could happen in the `keypress`
+            // event), we need to restart it.
+            stream[ESCAPE_DECODER] = emitKeys(stream);
+            stream[ESCAPE_DECODER].next();
+            throw err;
+          }
         }
       }
     } else {

Which is still bad because throw will break for loop, and the rest of the chunk will get eaten, but at least nothing will freeze permanently.

I have no opinion about other changes. Maybe a guarantee that REPL never throws is a good thing.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@Fishrock123

Fishrock123 commented Jun 13, 2015

Copy link
Copy Markdown
Contributor

I think I'd prefer @rlidwka's fix, i.e. to fix it in readline.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@rvagg
rvagg force-pushed the master branch from 9d21135 to 628a3ab Compare June 25, 2015 09:18
@rlidwka rlidwka mentioned this pull request Jul 5, 2015
readline: fix freeze if keypress event throws #2107
Closed
@indutny
indutny force-pushed the master branch from bffb204 to eb35968 Compare July 22, 2015 21:21
@rvagg
rvagg force-pushed the master branch from 11c25c2 to ba02bd0 Compare September 6, 2015 11:55
@jasnell

jasnell commented Nov 16, 2015

Copy link
Copy Markdown
Member

Alternative solution was landed in #2107

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@jasnell jasnell closed this Nov 16, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

No reviews

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

repl Issues and PRs related to the REPL subsystem.

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

4 participants

@monsanto @rlidwka @Fishrock123 @jasnell
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.