Skip to content

Commit

Permalink
Link state tracking tweaks and improved shutdown
Browse files Browse the repository at this point in the history
  • Loading branch information
neilalexander committed Aug 11, 2024
1 parent ef989be commit b1283e1
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 50 deletions.
66 changes: 29 additions & 37 deletions src/core/link.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"context"
"encoding/hex"
"errors"
"fmt"
"io"
"net"
Expand Down Expand Up @@ -40,7 +41,8 @@ type links struct {
ws *linkWS // WS interface support
wss *linkWSS // WSS interface support
// _links can only be modified safely from within the links actor
_links map[linkInfo]*link // *link is nil if connection in progress
_links map[linkInfo]*link // *link is nil if connection in progress
_listeners map[*Listener]context.CancelFunc
}

type linkProtocol interface {
Expand Down Expand Up @@ -85,13 +87,6 @@ func (l *Listener) Addr() net.Addr {
return l.listener.Addr()
}

func (l *Listener) Close() error {
l.Cancel()
err := l.listener.Close()
<-l.ctx.Done()
return err
}

func (l *links) init(c *Core) error {
l.core = c
l.tcp = l.newLinkTCP()
Expand All @@ -102,32 +97,18 @@ func (l *links) init(c *Core) error {
l.ws = l.newLinkWS()
l.wss = l.newLinkWSS()
l._links = make(map[linkInfo]*link)

var listeners []ListenAddress
phony.Block(c, func() {
listeners = make([]ListenAddress, 0, len(c.config._listeners))
for listener := range c.config._listeners {
listeners = append(listeners, listener)
}
})
l._listeners = make(map[*Listener]context.CancelFunc)

return nil
}

func (l *links) shutdown() {
phony.Block(l.tcp, func() {
for l := range l.tcp._listeners {
_ = l.Close()
}
})
phony.Block(l.tls, func() {
for l := range l.tls._listeners {
_ = l.Close()
phony.Block(l, func() {
for listener := range l._listeners {
_ = listener.listener.Close()
}
})
phony.Block(l.unix, func() {
for l := range l.unix._listeners {
_ = l.Close()
for _, link := range l._links {
_ = link._conn.Close()

This comment has been minimized.

Copy link
@EugeneMartein

EugeneMartein Aug 20, 2024

This line cause error use-after-free on Android app shutdown. The fix for this line should be to check for the existence of conn, as done in line 238.

image

}
})
}
Expand Down Expand Up @@ -457,11 +438,18 @@ func (l *links) listen(u *url.URL, sintf string) (*Listener, error) {
options.password = []byte(p)
}

phony.Block(l, func() {
l._listeners[li] = cancel
})

go func() {
l.core.log.Infof("%s listener started on %s", strings.ToUpper(u.Scheme), listener.Addr())
defer l.core.log.Infof("%s listener stopped on %s", strings.ToUpper(u.Scheme), listener.Addr())
l.core.log.Infof("%s listener started on %s", strings.ToUpper(u.Scheme), li.listener.Addr())
defer l.core.log.Infof("%s listener stopped on %s", strings.ToUpper(u.Scheme), li.listener.Addr())
defer phony.Block(l, func() {
delete(l._listeners, li)
})
for {
conn, err := listener.Accept()
conn, err := li.listener.Accept()
if err != nil {
return
}
Expand Down Expand Up @@ -517,25 +505,29 @@ func (l *links) listen(u *url.URL, sintf string) (*Listener, error) {
// Store the state of the link so that it can be queried later.
l._links[info] = state
})
defer phony.Block(l, func() {
if l._links[info] == state {
delete(l._links, info)
}
})
if lc == nil {
return
}

// Give the connection to the handler. The handler will block
// for the lifetime of the connection.
if err = l.handler(linkTypeIncoming, options, lc, nil); err != nil && err != io.EOF {
switch err = l.handler(linkTypeIncoming, options, lc, nil); {
case err == nil:
case errors.Is(err, io.EOF):
case errors.Is(err, net.ErrClosed):
default:
l.core.log.Debugf("Link %s error: %s\n", u.Host, err)
}

// The handler has stopped running so the connection is dead,
// try to close the underlying socket just in case and then
// drop the link state.
_ = lc.Close()
phony.Block(l, func() {
if l._links[info] == state {
delete(l._links, info)
}
})
}(conn)
}
}()
Expand Down
2 changes: 0 additions & 2 deletions src/core/link_tcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ type linkTCP struct {
phony.Inbox
*links
listenconfig *net.ListenConfig
_listeners map[*Listener]context.CancelFunc
}

func (l *links) newLinkTCP() *linkTCP {
Expand All @@ -24,7 +23,6 @@ func (l *links) newLinkTCP() *linkTCP {
listenconfig: &net.ListenConfig{
KeepAlive: -1,
},
_listeners: map[*Listener]context.CancelFunc{},
}
lt.listenconfig.Control = lt.tcpContext
return lt
Expand Down
10 changes: 4 additions & 6 deletions src/core/link_tls.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,9 @@ import (
type linkTLS struct {
phony.Inbox
*links
tcp *linkTCP
listener *net.ListenConfig
config *tls.Config
_listeners map[*Listener]context.CancelFunc
tcp *linkTCP
listener *net.ListenConfig
config *tls.Config
}

func (l *links) newLinkTLS(tcp *linkTCP) *linkTLS {
Expand All @@ -27,8 +26,7 @@ func (l *links) newLinkTLS(tcp *linkTCP) *linkTLS {
Control: tcp.tcpContext,
KeepAlive: -1,
},
config: l.core.config.tls.Clone(),
_listeners: map[*Listener]context.CancelFunc{},
config: l.core.config.tls.Clone(),
}
return lt
}
Expand Down
6 changes: 2 additions & 4 deletions src/core/link_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,8 @@ import (
type linkUNIX struct {
phony.Inbox
*links
dialer *net.Dialer
listener *net.ListenConfig
_listeners map[*Listener]context.CancelFunc
dialer *net.Dialer
listener *net.ListenConfig
}

func (l *links) newLinkUNIX() *linkUNIX {
Expand All @@ -27,7 +26,6 @@ func (l *links) newLinkUNIX() *linkUNIX {
listener: &net.ListenConfig{
KeepAlive: -1,
},
_listeners: map[*Listener]context.CancelFunc{},
}
return lt
}
Expand Down
6 changes: 5 additions & 1 deletion src/core/link_ws.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
type linkWS struct {
phony.Inbox
*links
listenconfig *net.ListenConfig
}

type linkWSConn struct {
Expand Down Expand Up @@ -78,6 +79,9 @@ func (s *wsServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
func (l *links) newLinkWS() *linkWS {
lt := &linkWS{
links: l,
listenconfig: &net.ListenConfig{
KeepAlive: -1,
},
}
return lt
}
Expand All @@ -95,7 +99,7 @@ func (l *linkWS) dial(ctx context.Context, url *url.URL, info linkInfo, options
}

func (l *linkWS) listen(ctx context.Context, url *url.URL, _ string) (net.Listener, error) {
nl, err := net.Listen("tcp", url.Host)
nl, err := l.listenconfig.Listen(ctx, "tcp", url.Host)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit b1283e1

Please sign in to comment.