cmd, console, internal: support interrupting the js console #23387 (#1533)

This commit is contained in:
Daniel Liu 2025-09-21 19:23:41 +08:00 committed by GitHub
parent 607fa3cc90
commit 8615067df1
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 195 additions and 70 deletions

View file

@ -18,12 +18,9 @@ package main
import ( import (
"fmt" "fmt"
"os"
"os/signal"
"path/filepath" "path/filepath"
"slices" "slices"
"strings" "strings"
"syscall"
"github.com/XinFinOrg/XDPoSChain/cmd/utils" "github.com/XinFinOrg/XDPoSChain/cmd/utils"
"github.com/XinFinOrg/XDPoSChain/console" "github.com/XinFinOrg/XDPoSChain/console"
@ -76,10 +73,10 @@ JavaScript API. See https://github.com/XinFinOrg/XDPoSChain/wiki/JavaScript-Cons
func localConsole(ctx *cli.Context) error { func localConsole(ctx *cli.Context) error {
// Create and start the node based on the CLI flags // Create and start the node based on the CLI flags
stack, backend, cfg := makeFullNode(ctx) stack, backend, cfg := makeFullNode(ctx)
startNode(ctx, stack, backend, cfg) startNode(ctx, stack, backend, cfg, true)
defer stack.Close() defer stack.Close()
// Attach to the newly started node and start the JavaScript console // Attach to the newly started node and create the JavaScript console.
client := stack.Attach() client := stack.Attach()
config := console.Config{ config := console.Config{
DataDir: utils.MakeDataDir(ctx), DataDir: utils.MakeDataDir(ctx),
@ -87,22 +84,28 @@ func localConsole(ctx *cli.Context) error {
Client: client, Client: client,
Preload: utils.MakeConsolePreloads(ctx), Preload: utils.MakeConsolePreloads(ctx),
} }
console, err := console.New(config) console, err := console.New(config)
if err != nil { if err != nil {
utils.Fatalf("failed to start the JavaScript console: %v", err) return fmt.Errorf("failed to start the JavaScript console: %v", err)
} }
defer console.Stop(false) defer console.Stop(false)
// If only a short execution was requested, evaluate and return // If only a short execution was requested, evaluate and return.
if script := ctx.String(utils.ExecFlag.Name); script != "" { if script := ctx.String(utils.ExecFlag.Name); script != "" {
console.Evaluate(script) console.Evaluate(script)
return nil return nil
} }
// Otherwise print the welcome screen and enter interactive mode
// Track node shutdown and stop the console when it goes down.
// This happens when SIGTERM is sent to the process.
go func() {
stack.Wait()
console.StopInteractive()
}()
// Print the welcome screen and enter interactive mode.
console.Welcome() console.Welcome()
console.Interactive() console.Interactive()
return nil return nil
} }
@ -139,7 +142,6 @@ func remoteConsole(ctx *cli.Context) error {
Client: client, Client: client,
Preload: utils.MakeConsolePreloads(ctx), Preload: utils.MakeConsolePreloads(ctx),
} }
console, err := console.New(config) console, err := console.New(config)
if err != nil { if err != nil {
utils.Fatalf("Failed to start the JavaScript console: %v", err) utils.Fatalf("Failed to start the JavaScript console: %v", err)
@ -154,7 +156,6 @@ func remoteConsole(ctx *cli.Context) error {
// Otherwise print the welcome screen and enter interactive mode // Otherwise print the welcome screen and enter interactive mode
console.Welcome() console.Welcome()
console.Interactive() console.Interactive()
return nil return nil
} }
@ -178,7 +179,7 @@ func dialRPC(endpoint string) (*rpc.Client, error) {
func ephemeralConsole(ctx *cli.Context) error { func ephemeralConsole(ctx *cli.Context) error {
// Create and start the node based on the CLI flags // Create and start the node based on the CLI flags
stack, backend, cfg := makeFullNode(ctx) stack, backend, cfg := makeFullNode(ctx)
startNode(ctx, stack, backend, cfg) startNode(ctx, stack, backend, cfg, false)
defer stack.Close() defer stack.Close()
// Attach to the newly started node and start the JavaScript console // Attach to the newly started node and start the JavaScript console
@ -192,25 +193,24 @@ func ephemeralConsole(ctx *cli.Context) error {
console, err := console.New(config) console, err := console.New(config)
if err != nil { if err != nil {
utils.Fatalf("Failed to start the JavaScript console: %v", err) return fmt.Errorf("failed to start the JavaScript console: %v", err)
} }
defer console.Stop(false) defer console.Stop(false)
// Evaluate each of the specified JavaScript files // Interrupt the JS interpreter when node is stopped.
go func() {
stack.Wait()
console.Stop(false)
}()
// Evaluate each of the specified JavaScript files.
for _, file := range ctx.Args().Slice() { for _, file := range ctx.Args().Slice() {
if err = console.Execute(file); err != nil { if err = console.Execute(file); err != nil {
utils.Fatalf("Failed to execute %s: %v", file, err) return fmt.Errorf("failed to execute %s: %v", file, err)
} }
} }
// Wait for pending callbacks, but stop for Ctrl-C.
abort := make(chan os.Signal, 1)
signal.Notify(abort, syscall.SIGINT, syscall.SIGTERM)
go func() { // The main script is now done, but keep running timers/callbacks.
<-abort
os.Exit(0)
}()
console.Stop(true) console.Stop(true)
return nil return nil
} }

View file

@ -264,7 +264,7 @@ func main() {
func XDC(ctx *cli.Context) error { func XDC(ctx *cli.Context) error {
stack, backend, cfg := makeFullNode(ctx) stack, backend, cfg := makeFullNode(ctx)
defer stack.Close() defer stack.Close()
startNode(ctx, stack, backend, cfg) startNode(ctx, stack, backend, cfg, false)
stack.Wait() stack.Wait()
return nil return nil
} }
@ -272,9 +272,9 @@ func XDC(ctx *cli.Context) error {
// startNode boots up the system node and all registered protocols, after which // startNode boots up the system node and all registered protocols, after which
// it unlocks any requested accounts, and starts the RPC/IPC interfaces and the // it unlocks any requested accounts, and starts the RPC/IPC interfaces and the
// miner. // miner.
func startNode(ctx *cli.Context, stack *node.Node, backend ethapi.Backend, cfg XDCConfig) { func startNode(ctx *cli.Context, stack *node.Node, backend ethapi.Backend, cfg XDCConfig, isConsole bool) {
// Start up the node itself // Start up the node itself
utils.StartNode(stack) utils.StartNode(stack, isConsole)
// Unlock any account specifically requested // Unlock any account specifically requested
backends := stack.AccountManager().Backends(keystore.KeyStoreType) backends := stack.AccountManager().Backends(keystore.KeyStoreType)

View file

@ -64,7 +64,7 @@ func Fatalf(format string, args ...interface{}) {
os.Exit(1) os.Exit(1)
} }
func StartNode(stack *node.Node) { func StartNode(stack *node.Node, isConsole bool) {
if err := stack.Start(); err != nil { if err := stack.Start(); err != nil {
Fatalf("Error starting protocol stack: %v", err) Fatalf("Error starting protocol stack: %v", err)
} }
@ -72,17 +72,34 @@ func StartNode(stack *node.Node) {
sigc := make(chan os.Signal, 1) sigc := make(chan os.Signal, 1)
signal.Notify(sigc, syscall.SIGINT, syscall.SIGTERM) signal.Notify(sigc, syscall.SIGINT, syscall.SIGTERM)
defer signal.Stop(sigc) defer signal.Stop(sigc)
<-sigc
log.Info("Got interrupt, shutting down...") shutdown := func() {
go stack.Close() log.Info("Got interrupt, shutting down...")
for i := 10; i > 0; i-- { go stack.Close()
<-sigc for i := 10; i > 0; i-- {
if i > 1 { <-sigc
log.Warn("Already shutting down, interrupt more to panic.", "times", i-1) if i > 1 {
log.Warn("Already shutting down, interrupt more to panic.", "times", i-1)
}
} }
debug.Exit() // ensure trace and CPU profile data is flushed.
debug.LoudPanic("boom")
}
if isConsole {
// In JS console mode, SIGINT is ignored because it's handled by the console.
// However, SIGTERM still shuts down the node.
for {
sig := <-sigc
if sig == syscall.SIGTERM {
shutdown()
return
}
}
} else {
<-sigc
shutdown()
} }
debug.Exit() // ensure trace and CPU profile data is flushed.
debug.LoudPanic("boom")
}() }()
} }

View file

@ -17,6 +17,7 @@
package console package console
import ( import (
"errors"
"fmt" "fmt"
"io" "io"
"os" "os"
@ -25,6 +26,7 @@ import (
"regexp" "regexp"
"sort" "sort"
"strings" "strings"
"sync"
"syscall" "syscall"
"github.com/XinFinOrg/XDPoSChain/console/prompt" "github.com/XinFinOrg/XDPoSChain/console/prompt"
@ -73,6 +75,13 @@ type Console struct {
histPath string // Absolute path to the console scrollback history histPath string // Absolute path to the console scrollback history
history []string // Scroll history maintained by the console history []string // Scroll history maintained by the console
printer io.Writer // Output writer to serialize any display strings to printer io.Writer // Output writer to serialize any display strings to
interactiveStopped chan struct{}
stopInteractiveCh chan struct{}
signalReceived chan struct{}
stopped chan struct{}
wg sync.WaitGroup
stopOnce sync.Once
} }
// New initializes a JavaScript interpreted runtime environment and sets defaults // New initializes a JavaScript interpreted runtime environment and sets defaults
@ -91,12 +100,16 @@ func New(config Config) (*Console, error) {
// Initialize the console and return // Initialize the console and return
console := &Console{ console := &Console{
client: config.Client, client: config.Client,
jsre: jsre.New(config.DocRoot, config.Printer), jsre: jsre.New(config.DocRoot, config.Printer),
prompt: config.Prompt, prompt: config.Prompt,
prompter: config.Prompter, prompter: config.Prompter,
printer: config.Printer, printer: config.Printer,
histPath: filepath.Join(config.DataDir, HistoryFile), histPath: filepath.Join(config.DataDir, HistoryFile),
interactiveStopped: make(chan struct{}),
stopInteractiveCh: make(chan struct{}),
signalReceived: make(chan struct{}, 1),
stopped: make(chan struct{}),
} }
if err := os.MkdirAll(config.DataDir, 0700); err != nil { if err := os.MkdirAll(config.DataDir, 0700); err != nil {
return nil, err return nil, err
@ -104,6 +117,10 @@ func New(config Config) (*Console, error) {
if err := console.init(config.Preload); err != nil { if err := console.init(config.Preload); err != nil {
return nil, err return nil, err
} }
console.wg.Add(1)
go console.interruptHandler()
return console, nil return console, nil
} }
@ -305,44 +322,100 @@ func (c *Console) Evaluate(statement string) {
} }
}() }()
c.jsre.Evaluate(statement, c.printer) c.jsre.Evaluate(statement, c.printer)
// Avoid exiting Interactive when jsre was interrupted by SIGINT.
c.clearSignalReceived()
} }
// Interactive starts an interactive user session, where input is propted from // interruptHandler runs in its own goroutine and waits for signals.
// When a signal is received, it interrupts the JS interpreter.
func (c *Console) interruptHandler() {
defer c.wg.Done()
// During Interactive, liner inhibits the signal while it is prompting for
// input. However, the signal will be received while evaluating JS.
//
// On unsupported terminals, SIGINT can also happen while prompting.
// Unfortunately, it is not possible to abort the prompt in this case and
// the c.readLines goroutine leaks.
sig := make(chan os.Signal, 1)
signal.Notify(sig, syscall.SIGINT)
defer signal.Stop(sig)
for {
select {
case <-sig:
c.setSignalReceived()
c.jsre.Interrupt(errors.New("interrupted"))
case <-c.stopInteractiveCh:
close(c.interactiveStopped)
c.jsre.Interrupt(errors.New("interrupted"))
case <-c.stopped:
return
}
}
}
func (c *Console) setSignalReceived() {
select {
case c.signalReceived <- struct{}{}:
default:
}
}
func (c *Console) clearSignalReceived() {
select {
case <-c.signalReceived:
default:
}
}
// StopInteractive causes Interactive to return as soon as possible.
func (c *Console) StopInteractive() {
select {
case c.stopInteractiveCh <- struct{}{}:
case <-c.stopped:
}
}
// Interactive starts an interactive user session, where in.put is propted from
// the configured user prompter. // the configured user prompter.
func (c *Console) Interactive() { func (c *Console) Interactive() {
var ( var (
prompt = c.prompt // Current prompt line (used for multi-line inputs) prompt = c.prompt // the current prompt line (used for multi-line inputs)
indents = 0 // Current number of input indents (used for multi-line inputs) indents = 0 // the current number of input indents (used for multi-line inputs)
input = "" // Current user input input = "" // the current user input
inputLine = make(chan string, 1) // receives user input inputLine = make(chan string, 1) // receives user input
inputErr = make(chan error, 1) // receives liner errors inputErr = make(chan error, 1) // receives liner errors
requestLine = make(chan string) // requests a line of input requestLine = make(chan string) // requests a line of input
interrupt = make(chan os.Signal, 1)
) )
// Monitor Ctrl-C. While liner does turn on the relevant terminal mode bits to avoid defer func() {
// the signal, a signal can still be received for unsupported terminals. Unfortunately c.writeHistory()
// there is no way to cancel the line reader when this happens. The readLines }()
// goroutine will be leaked in this case.
signal.Notify(interrupt, syscall.SIGINT, syscall.SIGTERM)
defer signal.Stop(interrupt)
// The line reader runs in a separate goroutine. // The line reader runs in a separate goroutine.
go c.readLines(inputLine, inputErr, requestLine) go c.readLines(inputLine, inputErr, requestLine)
defer close(requestLine) defer close(requestLine)
// Start sending prompts to the user and reading back inputs
for { for {
// Send the next prompt, triggering an input read. // Send the next prompt, triggering an input read.
requestLine <- prompt requestLine <- prompt
select { select {
case <-interrupt: case <-c.interactiveStopped:
fmt.Fprintln(c.printer, "node is down, exiting console")
return
case <-c.signalReceived:
// SIGINT received while prompting for input -> unsupported terminal.
// I'm not sure if the best choice would be to leave the console running here.
// Bash keeps running in this case. node.js does not.
fmt.Fprintln(c.printer, "caught interrupt, exiting") fmt.Fprintln(c.printer, "caught interrupt, exiting")
return return
case err := <-inputErr: case err := <-inputErr:
if err == liner.ErrPromptAborted && indents > 0 { if err == liner.ErrPromptAborted {
// When prompting for multi-line input, the first Ctrl-C resets // When prompting for multi-line input, the first Ctrl-C resets
// the multi-line state. // the multi-line state.
prompt, indents, input = c.prompt, 0, "" prompt, indents, input = c.prompt, 0, ""
@ -445,12 +518,19 @@ func (c *Console) Execute(path string) error {
// Stop cleans up the console and terminates the runtime environment. // Stop cleans up the console and terminates the runtime environment.
func (c *Console) Stop(graceful bool) error { func (c *Console) Stop(graceful bool) error {
if err := os.WriteFile(c.histPath, []byte(strings.Join(c.history, "\n")), 0600); err != nil { c.stopOnce.Do(func() {
return err // Stop the interrupt handler.
} close(c.stopped)
if err := os.Chmod(c.histPath, 0600); err != nil { // Force 0600, even if it was different previously c.wg.Wait()
return err })
}
c.jsre.Stop(graceful) c.jsre.Stop(graceful)
return nil return nil
} }
func (c *Console) writeHistory() error {
if err := os.WriteFile(c.histPath, []byte(strings.Join(c.history, "\n")), 0600); err != nil {
return err
}
return os.Chmod(c.histPath, 0600) // Force 0600, even if it was different previously
}

View file

@ -20,6 +20,7 @@ package jsre
import ( import (
crand "crypto/rand" crand "crypto/rand"
"encoding/binary" "encoding/binary"
"errors"
"fmt" "fmt"
"io" "io"
"math/rand" "math/rand"
@ -220,19 +221,33 @@ loop:
} }
// Do executes the given function on the JS event loop. // Do executes the given function on the JS event loop.
// When the runtime is stopped, fn will not execute.
func (re *JSRE) Do(fn func(*goja.Runtime)) { func (re *JSRE) Do(fn func(*goja.Runtime)) {
done := make(chan bool) done := make(chan bool)
req := &evalReq{fn, done} req := &evalReq{fn, done}
re.evalQueue <- req select {
<-done case re.evalQueue <- req:
<-done
case <-re.closed:
}
} }
// stops the event loop before exit, optionally waits for all timers to expire // Stop terminates the event loop, optionally waiting for all timers to expire.
func (re *JSRE) Stop(waitForCallbacks bool) { func (re *JSRE) Stop(waitForCallbacks bool) {
select { timeout := time.NewTimer(10 * time.Millisecond)
case <-re.closed: defer timeout.Stop()
case re.stopEventLoop <- waitForCallbacks:
<-re.closed for {
select {
case <-re.closed:
return
case re.stopEventLoop <- waitForCallbacks:
<-re.closed
return
case <-timeout.C:
// JS is blocked, interrupt and try again.
re.vm.Interrupt(errors.New("JS runtime stopped"))
}
} }
} }
@ -282,6 +297,19 @@ func (re *JSRE) Evaluate(code string, w io.Writer) {
}) })
} }
// Interrupt stops the current JS evaluation.
func (re *JSRE) Interrupt(v interface{}) {
done := make(chan bool)
noop := func(*goja.Runtime) {}
select {
case re.evalQueue <- &evalReq{noop, done}:
// event loop is not blocked.
default:
re.vm.Interrupt(v)
}
}
// Compile compiles and then runs a piece of JS code. // Compile compiles and then runs a piece of JS code.
func (re *JSRE) Compile(filename string, src string) (err error) { func (re *JSRE) Compile(filename string, src string) (err error) {
re.Do(func(vm *goja.Runtime) { _, err = compileAndRun(vm, filename, src) }) re.Do(func(vm *goja.Runtime) { _, err = compileAndRun(vm, filename, src) })