Skip to content

Commit

Permalink
cmd/geth, console: better handle local console interrupt
Browse files Browse the repository at this point in the history
  • Loading branch information
holiman committed Aug 27, 2021
1 parent 148d25b commit 90f5546
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 6 deletions.
11 changes: 9 additions & 2 deletions cmd/geth/consolecmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,13 @@ func localConsole(ctx *cli.Context) error {
if err != nil {
utils.Fatalf("Failed to start the JavaScript console: %v", err)
}
exitCh := make(chan struct{})
go func() {
// In case the blockchain is stopped, e.g. via ctrl-c, we should
// also close down the console.
stack.Wait()
close(exitCh)
}()
defer console.Stop(false)

// If only a short execution was requested, evaluate and return
Expand All @@ -105,7 +112,7 @@ func localConsole(ctx *cli.Context) error {
}
// Otherwise print the welcome screen and enter interactive mode
console.Welcome()
console.Interactive()
console.Interactive(exitCh)

return nil
}
Expand Down Expand Up @@ -164,7 +171,7 @@ func remoteConsole(ctx *cli.Context) error {

// Otherwise print the welcome screen and enter interactive mode
console.Welcome()
console.Interactive()
console.Interactive(nil)

return nil
}
Expand Down
7 changes: 5 additions & 2 deletions console/console.go
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ func (c *Console) Evaluate(statement string, abortCh chan os.Signal) {

// Interactive starts an interactive user session, where input is propted from
// the configured user prompter.
func (c *Console) Interactive() {
func (c *Console) Interactive(exitCh chan struct{}) {
var (
prompt = c.prompt // the current prompt line (used for multi-line inputs)
indents = 0 // the current number of input indents (used for multi-line inputs)
Expand All @@ -380,7 +380,10 @@ func (c *Console) Interactive() {
requestLine <- prompt

select {
case <-interrupt:
case <-exitCh: // Exit via external notification (backend closed)
fmt.Fprintln(c.printer, "exiting console")
return
case <-interrupt: // Exit via our own os.Signal interrupt
fmt.Fprintln(c.printer, "caught interrupt, exiting")
return

Expand Down
2 changes: 1 addition & 1 deletion console/console_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ func TestInteractive(t *testing.T) {
tester := newTester(t, nil)
defer tester.Close(t)

go tester.console.Interactive()
go tester.console.Interactive(nil)

// Wait for a prompt and send a statement back
select {
Expand Down
2 changes: 1 addition & 1 deletion internal/jsre/jsre.go
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ func (re *JSRE) Evaluate(code string, w io.Writer) {
})
}

func (re *JSRE) Interrupt(v interface{}){
func (re *JSRE) Interrupt(v interface{}) {
re.vm.Interrupt(v)
}

Expand Down

0 comments on commit 90f5546

Please sign in to comment.