Skip to content

Commit

Permalink
fix: make doctor panic if hang too much time
Browse files Browse the repository at this point in the history
Signed-off-by: gfanton <8671905+gfanton@users.noreply.github.com>
  • Loading branch information
gfanton committed Oct 12, 2022
1 parent 2c54e1d commit 67bec21
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 15 deletions.
23 changes: 10 additions & 13 deletions go/cmd/berty-doctor/main.go
Expand Up @@ -6,7 +6,6 @@ import (
"fmt"
"os"
"os/exec"
"os/signal"
"strings"
"sync"
"time"
Expand All @@ -32,6 +31,16 @@ func main() {
flag.Parse()
var wg sync.WaitGroup
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()

go func() {
<-ctx.Done()
if ctx.Err() == context.DeadlineExceeded {
time.Sleep(time.Second * 5)
// if process still running after 5sc, panic
panic("timeout while waiting for doctor to exit")
}
}()

// check for programs
wg.Add(1)
Expand Down Expand Up @@ -108,18 +117,6 @@ func main() {
// FIXME: node version
// FIXME: check termcaps support for mini

// Cancel ctx on sigInt.
go func() {
signalChannel := make(chan os.Signal, 1)
signal.Notify(signalChannel, os.Interrupt)

select {
case <-signalChannel:
cancel()
case <-ctx.Done():
}
}()

// summary
{
wg.Wait()
Expand Down
22 changes: 20 additions & 2 deletions go/cmd/berty-doctor/rdvp.go
Expand Up @@ -8,19 +8,22 @@ import (
"io"
"math"
"math/rand"
"os"
"runtime"
"strconv"
"sync"
"time"

"github.com/denisbrodbeck/machineid"
ipfs_log "github.com/ipfs/go-log/v2"
p2p "github.com/libp2p/go-libp2p"
"github.com/libp2p/go-libp2p-core/discovery"
"github.com/libp2p/go-libp2p-core/peer"
"github.com/libp2p/go-libp2p-core/peerstore"
"github.com/libp2p/go-libp2p/p2p/protocol/ping"
ma "github.com/multiformats/go-multiaddr"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"moul.io/srand"

"berty.tech/berty/v2/go/internal/ipfsutil"
Expand All @@ -30,7 +33,22 @@ import (
const timeRounding = time.Microsecond * 10

func testRDVPs(ctx context.Context, gwg *sync.WaitGroup, addrs []string) {
defer (*gwg).Done()
defer gwg.Done()
var err error

logger := zap.NewNop()
if debug := os.Getenv("DOCTOR_DEBUG"); debug == "true" || debug == "yes" {
cfg := zap.NewDevelopmentConfig()
cfg.EncoderConfig.EncodeLevel = zapcore.CapitalColorLevelEncoder
if logger, err = cfg.Build(); err != nil {
panic(err)
}

ipfs_log.SetDebugLogging()
ipfs_log.SetPrimaryCore(logger.Core())

logger.Debug("debug mode enable")
}

// Isolate each server
var pis []peer.AddrInfo
Expand Down Expand Up @@ -143,7 +161,7 @@ func testRDVPs(ctx context.Context, gwg *sync.WaitGroup, addrs []string) {

// Setup discovery
disc := tinder.NewRendezvousDiscovery(
zap.NewNop(),
logger.Named("tinder"),
host,
tgtPi.ID,
rand.New(rand.NewSource(srand.SafeFast())), //nolint:gosec
Expand Down

0 comments on commit 67bec21

Please sign in to comment.