英文:
Why networkmanager.WatchState panics?
问题
我需要在网络状态发生变化时监视网络变化,并在需要时设置一些路由规则(例如,如果接口重新启动,我需要重新设置IP路由表,因为源是动态的,无法硬编码)。为了实现这一点,我正在尝试使用networkmanager包,但WatchState出现了恐慌。有任何想法是为什么?
package main
import (
"flag"
"os"
"launchpad.net/ubuntu-push/bus"
"launchpad.net/ubuntu-push/bus/networkmanager"
"launchpad.net/ubuntu-push/logger"
)
func main() {
flag.Parse()
lg := logger.NewSimpleLogger(os.Stderr, "info")
endp := bus.SystemBus.Endpoint(networkmanager.BusAddress, lg)
nm := networkmanager.New(endp, lg)
watchState, cancel, err := nm.WatchState()
if err != nil {
log.Printf("error %v", err)
return
}
defer cancel.Cancel()
for state := range watchState {
log.Printf("state is %v", state)
}
}
恐慌跟踪:
panic: runtime error: invalid memory address or nil pointer dereference
[signal 0xb code=0x1 addr=0x0 pc=0x488788]
goroutine 1 [running]:
launchpad.net/go-dbus/v1.(*ObjectProxy).WatchSignal(0x0, 0x616cd0, 0x1e, 0x5f0af0, 0xc, 0x7f9733d0e478, 0x0, 0x0)
/root/go/src/launchpad.net/go-dbus/v1/dbus.go:97 +0x198
launchpad.net/ubuntu-push/bus.(*endpoint).WatchSignal(0xc20803e0c0, 0x5f0af0, 0xc, 0xc20800a0e0, 0xc20802a530, 0x0, 0x0, 0x0, 0x0)
/root/go/src/launchpad.net/ubuntu-push/bus/endpoint.go:133 +0x91
launchpad.net/ubuntu-push/bus/networkmanager.(*networkManager).WatchState(0xc20800a0c0, 0x5bce80, 0x0, 0x0, 0x0, 0x0)
/root/go/src/launchpad.net/ubuntu-push/bus/networkmanager/networkmanager.go:108 +0x1cf
main.main()
/root/go/src/github.com/vinkoo/net/main.go:16 +0x1f9
goroutine 2 [runnable]:
runtime.forcegchelper()
/usr/local/go/src/runtime/proc.go:90
runtime.goexit()
/usr/local/go/src/runtime/asm_amd64.s:2232 +0x1
goroutine 3 [runnable]:
runtime.bgsweep()
/usr/local/go/src/runtime/mgc0.go:82
runtime.goexit()
/usr/local/go/src/runtime/asm_amd64.s:2232 +0x1
goroutine 4 [runnable]:
runtime.runfinq()
/usr/local/go/src/runtime/malloc.go:712
runtime.goexit()
/usr/local/go/src/runtime/asm_amd64.s:2232 +0x1
exit status 2
编辑: 程序似乎在networkmanager的任何方法上都会出现恐慌。GetState的恐慌如下所示。
goroutine 1 [running]:
launchpad.net/go-dbus/v1.(*ObjectProxy).Call(0x0, 0x616c90, 0x1f, 0x5e26d0, 0x3, 0xc20801d5a0, 0x2, 0x2, 0x7f13a95d0b50, 0x0, ...)
/root/go/src/launchpad.net/go-dbus/v1/dbus.go:77 +0x1d1
launchpad.net/ubuntu-push/bus.(*endpoint).GetProperty(0xc20803e0c0, 0x5eac50, 0x5, 0x0, 0x0, 0x0, 0x0)
/root/go/src/launchpad.net/ubuntu-push/bus/endpoint.go:169 +0x1cf
launchpad.net/ubuntu-push/bus/networkmanager.(*networkManager).GetState(0xc20800a0c0, 0x5bce80)
/root/go/src/launchpad.net/ubuntu-push/bus/networkmanager/networkmanager.go:79 +0x62
main.main()
英文:
I need to watch network changes and set some routing rules whenever the network status changes(e.g. if an interface is rebooted I need to set the ip routing tables again b/c the src is dynamic so I can't hardcode it).
To achieve this I'm trying to use networkmanager package but WatchState panics. Any idea why?
package main
import(
"flag"
"os"
"launchpad.net/ubuntu-push/bus"
"launchpad.net/ubuntu-push/bus/networkmanager"
"launchpad.net/ubuntu-push/logger"
)
func main(){
flag.Parse()
lg := logger.NewSimpleLogger(os.Stderr, "info")
endp := bus.SystemBus.Endpoint(networkmanager.BusAddress, lg)
nm := networkmanager.New(endp, lg)
watchState, cancel, err := nm.WatchState()
if err !=nil{
log.Printf("error %v", err)
return
}
defer cancel.Cancel()
for state := range watchState{
log.Printf.("state is %v", state)
}
}
Panic trace
panic: runtime error: invalid memory address or nil pointer dereference
[signal 0xb code=0x1 addr=0x0 pc=0x488788]
goroutine 1 [running]:
launchpad.net/go-dbus/v1.(*ObjectProxy).WatchSignal(0x0, 0x616cd0, 0x1e, 0x5f0af0, 0xc, 0x7f9733d0e478, 0x0, 0x0)
/root/go/src/launchpad.net/go-dbus/v1/dbus.go:97 +0x198
launchpad.net/ubuntu-push/bus.(*endpoint).WatchSignal(0xc20803e0c0, 0x5f0af0, 0xc, 0xc20800a0e0, 0xc20802a530, 0x0, 0x0, 0x0, 0x0)
/root/go/src/launchpad.net/ubuntu-push/bus/endpoint.go:133 +0x91
launchpad.net/ubuntu-push/bus/networkmanager.(*networkManager).WatchState(0xc20800a0c0, 0x5bce80, 0x0, 0x0, 0x0, 0x0)
/root/go/src/launchpad.net/ubuntu-push/bus/networkmanager/networkmanager.go:108 +0x1cf
main.main()
/root/go/src/github.com/vinkoo/net/main.go:16 +0x1f9
goroutine 2 [runnable]:
runtime.forcegchelper()
/usr/local/go/src/runtime/proc.go:90
runtime.goexit()
/usr/local/go/src/runtime/asm_amd64.s:2232 +0x1
goroutine 3 [runnable]:
runtime.bgsweep()
/usr/local/go/src/runtime/mgc0.go:82
runtime.goexit()
/usr/local/go/src/runtime/asm_amd64.s:2232 +0x1
goroutine 4 [runnable]:
runtime.runfinq()
/usr/local/go/src/runtime/malloc.go:712
runtime.goexit()
/usr/local/go/src/runtime/asm_amd64.s:2232 +0x1
exit status 2
Edit: It seems the program panics on any method from the networkmanager. GetState panics as follows.
goroutine 1 [running]:
launchpad.net/go-dbus/v1.(*ObjectProxy).Call(0x0, 0x616c90, 0x1f, 0x5e26d0, 0x3, 0xc20801d5a0, 0x2, 0x2, 0x7f13a95d0b50, 0x0, ...)
/root/go/src/launchpad.net/go-dbus/v1/dbus.go:77 +0x1d1
launchpad.net/ubuntu-push/bus.(*endpoint).GetProperty(0xc20803e0c0, 0x5eac50, 0x5, 0x0, 0x0, 0x0, 0x0)
/root/go/src/launchpad.net/ubuntu-push/bus/endpoint.go:169 +0x1cf
launchpad.net/ubuntu-push/bus/networkmanager.(*networkManager).GetState(0xc20800a0c0, 0x5bce80)
/root/go/src/launchpad.net/ubuntu-push/bus/networkmanager/networkmanager.go:79 +0x62
main.main()
答案1
得分: 0
我自己没有使用过这个包,但是从检查源代码(http://bazaar.launchpad.net/+branch/ubuntu-push/view/head:/bus/endpoint.go)来看,似乎你应该在调用endp.Dial
之前调用networkmanager.New
,否则endpoint.proxy
将保持未初始化状态。
endp := bus.SystemBus.Endpoint(networkmanager.BusAddress, lg)
err := endp.Dial()
if err != nil{
log.Printf("dial error %v", err)
return
}
defer endp.Close()
nm := networkmanager.New(endp, lg)
英文:
I haven't used the package myself, but from inspecting the source code, it seems like you should call endp.Dial
before networkmanager.New
because otherwise, endpoint.proxy
stays uninitialised.
endp := bus.SystemBus.Endpoint(networkmanager.BusAddress, lg)
err := endp.Dial()
if err != nil{
log.Printf("dial error %v", err)
return
}
defer endp.Close()
nm := networkmanager.New(endp, lg)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论