mochi-mqtt / server

The fully compliant, embeddable high-performance Go MQTT v5 server for IoT, smarthome, and pubsub
MIT License
1.3k stars 223 forks source link

Got nil exception when inceptor publish #335

Closed lucasjinreal closed 1 year ago

lucasjinreal commented 1 year ago
time=2023-11-24T22:09:55.687+08:00 level=INFO msg="--> received from client" hook=my-custom-events-hook client=android_client_3B0iX_usrZK8kZTzEHC payload="{\"id\":\"6c8dc60f-f947-4ef5-b905-371556f444cd\",\"type\":\"ChatText\",\"fromId\":\"usrZK8kZTzEHC\",\"fromName\":\"小木木\",\"toId\":null,\"toName\":null,\"text\":\"人呢\",\"roomId\":\"0ad36464-2f35-4c17-b2d8-8e97e3521aee\",\"originality\":\"Original\",\"attachment\":null,\"thumbnail\":null,\"originalId\":null,\"originalMessage\":null,\"sendTime\":1700834995068,\"size\":null,\"mime\":null,\"longitude\":null,\"latitude\":null,\"additionalFields\":null}"
time=2023-11-24T22:09:55.687+08:00 level=INFO msg="--> published to client" hook=my-custom-events-hook client=android_client_3B0iX_usrZK8kZTzEHC payload="{\"id\":\"6c8dc60f-f947-4ef5-b905-371556f444cd\",\"type\":\"ChatText\",\"fromId\":\"usrZK8kZTzEHC\",\"fromName\":\"小木木\",\"toId\":null,\"toName\":null,\"text\":\"人呢\",\"roomId\":\"0ad36464-2f35-4c17-b2d8-8e97e3521aee\",\"originality\":\"Original\",\"attachment\":null,\"thumbnail\":null,\"originalId\":null,\"originalMessage\":null,\"sendTime\":1700834995068,\"size\":null,\"mime\":null,\"longitude\":null,\"latitude\":null,\"additionalFields\":null}"
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0xa64fa3]

goroutine 26 [running]:
chat/immqttv2.(*CustomHook).OnPublished(_, _, {{{{0x0, 0x0, 0x0}, {0x0, 0x0, 0x0}, {0x0, 0x0, ...}, ...}, ...}, ...})
        /root/Deploy/uranus/chat/immqttv2/hook.go:80 +0x143
github.com/mochi-mqtt/server/v2.(*Hooks).OnPublished(_, _, {{{{0x0, 0x0, 0x0}, {0x0, 0x0, 0x0}, {0x0, 0x0, ...}, ...}, ...}, ...})
        /root/goworkspace/pkg/mod/github.com/mochi-mqtt/server/v2@v2.4.1/hooks.go:419 +0x14c
github.com/mochi-mqtt/server/v2.(*Server).processPublish(_, _, {{{{0x0, 0x0, 0x0}, {0x0, 0x0, 0x0}, {0x0, 0x0, ...}, ...}, ...}, ...})
        /root/goworkspace/pkg/mod/github.com/mochi-mqtt/server/v2@v2.4.1/server.go:875 +0xb45
github.com/mochi-mqtt/server/v2.(*Server).processPacket(_, _, {{{{0x0, 0x0, 0x0}, {0x0, 0x0, 0x0}, {0x0, 0x0, ...}, ...}, ...}, ...})
        /root/goworkspace/pkg/mod/github.com/mochi-mqtt/server/v2@v2.4.1/server.go:597 +0x128
github.com/mochi-mqtt/server/v2.(*Server).receivePacket(_, _, {{{{0x0, 0x0, 0x0}, {0x0, 0x0, 0x0}, {0x0, 0x0, ...}, ...}, ...}, ...})
        /root/goworkspace/pkg/mod/github.com/mochi-mqtt/server/v2@v2.4.1/server.go:435 +0x58
github.com/mochi-mqtt/server/v2.(*Client).Read(0xc0004bc500, 0xc0003a17c0)
        /root/goworkspace/pkg/mod/github.com/mochi-mqtt/server/v2@v2.4.1/clients.go:367 +0x15d
github.com/mochi-mqtt/server/v2.(*Server).attachClient(0xc000508190, 0xc0004bc500, {0xb959bb, 0x2})
        /root/goworkspace/pkg/mod/github.com/mochi-mqtt/server/v2@v2.4.1/server.go:390 +0x665
github.com/mochi-mqtt/server/v2.(*Server).EstablishConnection(0xc000051000?, {0xb959bb, 0x2}, {0xcc2f30?, 0xc00004e010?})
        /root/goworkspace/pkg/mod/github.com/mochi-mqtt/server/v2@v2.4.1/server.go:327 +0x5e
github.com/mochi-mqtt/server/v2/listeners.(*TCP).Serve.func1()
        /root/goworkspace/pkg/mod/github.com/mochi-mqtt/server/v2@v2.4.1/listeners/tcp.go:84 +0x43
created by github.com/mochi-mqtt/server/v2/listeners.(*TCP).Serve in goroutine 24
        /root/goworkspace/pkg/mod/github.com/mochi-mqtt/server/v2@v2.4.1/listeners/tcp.go:83 +0x128

what's wrong here when I try to

func GlobalInterceptors(cl *mqtt.Client, pk packets.Packet, server *mqtt.Server) error {
    // version := client.Version()
    topic := pk.TopicName
    clientId := cl.ID
    payload := string(pk.Payload)

    log.Infof("[v2] Published msg, [Interceptors] topic: %s, clientId: %s, payload: %s ", topic, clientId, payload)

    if strings.HasPrefix(clientId, "supereme_") {
        // do nothing
        return nil
    }

Something like this?

werbenhu commented 1 year ago

首先,你这样描述问题我们看不出任何东西。 其次,这很明显是一个使用空指针的问题。你自己调试下看看哪个对象为空。 最后,我建议你先把go用熟了,把一些基础先打牢。

lucasjinreal commented 1 year ago

@werbenhu thanks for the suggestions, what's the difference of onPublish and onPublished? what should to override if I want inceptor the message (maybe change the content of payload)?

lucasjinreal commented 1 year ago

@werbenhu Hi, I debugged a little bit found the nil comes from the Server I use h.config.Server which suggested in example code. I want using Server to Publish in onPublish method.

截屏2023-11-25 10 47 01

can u help why it nil here? at 70 lines of hook.go

lucasjinreal commented 1 year ago

found the reason.