websocketd icon indicating copy to clipboard operation
websocketd copied to clipboard

runtime error: invalid memory address or nil pointer dereference

Open ctmnz opened this issue 6 years ago • 3 comments

I experimented using websocketd in combination with python script that reads 1.5M text file. Code:

#!/usr/bin/python

from sys import stdout
from time import sleep


f = open('mytextfile.txt', 'r')

while True:
    line = f.readline()
    if line is not None:
        l = len(line.decode("utf-8"))
        print("{}".format(line))
        sleeptime = l/15.0
        stdout.flush()
        sleep(sleeptime)
    else:
        break

The client/consumer connection :

<!DOCTYPE html>

<body>
  <script>
    // helper function: log message to screen
    function log(msg) {
      var x = document.createElement("P");
      var t = document.createTextNode(msg);
      x.appendChild(t)
      document.body.appendChild(x)
      window.scrollTo(0,document.body.scrollHeight);
    }
      var ws = new WebSocket('ws://192.168.0.107:8080/');
    ws.onopen = function() {
      log('Starting streaming....');
    };
    ws.onclose = function() {
      log('End of the stream');
    };
    ws.onmessage = function(event) {
      log(event.data);
    };
  </script>

</body>

I left my browser to 'play' the text while I am away. 10 hours later I have seen this error in my 'backend' console:

2019/01/29 18:27:05 http: panic serving 192.168.0.102:59831: runtime error: invalid memory address or nil pointer dereference
goroutine 12 [running]:
net/http.(*conn).serve.func1(0x9862600)
        /Users/abc/projects/websocketd/go-1.11.5/src/net/http/server.go:1746 +0x9f
panic(0x82a3220, 0x84e4a90)
        /Users/abc/projects/websocketd/go-1.11.5/src/runtime/panic.go:513 +0x16d
github.com/joewalnes/websocketd/libwebsocketd.(*WebSocketEndpoint).Send(0x980c3b0, 0x992c1dc, 0x0, 0x1, 0x1)
        /Users/abc/projects/websocketd/libwebsocketd/websocket_endpoint.go:52 +0x4e
github.com/joewalnes/websocketd/libwebsocketd.PipeEndpoints(0x83336e0, 0x98e65a0, 0x8333700, 0x980c3b0)
        /Users/abc/projects/websocketd/libwebsocketd/endpoint.go:24 +0x185
github.com/joewalnes/websocketd/libwebsocketd.(*WebsocketdHandler).accept(0x98544e0, 0x980a3c0, 0x98e63a0)
        /Users/abc/projects/websocketd/libwebsocketd/handler.go:81 +0x437
github.com/joewalnes/websocketd/libwebsocketd.(*WebsocketdServer).ServeHTTP(0x980c240, 0x83334a0, 0x984ac60, 0x9882600)
        /Users/abc/projects/websocketd/libwebsocketd/http.go:116 +0x107f
net/http.(*ServeMux).ServeHTTP(0x84eb140, 0x83334a0, 0x984ac60, 0x9882600)
        /Users/abc/projects/websocketd/go-1.11.5/src/net/http/server.go:2361 +0xf0
net/http.serverHandler.ServeHTTP(0x9882280, 0x83334a0, 0x984ac60, 0x9882600)
        /Users/abc/projects/websocketd/go-1.11.5/src/net/http/server.go:2741 +0x98
net/http.(*conn).serve(0x9862600, 0x83335c0, 0x98e6260)
        /Users/abc/projects/websocketd/go-1.11.5/src/net/http/server.go:1847 +0x5ea
created by net/http.(*Server).Serve
        /Users/abc/projects/websocketd/go-1.11.5/src/net/http/server.go:2851 +0x26a

The version of websocketd 0.3.1 (go1.11.5 linux-386) on Ubuntu 18.04.1

ctmnz avatar Jan 29 '19 19:01 ctmnz

Thanks! Any chance you can look at memory in system in the beginning and few hours into script running?

asergeyev avatar Jan 30 '19 03:01 asergeyev

I believe there is no need for any testing. websocket_endpoint.go:52 ---> w.Close() // could need error handling If we.ws.NextWriter(we.mtype) returns an error it's possible that w is nil and w.Close() will panic in this case.

erka avatar Feb 01 '19 18:02 erka

It seem to happen occasionally from random errors people posted here since your message. I'll patch. Thanks and sorry for very long reaction time :)

asergeyev avatar Mar 18 '21 16:03 asergeyev