Skip to content

Commit

Permalink
Fix yield and ctrlc
Browse files Browse the repository at this point in the history
  • Loading branch information
raphaelvigee committed May 11, 2024
1 parent 7c1c3a6 commit 025866b
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 54 deletions.
26 changes: 19 additions & 7 deletions BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,6 @@ deps = ["go.mod", "go.sum"] + glob(

release = "release" in CONFIG["profiles"]

def _gobuild(pkg, args):
return "go build -trimpath {} -ldflags='-s -w' -o $OUT {}".format(args, pkg)

build_flags = ""
if release:
version = target(
Expand All @@ -101,10 +98,7 @@ for os in ["linux", "darwin"]:
name = "build_{}_{}".format(os, arch),
run = [
"go version",
_gobuild(
"github.com/hephbuild/heph/cmd/heph",
build_flags,
),
"go build -o $OUT -trimpath -ldflags='-s -w' github.com/hephbuild/heph/cmd/heph",
],
out = "heph_{}_{}".format(os, arch),
deps = deps,
Expand All @@ -119,6 +113,24 @@ for os in ["linux", "darwin"]:
)
builds.append(t)

target(
name = "build_debug_{}_{}".format(os, arch),
run = [
"go version",
"go build -o $OUT -trimpath -gcflags='all=-N -l' github.com/hephbuild/heph/cmd/heph",
],
out = "heph_debug_{}_{}".format(os, arch),
deps = deps,
env = {
"GOOS": os,
"GOARCH": arch,
"CGO_ENABLED": "0",
},
tools = ["go"],
labels = ["build-debug"],
pass_env = go_env_vars,
)

target(
name = "cp_builds",
run = "cp * $1",
Expand Down
8 changes: 7 additions & 1 deletion cmd/heph/entrypoint.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"context"
"errors"
"github.com/hephbuild/heph/bootstrap"
"github.com/hephbuild/heph/utils/xcontext"
Expand All @@ -11,13 +12,18 @@ import (

func execute() error {
ctx, cancel := xcontext.BootstrapSoftCancel()
defer cancel()
defer cancel(nil)

vfssimple.WithContext(ctx)

err := rootCmd.ExecuteContext(ctx)
postRun(err)
if err != nil {
// Handle ctrlc gracefuly
if ctx.Err() != nil {
return context.Cause(ctx)
}

return err
}

Expand Down
2 changes: 1 addition & 1 deletion platform/provider_local.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func (p *localExecutor) Exec(ctx context.Context, o ExecOptions, execArgs []stri
}

sctx, hctx, cancel := xcontext.NewSoftCancel(ctx)
defer cancel()
defer cancel(nil)

cmd := sandbox.Exec(sandbox.ExecConfig{
Context: hctx,
Expand Down
52 changes: 27 additions & 25 deletions utils/xcontext/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package xcontext

import (
"context"
"fmt"
"github.com/hephbuild/heph/log/log"
"github.com/hephbuild/heph/utils/ads"
"github.com/hephbuild/heph/utils/xsync"
Expand All @@ -22,9 +23,11 @@ func IsDone(ctx context.Context) bool {
}
}

type CancelFunc = context.CancelCauseFunc

type entry struct {
softCancel context.CancelFunc
hardCancel context.CancelFunc
softCancel CancelFunc
hardCancel CancelFunc
}

type state struct {
Expand All @@ -42,9 +45,9 @@ func newSoftCancelState() *state {

// New returns one context that will be canceled by soft cancel first, the second one will act as a force cancel
// both inherit values from their parents
func (a *state) New(parent context.Context) (context.Context, context.Context, func()) {
scctx, scancel := context.WithCancel(parent)
hcctx, hcancel := context.WithCancel(context.Background())
func (a *state) New(parent context.Context) (context.Context, context.Context, CancelFunc) {
scctx, scancel := context.WithCancelCause(parent)
hcctx, hcancel := context.WithCancelCause(context.Background())

hctx := CancellableContext{
Parent: parent,
Expand All @@ -58,9 +61,9 @@ func (a *state) New(parent context.Context) (context.Context, context.Context, f

a.add(e)

return scctx, hctx, func() {
e.softCancel()
e.hardCancel()
return scctx, hctx, func(cause error) {
e.softCancel(cause)
e.hardCancel(cause)

a.remove(e)
}
Expand Down Expand Up @@ -95,7 +98,7 @@ func (a *state) has() bool {
return len(a.ctxs) > 0
}

func (a *state) hardCancel() bool {
func (a *state) hardCancel(cause error) bool {
a.m.Lock()
defer a.m.Unlock()

Expand All @@ -104,7 +107,7 @@ func (a *state) hardCancel() bool {
}

for _, e := range a.ctxs {
e.hardCancel()
e.hardCancel(cause)
}

return true
Expand All @@ -113,7 +116,7 @@ func (a *state) hardCancel() bool {
type keySoftCancelState struct{}

// NewSoftCancel See softCancel.New
func NewSoftCancel(parent context.Context) (context.Context, context.Context, context.CancelFunc) {
func NewSoftCancel(parent context.Context) (context.Context, context.Context, CancelFunc) {
sc := parent.Value(keySoftCancelState{}).(*state)

return sc.New(parent)
Expand All @@ -140,33 +143,32 @@ func Cancel(ctx context.Context) {
}

const stuckTimeout = 5 * time.Second
const forceTimeout = 1 * time.Second

func BootstrapSoftCancel() (context.Context, context.CancelFunc) {
ctx, cancel := context.WithCancel(context.Background())
func BootstrapSoftCancel() (context.Context, CancelFunc) {
ctx, cancel := context.WithCancelCause(context.Background())

sigCh := make(chan os.Signal)
signal.Notify(sigCh, os.Interrupt, syscall.SIGTERM)

sc := newSoftCancelState()

go func() {
<-sigCh
cancel()
sig := <-sigCh
cancel(fmt.Errorf(sig.String()))
if sc.has() {
hardCanceled := false
go func() {
<-time.After(200 * time.Millisecond)
<-time.After(forceTimeout)
if hardCanceled {
return
}
log.Warnf("Attempting to cancel... ctrl+c one more time to force")
}()
select {
case <-sigCh:
}
log.Warnf("Forcing cancellation...")
sig := <-sigCh
hardCanceled = true
sc.hardCancel()
log.Warnf("Forcing cancellation...")
sc.hardCancel(fmt.Errorf(sig.String()))
select {
// Wait for soft cancel to all be unregistered, should be fast, unless something is stuck
case <-sc.wait():
Expand All @@ -180,7 +182,7 @@ func BootstrapSoftCancel() (context.Context, context.CancelFunc) {
}

log.Error("Something seems to be stuck, ctrl+c one more time to forcefully exit")
sig := <-sigCh
sig = <-sigCh
sigN := 0
if sig, ok := sig.(syscall.Signal); ok {
sigN = int(sig)
Expand All @@ -194,8 +196,8 @@ func BootstrapSoftCancel() (context.Context, context.CancelFunc) {
sigCh <- os.Interrupt
}))

return ctx, func() {
cancel()
sc.hardCancel()
return ctx, func(cause error) {
cancel(cause)
sc.hardCancel(cause)
}
}
8 changes: 6 additions & 2 deletions worker2/dag.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,15 +83,19 @@ func (d *nodesTransitive[T]) TransitiveSet() *sets.Set[*Node[T], *Node[T]] {
func (d *nodesTransitive[T]) TransitiveValues() iter.Seq2[int, T] {
return func(yield func(int, T) bool) {
for i, node := range d.TransitiveSet().Slice() {
yield(i, node.V)
if !yield(i, node.V) {
break
}
}
}
}

func (d *nodesTransitive[T]) Values() iter.Seq2[int, T] {
return func(yield func(int, T) bool) {
for i, node := range d.Set().Slice() {
yield(i, node.V)
if !yield(i, node.V) {
break
}
}
}
}
Expand Down
23 changes: 5 additions & 18 deletions worker2/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"github.com/bep/debounce"
"github.com/dlsniper/debugger"
"github.com/hephbuild/heph/utils/ads"
"go.uber.org/multierr"
"runtime"
"sync"
"sync/atomic"
Expand Down Expand Up @@ -111,13 +110,10 @@ func (e *Engine) waitForDeps(exec *Execution) error {
exec.c.L.Lock()
defer exec.c.L.Unlock()

var errs []error
for {
depObj := exec.Dep.GetNode()

allDepsSucceeded := true
allDepsDone := true
errs = errs[:0]
for _, dep := range depObj.Dependencies.Values() {
depExec := e.scheduleOne(dep)

Expand All @@ -129,30 +125,21 @@ func (e *Engine) waitForDeps(exec *Execution) error {
allDepsDone = false
}

if state != ExecStateSucceeded {
allDepsSucceeded = false
}

switch state {
case ExecStateSkipped, ExecStateFailed:
errs = append(errs, Error{
// Prevent accumulating a million errors, fail early
return Error{
ID: depExec.ID,
State: depExec.State,
Name: depExec.Dep.GetName(),
Err: depExec.Err,
})
}
}
}

if allDepsDone {
if len(errs) > 0 {
return multierr.Combine(errs...)
}

if allDepsSucceeded {
if e.tryFreeze(depObj) {
return nil
}
if e.tryFreeze(depObj) {
return nil
}
}

Expand Down

0 comments on commit 025866b

Please sign in to comment.