Browse Source

Panic wrapper updates
- Print panic after error in all `panicHandler` error cases
- Switch from `BasicWrap` to `Wrap` to pass the list of signals that the server expects to handle through the
`WrapConfig`

Michael Goldberger 9 năm trước cách đây
mục cha
commit
518f3b8f1e
2 tập tin đã thay đổi với 18 bổ sung7 xóa
  1. 12 7
      Server/main.go
  2. 6 0
      psiphon/server/services.go

+ 12 - 7
Server/main.go

@@ -28,6 +28,7 @@ import (
 	"os"
 	"strconv"
 	"strings"
+	"syscall"
 	"time"
 
 	"github.com/Psiphon-Inc/panicwrap"
@@ -189,15 +190,19 @@ func main() {
 
 		loadedConfigJSON = configJSON
 
+		// Comments from: https://github.com/mitchellh/panicwrap#usage
 		// Unhandled panic wrapper. Logs it, then re-executes the current executable
-		exitStatus, exitErr := panicwrap.BasicWrap(panicHandler)
-		if exitErr != nil {
+		exitStatus, err := panicwrap.Wrap(&panicwrap.WrapConfig{
+			Handler:        panicHandler,
+			ForwardSignals: []os.Signal{os.Interrupt, os.Kill, syscall.SIGTERM, syscall.SIGUSR1, syscall.SIGUSR2, syscall.SIGTSTP, syscall.SIGCONT},
+		})
+		if err != nil {
 			fmt.Printf("failed to set up the panic wrapper: %s\n", err)
 			os.Exit(1)
 		}
 
 		// If exitStatus >= 0, then we're the parent process and the panicwrap
-		// re-executed ourselves and completed. Just exit with the proper status.
+		// e-executed ourselves and completed. Just exit with the proper status.
 		if exitStatus >= 0 {
 			os.Exit(exitStatus)
 		}
@@ -226,7 +231,7 @@ func panicHandler(output string) {
 	if len(loadedConfigJSON) > 0 {
 		config, err := server.LoadConfig([]byte(loadedConfigJSON))
 		if err != nil {
-			fmt.Errorf("error parsing configuration file: %s", err)
+			fmt.Printf("error parsing configuration file: %s\n%s\n", err, output)
 			os.Exit(1)
 		}
 
@@ -241,7 +246,7 @@ func panicHandler(output string) {
 		if config.PanicLogFilename != "" {
 			panicLog, err := rotate.NewRotatableFileWriter(config.PanicLogFilename, 0666)
 			if err != nil {
-				fmt.Printf("unable to set panic log output: %s\n", err)
+				fmt.Printf("unable to set panic log output: %s\n%s\n", err, output)
 				os.Exit(1)
 			}
 			jsonWriter = panicLog
@@ -252,11 +257,11 @@ func panicHandler(output string) {
 		enc := json.NewEncoder(jsonWriter)
 		err = enc.Encode(logEvent)
 		if err != nil {
-			fmt.Printf("unable to serialize panic message to JSON: %s\n", err)
+			fmt.Printf("unable to serialize panic message to JSON: %s\n%s\n", err, output)
 			os.Exit(1)
 		}
 	} else {
-		fmt.Errorf("no configuration JSON was loaded, cannot continue")
+		fmt.Printf("no configuration JSON was loaded, cannot continue\n%s\n", output)
 		os.Exit(1)
 	}
 

+ 6 - 0
psiphon/server/services.go

@@ -134,6 +134,12 @@ func RunServices(configJSON []byte) error {
 		}
 	}()
 
+	// In addition to the actual signal handling here, there is
+	// a list of signals that need to be passed through panicwrap
+	// in 'github.com/Psiphon-Labs/psiphon-tunnel-core/Server/main.go'
+	// where 'panicwrap.Wrap' is called. The handled signals below, and the
+	// list there must be kept in sync to ensure proper signal handling
+
 	// An OS signal triggers an orderly shutdown
 	systemStopSignal := make(chan os.Signal, 1)
 	signal.Notify(systemStopSignal, os.Interrupt, os.Kill, syscall.SIGTERM)