Skip to content

Commit

Permalink
fix(core): panic to error
Browse files Browse the repository at this point in the history
  • Loading branch information
arm64v8a committed Apr 17, 2023
1 parent 5c38ab8 commit 4f49953
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 10 deletions.
10 changes: 3 additions & 7 deletions libcore/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,11 @@ func Unsetenv(key string) error {
return os.Unsetenv(key)
}

func initCoreDefer() {
device.AllDefer("InitCore", ForceLog)
}

func InitCore(internalAssets string, externalAssets string, prefix string, useOfficial BoolFunc, // extractV2RayAssets
cachePath string, process string, //InitCore
enableLog bool, maxKB int32, //SetEnableLog
) {
defer initCoreDefer()
defer device.DeferPanicToError("InitCore", func(err error) { ForceLog(fmt.Sprintln(err)) })

isBgProcess := strings.HasSuffix(process, ":bg")

Expand All @@ -41,12 +37,12 @@ func InitCore(internalAssets string, externalAssets string, prefix string, useOf
go ForceLog(s)
} else {
// not fatal
ForceLog(fmt.Sprintln("Log not inited:", s, err.Error()))
// ForceLog(fmt.Sprintln("Log not inited:", s, err.Error()))
}

// Set up some component
go func() {
defer initCoreDefer()
defer device.DeferPanicToError("InitCore-go", func(err error) { ForceLog(fmt.Sprintln(err)) })
device.GoDebug(process)

externalAssetsPath = externalAssets
Expand Down
6 changes: 3 additions & 3 deletions libcore/device/debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ func GoDebug(any interface{}) {
}
}

func AllDefer(name string, log func(string)) {
func DeferPanicToError(name string, err func(error)) {
if r := recover(); r != nil {
s := fmt.Sprintln(name+" panic", r, string(debug.Stack()))
log(s)
s := fmt.Errorf("%s panic: %s\n%s", name, r, string(debug.Stack()))
err(s)
}
}
4 changes: 4 additions & 0 deletions libcore/speedtest.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package libcore
import (
"context"
"fmt"
"libcore/device"
"log"
"math/rand"
"net/http"
"strings"
Expand All @@ -13,6 +15,8 @@ import (
)

func UrlTestV2ray(instance *V2RayInstance, inbound string, link string, timeout int32) (int32, error) {
defer device.DeferPanicToError("UrlTestV2ray", func(err error) { log.Println(err) })

transport := &http.Transport{
TLSHandshakeTimeout: time.Duration(timeout) * time.Millisecond,
DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) {
Expand Down
7 changes: 7 additions & 0 deletions libcore/v2ray.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package libcore
import (
"context"
"fmt"
"libcore/device"
"log"
"strings"
"sync"
Expand Down Expand Up @@ -39,6 +40,8 @@ func NewV2rayInstance() *V2RayInstance {
}

func (instance *V2RayInstance) LoadConfig(content string) error {
defer device.DeferPanicToError("LoadConfig", func(err error) { log.Println(err) })

instance.access.Lock()
defer instance.access.Unlock()

Expand Down Expand Up @@ -77,6 +80,8 @@ func (instance *V2RayInstance) LoadConfig(content string) error {
}

func (instance *V2RayInstance) Start() error {
defer device.DeferPanicToError("Start", func(err error) { log.Println(err) })

instance.access.Lock()
defer instance.access.Unlock()
if instance.started {
Expand Down Expand Up @@ -105,6 +110,8 @@ func (instance *V2RayInstance) QueryStats(tag string, direct string) int64 {
}

func (instance *V2RayInstance) Close() error {
defer device.DeferPanicToError("Close", func(err error) { log.Println(err) })

instance.access.Lock()
defer instance.access.Unlock()
if instance.started {
Expand Down

0 comments on commit 4f49953

Please sign in to comment.