Skip to content

Commit

Permalink
Use context interrupts consistently in more places (ethereum-optimism…
Browse files Browse the repository at this point in the history
…#11511)

* Use context interrupts consistently in more places

* Fix CI lint errors

(cherry picked from commit 0410b7e)

* op-service/ctxinterrupt: address review comments

---------

Co-authored-by: protolambda <[email protected]>
  • Loading branch information
anacrolix and protolambda authored Aug 30, 2024
1 parent db8154b commit d520441
Show file tree
Hide file tree
Showing 29 changed files with 273 additions and 221 deletions.
16 changes: 2 additions & 14 deletions cannon/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@ import (
"errors"
"fmt"
"os"
"os/signal"
"syscall"

"github.com/ethereum-optimism/optimism/op-service/ctxinterrupt"
"github.com/urfave/cli/v2"

"github.com/ethereum-optimism/optimism/cannon/cmd"
Expand All @@ -23,18 +22,7 @@ func main() {
cmd.WitnessCommand,
cmd.RunCommand,
}
ctx, cancel := context.WithCancel(context.Background())

c := make(chan os.Signal, 1)
signal.Notify(c, syscall.SIGINT, syscall.SIGTERM)
go func() {
for {
<-c
cancel()
fmt.Println("\r\nExiting...")
}
}()

ctx := ctxinterrupt.WithSignalWaiterMain(context.Background())
err := app.RunContext(ctx, os.Args)
if err != nil {
if errors.Is(err, ctx.Err()) {
Expand Down
6 changes: 2 additions & 4 deletions op-alt-da/cmd/daserver/entrypoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (
"github.com/urfave/cli/v2"

altda "github.com/ethereum-optimism/optimism/op-alt-da"
"github.com/ethereum-optimism/optimism/op-service/ctxinterrupt"
oplog "github.com/ethereum-optimism/optimism/op-service/log"
"github.com/ethereum-optimism/optimism/op-service/opio"
)

func StartDAServer(cliCtx *cli.Context) error {
Expand Down Expand Up @@ -55,7 +55,5 @@ func StartDAServer(cliCtx *cli.Context) error {
}
}()

opio.BlockOnInterrupts()

return nil
return ctxinterrupt.Wait(cliCtx.Context)
}
4 changes: 2 additions & 2 deletions op-alt-da/cmd/daserver/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (

opservice "github.com/ethereum-optimism/optimism/op-service"
"github.com/ethereum-optimism/optimism/op-service/cliapp"
"github.com/ethereum-optimism/optimism/op-service/ctxinterrupt"
oplog "github.com/ethereum-optimism/optimism/op-service/log"
"github.com/ethereum-optimism/optimism/op-service/opio"
)

var Version = "v0.0.1"
Expand All @@ -26,7 +26,7 @@ func main() {
app.Description = "Service for storing AltDA inputs"
app.Action = StartDAServer

ctx := opio.WithInterruptBlocker(context.Background())
ctx := ctxinterrupt.WithSignalWaiterMain(context.Background())
err := app.RunContext(ctx, os.Args)
if err != nil {
log.Crit("Application failed", "message", err)
Expand Down
4 changes: 2 additions & 2 deletions op-batcher/cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ import (
"github.com/ethereum-optimism/optimism/op-batcher/metrics"
opservice "github.com/ethereum-optimism/optimism/op-service"
"github.com/ethereum-optimism/optimism/op-service/cliapp"
"github.com/ethereum-optimism/optimism/op-service/ctxinterrupt"
oplog "github.com/ethereum-optimism/optimism/op-service/log"
"github.com/ethereum-optimism/optimism/op-service/metrics/doc"
"github.com/ethereum-optimism/optimism/op-service/opio"
"github.com/ethereum/go-ethereum/log"
)

Expand All @@ -40,7 +40,7 @@ func main() {
},
}

ctx := opio.WithInterruptBlocker(context.Background())
ctx := ctxinterrupt.WithSignalWaiterMain(context.Background())
err := app.RunContext(ctx, os.Args)
if err != nil {
log.Crit("Application failed", "message", err)
Expand Down
6 changes: 2 additions & 4 deletions op-bootnode/bootnode/entrypoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ import (
"github.com/ethereum-optimism/optimism/op-node/p2p"
p2pcli "github.com/ethereum-optimism/optimism/op-node/p2p/cli"
"github.com/ethereum-optimism/optimism/op-node/rollup"
"github.com/ethereum-optimism/optimism/op-service/ctxinterrupt"
"github.com/ethereum-optimism/optimism/op-service/eth"
oplog "github.com/ethereum-optimism/optimism/op-service/log"
opmetrics "github.com/ethereum-optimism/optimism/op-service/metrics"
"github.com/ethereum-optimism/optimism/op-service/opio"
oprpc "github.com/ethereum-optimism/optimism/op-service/rpc"
)

Expand Down Expand Up @@ -116,9 +116,7 @@ func Main(cliCtx *cli.Context) error {
m.RecordUp()
}

opio.BlockOnInterrupts()

return nil
return ctxinterrupt.Wait(ctx)
}

// validateConfig ensures the minimal config required to run a bootnode
Expand Down
4 changes: 2 additions & 2 deletions op-chain-ops/cmd/check-ecotone/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ import (
op_service "github.com/ethereum-optimism/optimism/op-service"
"github.com/ethereum-optimism/optimism/op-service/cliapp"
"github.com/ethereum-optimism/optimism/op-service/client"
"github.com/ethereum-optimism/optimism/op-service/ctxinterrupt"
"github.com/ethereum-optimism/optimism/op-service/dial"
"github.com/ethereum-optimism/optimism/op-service/eth"
oplog "github.com/ethereum-optimism/optimism/op-service/log"
"github.com/ethereum-optimism/optimism/op-service/opio"
"github.com/ethereum-optimism/optimism/op-service/predeploys"
"github.com/ethereum-optimism/optimism/op-service/retry"
"github.com/ethereum-optimism/optimism/op-service/sources"
Expand Down Expand Up @@ -168,7 +168,7 @@ func makeCommandAction(fn CheckAction) func(c *cli.Context) error {
logCfg := oplog.ReadCLIConfig(c)
logger := oplog.NewLogger(c.App.Writer, logCfg)

c.Context = opio.CancelOnInterrupt(c.Context)
c.Context = ctxinterrupt.WithCancelOnInterrupt(c.Context)
l1Cl, err := ethclient.DialContext(c.Context, c.String(EndpointL1.Name))
if err != nil {
return fmt.Errorf("failed to dial L1 RPC: %w", err)
Expand Down
4 changes: 2 additions & 2 deletions op-chain-ops/cmd/check-fjord/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (
"github.com/ethereum-optimism/optimism/op-chain-ops/cmd/check-fjord/checks"
op_service "github.com/ethereum-optimism/optimism/op-service"
"github.com/ethereum-optimism/optimism/op-service/cliapp"
"github.com/ethereum-optimism/optimism/op-service/ctxinterrupt"
oplog "github.com/ethereum-optimism/optimism/op-service/log"
"github.com/ethereum-optimism/optimism/op-service/opio"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/urfave/cli/v2"
Expand Down Expand Up @@ -54,7 +54,7 @@ func makeCommandAction(fn CheckAction) func(c *cli.Context) error {
logCfg := oplog.ReadCLIConfig(c)
logger := oplog.NewLogger(c.App.Writer, logCfg)

c.Context = opio.CancelOnInterrupt(c.Context)
c.Context = ctxinterrupt.WithCancelOnInterrupt(c.Context)
l2Cl, err := ethclient.DialContext(c.Context, c.String(EndpointL2.Name))
if err != nil {
return fmt.Errorf("failed to dial L2 RPC: %w", err)
Expand Down
4 changes: 2 additions & 2 deletions op-chain-ops/cmd/op-simulate/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ import (

op_service "github.com/ethereum-optimism/optimism/op-service"
"github.com/ethereum-optimism/optimism/op-service/cliapp"
"github.com/ethereum-optimism/optimism/op-service/ctxinterrupt"
oplog "github.com/ethereum-optimism/optimism/op-service/log"
"github.com/ethereum-optimism/optimism/op-service/opio"
)

var EnvPrefix = "OP_SIMULATE"
Expand Down Expand Up @@ -82,7 +82,7 @@ func main() {
}

func mainAction(c *cli.Context) error {
ctx := opio.CancelOnInterrupt(c.Context)
ctx := ctxinterrupt.WithCancelOnInterrupt(c.Context)
logCfg := oplog.ReadCLIConfig(c)
logger := oplog.NewLogger(c.App.Writer, logCfg)

Expand Down
4 changes: 2 additions & 2 deletions op-challenger/cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ import (
"github.com/ethereum-optimism/optimism/op-challenger/version"
opservice "github.com/ethereum-optimism/optimism/op-service"
"github.com/ethereum-optimism/optimism/op-service/cliapp"
"github.com/ethereum-optimism/optimism/op-service/ctxinterrupt"
oplog "github.com/ethereum-optimism/optimism/op-service/log"
"github.com/ethereum-optimism/optimism/op-service/opio"
)

var (
Expand All @@ -29,7 +29,7 @@ var VersionWithMeta = opservice.FormatVersion(version.Version, GitCommit, GitDat

func main() {
args := os.Args
ctx := opio.WithInterruptBlocker(context.Background())
ctx := ctxinterrupt.WithSignalWaiterMain(context.Background())
if err := run(ctx, args, func(ctx context.Context, l log.Logger, config *config.Config) (cliapp.Lifecycle, error) {
return challenger.Main(ctx, l, config, metrics.NewMetrics())
}); err != nil {
Expand Down
4 changes: 2 additions & 2 deletions op-challenger/cmd/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (
"github.com/ethereum-optimism/optimism/op-challenger/flags"
contractMetrics "github.com/ethereum-optimism/optimism/op-challenger/game/fault/contracts/metrics"
opservice "github.com/ethereum-optimism/optimism/op-service"
"github.com/ethereum-optimism/optimism/op-service/ctxinterrupt"
"github.com/ethereum-optimism/optimism/op-service/dial"
"github.com/ethereum-optimism/optimism/op-service/opio"
"github.com/ethereum-optimism/optimism/op-service/sources/batching"
"github.com/ethereum-optimism/optimism/op-service/txmgr"
"github.com/ethereum-optimism/optimism/op-service/txmgr/metrics"
Expand All @@ -20,7 +20,7 @@ type ContractCreator[T any] func(context.Context, contractMetrics.ContractMetric

func Interruptible(action cli.ActionFunc) cli.ActionFunc {
return func(ctx *cli.Context) error {
ctx.Context = opio.CancelOnInterrupt(ctx.Context)
ctx.Context = ctxinterrupt.WithCancelOnInterrupt(ctx.Context)
return action(ctx)
}
}
Expand Down
4 changes: 2 additions & 2 deletions op-conductor/cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import (
"github.com/ethereum-optimism/optimism/op-conductor/flags"
opservice "github.com/ethereum-optimism/optimism/op-service"
"github.com/ethereum-optimism/optimism/op-service/cliapp"
"github.com/ethereum-optimism/optimism/op-service/ctxinterrupt"
oplog "github.com/ethereum-optimism/optimism/op-service/log"
"github.com/ethereum-optimism/optimism/op-service/opio"
)

var (
Expand All @@ -34,7 +34,7 @@ func main() {
app.Action = cliapp.LifecycleCmd(OpConductorMain)
app.Commands = []*cli.Command{}

ctx := opio.WithInterruptBlocker(context.Background())
ctx := ctxinterrupt.WithSignalWaiterMain(context.Background())
err := app.RunContext(ctx, os.Args)
if err != nil {
log.Crit("Application failed", "message", err)
Expand Down
2 changes: 1 addition & 1 deletion op-conductor/consensus/raft_fsm.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func NewUnsafeHeadTracker(log log.Logger) *unsafeHeadTracker {

// Apply implements raft.FSM, it applies the latest change (latest unsafe head payload) to FSM.
func (t *unsafeHeadTracker) Apply(l *raft.Log) interface{} {
if l.Data == nil || len(l.Data) == 0 {
if len(l.Data) == 0 {
return fmt.Errorf("log data is nil or empty")
}

Expand Down
4 changes: 2 additions & 2 deletions op-dispute-mon/cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ import (
"github.com/ethereum-optimism/optimism/op-dispute-mon/version"
opservice "github.com/ethereum-optimism/optimism/op-service"
"github.com/ethereum-optimism/optimism/op-service/cliapp"
"github.com/ethereum-optimism/optimism/op-service/ctxinterrupt"
oplog "github.com/ethereum-optimism/optimism/op-service/log"
"github.com/ethereum-optimism/optimism/op-service/opio"
)

var (
Expand All @@ -28,7 +28,7 @@ var VersionWithMeta = opservice.FormatVersion(version.Version, GitCommit, GitDat

func main() {
args := os.Args
ctx := opio.WithInterruptBlocker(context.Background())
ctx := ctxinterrupt.WithSignalWaiterMain(context.Background())
if err := run(ctx, args, monitor.Main); err != nil {
log.Crit("Application failed", "err", err)
}
Expand Down
3 changes: 2 additions & 1 deletion op-e2e/external_geth/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@ func run(configPath string) error {
fmt.Printf("================== op-geth shim awaiting termination ==========================\n")

sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
defer signal.Stop(sigs)
signal.Notify(sigs, os.Interrupt, syscall.SIGTERM)

select {
case <-sigs:
Expand Down
4 changes: 2 additions & 2 deletions op-node/cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ import (
"github.com/ethereum-optimism/optimism/op-node/version"
opservice "github.com/ethereum-optimism/optimism/op-service"
"github.com/ethereum-optimism/optimism/op-service/cliapp"
"github.com/ethereum-optimism/optimism/op-service/ctxinterrupt"
oplog "github.com/ethereum-optimism/optimism/op-service/log"
"github.com/ethereum-optimism/optimism/op-service/metrics/doc"
"github.com/ethereum-optimism/optimism/op-service/opio"
)

var (
Expand Down Expand Up @@ -64,7 +64,7 @@ func main() {
},
}

ctx := opio.WithInterruptBlocker(context.Background())
ctx := ctxinterrupt.WithSignalWaiterMain(context.Background())
err := app.RunContext(ctx, os.Args)
if err != nil {
log.Crit("Application failed", "message", err)
Expand Down
6 changes: 5 additions & 1 deletion op-proposer/cmd/main.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package main

import (
"context"
"os"

"github.com/ethereum-optimism/optimism/op-service/ctxinterrupt"

opservice "github.com/ethereum-optimism/optimism/op-service"
"github.com/urfave/cli/v2"

Expand Down Expand Up @@ -38,7 +41,8 @@ func main() {
},
}

err := app.Run(os.Args)
ctx := ctxinterrupt.WithSignalWaiterMain(context.Background())
err := app.RunContext(ctx, os.Args)
if err != nil {
log.Crit("Application failed", "message", err)
}
Expand Down
35 changes: 10 additions & 25 deletions op-service/cliapp/lifecycle.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (

"github.com/urfave/cli/v2"

"github.com/ethereum-optimism/optimism/op-service/opio"
"github.com/ethereum-optimism/optimism/op-service/ctxinterrupt"
)

type Lifecycle interface {
Expand All @@ -29,36 +29,26 @@ type Lifecycle interface {
// a shutdown when the Stop context is not expired.
type LifecycleAction func(ctx *cli.Context, close context.CancelCauseFunc) (Lifecycle, error)

var interruptErr = errors.New("interrupt signal")

// LifecycleCmd turns a LifecycleAction into an CLI action,
// by instrumenting it with CLI context and signal based termination.
// The signals are caught with the opio.BlockFn attached to the context, if any.
// If no block function is provided, it adds default interrupt handling.
// by instrumenting it with CLI context and signal based cancellation.
// The signals are caught with the ctxinterrupt.waiter attached to the context, or default
// interrupt signal handling if not already provided.
// The app may continue to run post-processing until fully shutting down.
// The user can force an early shut-down during post-processing by sending a second interruption signal.
func LifecycleCmd(fn LifecycleAction) cli.ActionFunc {
return func(ctx *cli.Context) error {
hostCtx := ctx.Context
blockOnInterrupt := opio.BlockerFromContext(hostCtx)
if blockOnInterrupt == nil { // add default interrupt blocker to context if none is set.
hostCtx = opio.WithInterruptBlocker(hostCtx)
blockOnInterrupt = opio.BlockerFromContext(hostCtx)
}
appCtx, appCancel := context.WithCancelCause(hostCtx)
hostCtx, stop := ctxinterrupt.WithSignalWaiter(ctx.Context)
defer stop()
appCtx, appCancel := context.WithCancelCause(ctxinterrupt.WithCancelOnInterrupt(hostCtx))
// This is updated so the fn callback cli.Context uses the appCtx we just made.
ctx.Context = appCtx

go func() {
blockOnInterrupt(appCtx)
appCancel(interruptErr)
}()

appLifecycle, err := fn(ctx, appCancel)
if err != nil {
// join errors to include context cause (nil errors are dropped)
return errors.Join(
fmt.Errorf("failed to setup: %w", err),
context.Cause(appCtx),
context.Cause(ctx.Context),
)
}

Expand All @@ -75,15 +65,10 @@ func LifecycleCmd(fn LifecycleAction) cli.ActionFunc {

// Graceful stop context.
// This allows the service to idle before shutdown, if halted. User may interrupt.
stopCtx, stopCancel := context.WithCancelCause(hostCtx)
go func() {
blockOnInterrupt(stopCtx)
stopCancel(interruptErr)
}()
stopCtx := ctxinterrupt.WithCancelOnInterrupt(hostCtx)

// Execute graceful stop.
stopErr := appLifecycle.Stop(stopCtx)
stopCancel(nil)
// note: Stop implementation may choose to suppress a context error,
// if it handles it well (e.g. stop idling after a halt).
if stopErr != nil {
Expand Down
Loading

0 comments on commit d520441

Please sign in to comment.