summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJakob Unterwurzacher2016-11-26 18:18:14 +0100
committerJakob Unterwurzacher2016-11-26 18:18:14 +0100
commit7fc93ec0937d925941107a0d05341b6da8540d05 (patch)
tree6a1f80a9f0ed0aa25247fcb10225c491d5502d81
parent3f5c48e0585f6fe0782aded541e42879eb570e69 (diff)
main: fix USR1 race condition, fixes test flakiness
We could have gotten the signal before the handler was ready, which caused the process to wait indefinitely. This was the reason test.bash sometimes hung.
-rw-r--r--daemonize.go11
1 files changed, 7 insertions, 4 deletions
diff --git a/daemonize.go b/daemonize.go
index b9857e7..9869258 100644
--- a/daemonize.go
+++ b/daemonize.go
@@ -10,19 +10,21 @@ import (
"github.com/rfjakob/gocryptfs/internal/tlog"
)
-// The child sends us USR1 if the mount was successful
+// The child sends us USR1 if the mount was successful. Exit with error code
+// 0 if we get it.
func exitOnUsr1() {
c := make(chan os.Signal, 1)
signal.Notify(c, syscall.SIGUSR1)
- <-c
- os.Exit(0)
+ go func() {
+ <-c
+ os.Exit(0)
+ }()
}
// forkChild - execute ourselves once again, this time with the "-fg" flag, and
// wait for SIGUSR1 or child exit.
// This is a workaround for the missing true fork function in Go.
func forkChild() int {
- go exitOnUsr1()
name := os.Args[0]
newArgs := []string{"-fg", fmt.Sprintf("-notifypid=%d", os.Getpid())}
newArgs = append(newArgs, os.Args[1:]...)
@@ -30,6 +32,7 @@ func forkChild() int {
c.Stdout = os.Stdout
c.Stderr = os.Stderr
c.Stdin = os.Stdin
+ exitOnUsr1()
err := c.Start()
if err != nil {
tlog.Fatal.Printf("forkChild: starting %s failed: %v\n", name, err)