-
-
Notifications
You must be signed in to change notification settings - Fork 307
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1668 from safing/fix/spn-start-stop
Fix starting/stopping SPN + more
- Loading branch information
Showing
22 changed files
with
718 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package service | ||
|
||
import ( | ||
"bytes" | ||
"errors" | ||
"fmt" | ||
"io" | ||
"runtime" | ||
|
||
"github.com/maruel/panicparse/v2/stack" | ||
|
||
"github.com/safing/portmaster/base/utils/debug" | ||
"github.com/safing/portmaster/service/mgr" | ||
) | ||
|
||
// GetWorkerInfo returns the worker info of all running workers. | ||
func (i *Instance) GetWorkerInfo() (*mgr.WorkerInfo, error) { | ||
snapshot, _, err := stack.ScanSnapshot(bytes.NewReader(fullStack()), io.Discard, stack.DefaultOpts()) | ||
if err != nil && !errors.Is(err, io.EOF) { | ||
return nil, fmt.Errorf("get stack: %w", err) | ||
} | ||
|
||
infos := make([]*mgr.WorkerInfo, 0, 32) | ||
for _, m := range i.serviceGroup.Modules() { | ||
wi, _ := m.Manager().WorkerInfo(snapshot) // Does not fail when we provide a snapshot. | ||
infos = append(infos, wi) | ||
} | ||
for _, m := range i.SpnGroup.Modules() { | ||
wi, _ := m.Manager().WorkerInfo(snapshot) // Does not fail when we provide a snapshot. | ||
infos = append(infos, wi) | ||
} | ||
|
||
return mgr.MergeWorkerInfo(infos...), nil | ||
} | ||
|
||
// AddWorkerInfoToDebugInfo adds the worker info of all running workers to the debug info. | ||
func (i *Instance) AddWorkerInfoToDebugInfo(di *debug.Info) { | ||
info, err := i.GetWorkerInfo() | ||
if err != nil { | ||
di.AddSection( | ||
"Worker Status Failed", | ||
debug.UseCodeSection, | ||
err.Error(), | ||
) | ||
return | ||
} | ||
|
||
di.AddSection( | ||
fmt.Sprintf("Worker Status: %d/%d (%d?)", info.Running, len(info.Workers), info.Missing+info.Other), | ||
debug.UseCodeSection, | ||
info.Format(), | ||
) | ||
} | ||
|
||
func fullStack() []byte { | ||
buf := make([]byte, 8096) | ||
for { | ||
n := runtime.Stack(buf, true) | ||
if n < len(buf) { | ||
return buf[:n] | ||
} | ||
buf = make([]byte, 2*len(buf)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package service | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"github.com/safing/portmaster/base/notifications" | ||
"github.com/safing/portmaster/service/mgr" | ||
) | ||
|
||
func TestDebug(t *testing.T) { | ||
t.Parallel() | ||
|
||
// Create test instance with at least one worker. | ||
i := &Instance{} | ||
n, err := notifications.New(i) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
i.serviceGroup = mgr.NewGroup(n) | ||
i.SpnGroup = mgr.NewExtendedGroup() | ||
err = i.Start() | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
time.Sleep(100 * time.Millisecond) | ||
|
||
info, err := i.GetWorkerInfo() | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
t.Log(info) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.