mirror of
https://github.com/SagerNet/sing-tun.git
synced 2025-04-03 03:47:39 +03:00
Fix darwin monitor socket leak
This commit is contained in:
parent
db70908d61
commit
7545dc2d56
1 changed files with 22 additions and 7 deletions
|
@ -17,14 +17,19 @@ import (
|
|||
)
|
||||
|
||||
type networkUpdateMonitor struct {
|
||||
access sync.Mutex
|
||||
callbacks list.List[NetworkUpdateCallback]
|
||||
routeSocket int
|
||||
logger logger.Logger
|
||||
access sync.Mutex
|
||||
callbacks list.List[NetworkUpdateCallback]
|
||||
routeSocketFile *os.File
|
||||
closeOnce sync.Once
|
||||
done chan struct{}
|
||||
logger logger.Logger
|
||||
}
|
||||
|
||||
func NewNetworkUpdateMonitor(logger logger.Logger) (NetworkUpdateMonitor, error) {
|
||||
return &networkUpdateMonitor{logger: logger}, nil
|
||||
return &networkUpdateMonitor{
|
||||
logger: logger,
|
||||
done: make(chan struct{}),
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (m *networkUpdateMonitor) Start() error {
|
||||
|
@ -34,6 +39,11 @@ func (m *networkUpdateMonitor) Start() error {
|
|||
|
||||
func (m *networkUpdateMonitor) loopUpdate() {
|
||||
for {
|
||||
select {
|
||||
case <-m.done:
|
||||
return
|
||||
case <-time.After(time.Second):
|
||||
}
|
||||
err := m.loopUpdate0()
|
||||
if err != nil {
|
||||
m.logger.Error("listen network update: ", err)
|
||||
|
@ -47,7 +57,9 @@ func (m *networkUpdateMonitor) loopUpdate0() error {
|
|||
if err != nil {
|
||||
return err
|
||||
}
|
||||
m.loopUpdate1(os.NewFile(uintptr(routeSocket), "route"))
|
||||
routeSocketFile := os.NewFile(uintptr(routeSocket), "route")
|
||||
m.routeSocketFile = routeSocketFile
|
||||
m.loopUpdate1(routeSocketFile)
|
||||
return nil
|
||||
}
|
||||
|
||||
|
@ -73,7 +85,10 @@ func (m *networkUpdateMonitor) loopUpdate1(routeSocketFile *os.File) {
|
|||
}
|
||||
|
||||
func (m *networkUpdateMonitor) Close() error {
|
||||
return unix.Close(m.routeSocket)
|
||||
m.closeOnce.Do(func() {
|
||||
close(m.done)
|
||||
})
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *defaultInterfaceMonitor) checkUpdate() error {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue