Skip to content

Commit

Permalink
fix hang on shutdown
Browse files Browse the repository at this point in the history
  • Loading branch information
0pcom committed Dec 6, 2023
1 parent 6349e60 commit 586ee26
Showing 1 changed file with 39 additions and 17 deletions.
56 changes: 39 additions & 17 deletions cmd/dmsgweb/commands/dmsgweb.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"regexp"
"strings"
"time"
"sync"

"github.com/armon/go-socks5"
"golang.org/x/net/proxy"
Expand All @@ -29,7 +30,7 @@ import (
dmsg "github.com/skycoin/dmsg/pkg/dmsg"
"github.com/skycoin/dmsg/pkg/dmsghttp"
)

var wg sync.WaitGroup
type customResolver struct{}

func (r *customResolver) Resolve(ctx context.Context, name string) (context.Context, net.IP, error) {
Expand All @@ -49,7 +50,7 @@ func (r *customResolver) Resolve(ctx context.Context, name string) (context.Cont

var (
httpC http.Client
httpClient *http.Client
httpClient http.Client
dmsgDisc string
dmsgSessions int
filterDomainSuffix string
Expand Down Expand Up @@ -125,7 +126,7 @@ var RootCmd = &cobra.Command{
}
// Configure custom HTTP transport with SOCKS5 proxy
// Configure HTTP client with custom transport
httpClient = &http.Client{
httpClient = http.Client{
Transport: &http.Transport{
Dial: dialer.Dial,
},
Expand Down Expand Up @@ -164,7 +165,7 @@ var RootCmd = &cobra.Command{
return dialer.Dial(network, addr)
}
}
fmt.Println(addr)
dmsgWebLog.Debug("Dialing address:", addr)
return net.Dial(network, addr)
},
}
Expand All @@ -177,12 +178,17 @@ var RootCmd = &cobra.Command{
if err != nil {
log.Fatalf("Failed to create SOCKS5 server: %v", err)
}
wg.Add(2)

go func() {
defer wg.Done()
dmsgWebLog.Debug("Serving SOCKS5 proxy on "+socksAddr)
err := server.ListenAndServe("tcp", socksAddr)
if err != nil {
log.Fatalf("Failed to start SOCKS5 server: %v", err)
}
dmsgWebLog.Debug("Stopped serving SOCKS5 proxy on "+socksAddr)

}()

r := gin.New()
Expand All @@ -192,7 +198,7 @@ var RootCmd = &cobra.Command{
r.Use(loggingMiddleware())

r.Any("/*path", func(c *gin.Context) {
fmt.Println(c.Request.Host)
dmsgWebLog.Debug("c.Request.Host:", c.Request.Host)
hostParts := strings.Split(c.Request.Host, ":")
var dmsgp string
if len(hostParts) > 1 {
Expand Down Expand Up @@ -227,33 +233,49 @@ var RootCmd = &cobra.Command{
c.Status(http.StatusOK)
io.Copy(c.Writer, resp.Body) //nolint
})
r.Run(":" + webPort) //nolint
go func() {
defer wg.Done()
dmsgWebLog.Debug("Serving http on "+webPort)
r.Run(":" + webPort) //nolint
dmsgWebLog.Debug("Stopped serving http on "+webPort)
}()
wg.Wait()
<-ctx.Done()
cancel()
closeDmsg()
},
}

func startDmsg(ctx context.Context, pk cipher.PubKey, sk cipher.SecKey) (dmsgC *dmsg.Client, stop func(), err error) {

Check failure on line 249 in cmd/dmsgweb/commands/dmsgweb.go

View workflow job for this annotation

GitHub Actions / linux

`startDmsg` - result `err` is always `nil` (unparam)
dmsgC = dmsg.NewClient(pk, sk, disc.NewHTTP(dmsgDisc, httpClient, dmsgWebLog), &dmsg.Config{MinSessions: dmsgSessions})
go dmsgC.Serve(context.Background())
dmsgC = dmsg.NewClient(pk, sk, disc.NewHTTP(dmsgDisc, &httpClient, dmsgWebLog), &dmsg.Config{MinSessions: dmsgSessions})

wg.Add(1)

go func() {
defer wg.Done()
select {
case <-ctx.Done():
dmsgC.Close()

Check failure on line 258 in cmd/dmsgweb/commands/dmsgweb.go

View workflow job for this annotation

GitHub Actions / linux

Error return value of `dmsgC.Close` is not checked (errcheck)
dmsgWebLog.WithError(ctx.Err()).Debug("Disconnected from dmsg network due to context cancellation.")
os.Exit(0)
case <-dmsgC.Ready():
dmsgWebLog.Debug("Dmsg network ready.")
}
}()

stop = func() {
err := dmsgC.Close()
dmsgWebLog.WithError(err).Debug("Disconnected from dmsg network.")
fmt.Printf("\n")
}

dmsgWebLog.WithField("public_key", pk.String()).WithField("dmsg_disc", dmsgDisc).
Debug("Connecting to dmsg network...")

select {
case <-ctx.Done():
stop()
return nil, nil, ctx.Err()

case <-dmsgC.Ready():
dmsgWebLog.Debug("Dmsg network ready.")
return dmsgC, stop, nil
}
return dmsgC, stop, nil
}


func loggingMiddleware() gin.HandlerFunc {
return func(c *gin.Context) {
start := time.Now()
Expand Down

0 comments on commit 586ee26

Please sign in to comment.