Skip to content

Commit

Permalink
recreate the control socket after a crash; fixes bblfsh#286
Browse files Browse the repository at this point in the history
Signed-off-by: Denys Smirnov <[email protected]>
  • Loading branch information
Denys Smirnov committed May 3, 2019
1 parent 01eba1f commit f629bc9
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions cmd/bblfshd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,9 +197,37 @@ func listenUser(d *daemon.Daemon) {
}
}

func unwrap(err error) error {
switch err := err.(type) {
case *net.OpError:
return unwrap(err.Err)
case *os.SyscallError:
return unwrap(err.Err)
}
return err
}

func isErrno(err error, eno syscall.Errno) bool {
if e, _ := unwrap(err).(syscall.Errno); e == eno {
return true
}
return false
}

func isAddrInUse(err error) bool {
return isErrno(err, syscall.EADDRINUSE)
}

func listenControl(d *daemon.Daemon) {
var err error
ctlListener, err = net.Listen(*ctl.network, *ctl.address)
if isAddrInUse(err) && *ctl.network == "unix" {
// bblfshd may have crashed and left the socket file
// remove it and try again
logrus.Warningf("control socket %s (%s) already exists, but is not active; re-creating", *ctl.address, *ctl.network)
_ = os.Remove(*ctl.address)
ctlListener, err = net.Listen(*ctl.network, *ctl.address)
}
if err != nil {
logrus.Fatalf("error creating control listener: %s", err)
}
Expand Down

0 comments on commit f629bc9

Please sign in to comment.