Skip to content
This repository has been archived by the owner on Feb 17, 2024. It is now read-only.

Commit

Permalink
Slightly better handling of SIGINT
Browse files Browse the repository at this point in the history
  • Loading branch information
chriso committed Jun 1, 2023
1 parent e5e906d commit 8653751
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 12 deletions.
12 changes: 12 additions & 0 deletions internal/debug/repl.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,17 @@ read_input:

switch command {
case "s", "step":
if ctx.Err() != nil {
r.println(`error: the module has exited. Try "restart", "quit" or "help"`)
goto read_input
}
r.stepping = true

case "c", "continue":
if ctx.Err() != nil {
r.println(`error: the module has exited. Try "restart", "quit" or "help"`)
goto read_input
}
r.stepping = false

case "r", "restart":
Expand Down Expand Up @@ -107,3 +115,7 @@ func (r *REPL) printf(s string, args ...any) (int, error) {
func (r *REPL) print(args ...any) (int, error) {
return fmt.Fprint(r.writer, args...)
}

func (r *REPL) println(args ...any) (int, error) {
return fmt.Fprintln(r.writer, args...)
}
1 change: 1 addition & 0 deletions internal/debug/tracer.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ func (t *Tracer) ModuleBefore(ctx context.Context, mod wazero.CompiledModule) {
}

func (t *Tracer) ModuleAfter(ctx context.Context, err any) {
t.print("\n")
switch e := err.(type) {
case nil:
t.print("The module exited normally\n")
Expand Down
3 changes: 1 addition & 2 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package main

import (
"context"
"io"
"log"
"os"
Expand All @@ -13,5 +12,5 @@ func init() {
}

func main() {
os.Exit(root(context.Background(), os.Args[1:]...))
os.Exit(root(os.Args[1:]...))
}
2 changes: 1 addition & 1 deletion replay.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func replay(ctx context.Context, args []string) error {

var debugREPL *debug.REPL
if debugger {
debugREPL = debug.NewREPL(os.Stdin, os.Stdout)
debugREPL = debug.NewREPL(os.Stdin, os.Stderr)
ctx = debug.RegisterFunctionListener(ctx, debugREPL)
}

Expand Down
4 changes: 3 additions & 1 deletion root.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ Example:
For a list of commands available, run 'timecraft help'.`

// root is the timecraft entrypoint.
func root(ctx context.Context, args ...string) int {
func root(args ...string) int {
var (
// Secret options, we don't document them since they are only used for
// development. Since they are not part of the public interface we may
Expand Down Expand Up @@ -96,6 +96,8 @@ func root(ctx context.Context, args ...string) int {
cmd, args := args[0], args[1:]

run_command:
ctx := context.Background()

var err error
switch cmd {
case "config":
Expand Down
17 changes: 9 additions & 8 deletions run.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func run(ctx context.Context, args []string) error {

var debugREPL *debug.REPL
if debugger {
debugREPL = debug.NewREPL(os.Stdin, os.Stdout)
debugREPL = debug.NewREPL(os.Stdin, os.Stderr)
ctx = debug.RegisterFunctionListener(ctx, debugREPL)
}

Expand Down Expand Up @@ -227,9 +227,9 @@ func run(ctx context.Context, args []string) error {
func instantiate(ctx context.Context, runtime wazero.Runtime, compiledModule wazero.CompiledModule, debugREPL *debug.REPL) (err error) {
if debugREPL != nil {
// The double defer is so that we can catch debug.{Quit,Restart}Error,
// whether it originates from deep with the WebAssembly module
// execution, or whether it originates from the OnEvent handler below
// which is called after the module exits.
// whether it originates from deep within the WebAssembly module, or
// whether it originates from the OnEvent handler below which is called
// after the module exits.
defer func() {
defer func() {
switch e := recover(); e {
Expand All @@ -243,10 +243,11 @@ func instantiate(ctx context.Context, runtime wazero.Runtime, compiledModule waz
}
}()

e := recover()
debugREPL.OnEvent(ctx, &debug.ModuleAfterEvent{Error: e})
if e != nil {
panic(e)
if panicErr := recover(); panicErr != nil {
debugREPL.OnEvent(ctx, &debug.ModuleAfterEvent{Error: panicErr})
panic(panicErr)
} else {
debugREPL.OnEvent(ctx, &debug.ModuleAfterEvent{Error: err})
}
}()

Expand Down

0 comments on commit 8653751

Please sign in to comment.