olebedev / go-starter-kit

[abandoned] Golang isomorphic react/hot reloadable/redux/css-modules/SSR starter kit
Other
2.82k stars 359 forks source link

Segfault in go-duktape when onEnter() subscribes to state #66

Closed JulesAU closed 7 years ago

JulesAU commented 7 years ago

My use case is: I'm testing a method of pre-fetching API data server side.

Inside the component's onEnter() I dispatch an async action (using redux-thunk) to fetch it, subscribe to state so I can wait for it to complete before calling callback().

In doing so, I found duktape predictably segfaulting.

In order to reproduce it, I ported what I'm doing over to your Usage component example, and used setTimeout to fake the async nature of it. So, I modify the onEnter code to be like this:

  static onEnter({store, nextState, replaceState, callback}) {
      const conf = {this: "that"}
      let waitAttempts = 1;
      store.subscribe(function () {
        console.log(JSON.stringify(store.getState()));
        if (--waitAttempts == 0) callback();
      });

      store.dispatch(setConfig(conf));
      setTimeout(() => {
        store.dispatch(setConfig(conf));
      }, 0);
      setTimeout(() => {
        store.dispatch(setConfig(conf));
      }, 0);
  }

It seems to be that running the callback() whilst there are still outstanding async actions may be the cause; if you change waitAttempts to 3 it doesn't crash. If you run it as is, you get this:

{"config":{"this":"that"},"dialog":false,"articles":{}}
action @ 19:08:44.767 SET_CONFIG
%c prev state color: #9E9E9E; font-weight: bold [object Object]
%c action color: #03A9F4; font-weight: bold [object Object]
%c next state color: #4CAF50; font-weight: bold [object Object]
—— log end ——
fatal error: unexpected signal during runtime execution
[signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0x7fff824fe5d3]

runtime stack:
runtime.throw(0x4550fa9, 0x2a)
    /usr/local/go/src/runtime/panic.go:566 +0x95
runtime.sigpanic()
    /usr/local/go/src/runtime/sigpanic_unix.go:12 +0x2cc

goroutine 40 [syscall, locked to thread]:
runtime.cgocall(0x43f1800, 0xc4200a36c8, 0xc400000000)
    /usr/local/go/src/runtime/cgocall.go:131 +0x110 fp=0xc4200a3680 sp=0xc4200a3640
/workspace/go-react-test/vendor/gopkg.in/olebedev/go-duktape%2ev2._Cfunc_duk_get_prop(0x4f04f20, 0xfffffffe, 0x0)
    /workspace/go-react-test/vendor/gopkg.in/olebedev/go-duktape.v2/_obj/_cgo_gotypes.go:1534 +0x4d fp=0xc4200a36c8 sp=0xc4200a3680
/workspace/go-react-test/vendor/gopkg.in/olebedev/go-duktape%2ev2.(*Context).GetProp(0xc420146038, 0xfffffffffffffffe, 0x453d1db)
    /workspace/go-react-test/vendor/gopkg.in/olebedev/go-duktape.v2/api.go:514 +0x85 fp=0xc4200a3710 sp=0xc4200a36c8
/workspace/go-react-test/vendor/gopkg.in/olebedev/go-duktape%2ev2.(*Context).putTimer(0xc420146038, 0x3ff0000000000000)
    /workspace/go-react-test/vendor/gopkg.in/olebedev/go-duktape.v2/timers.go:133 +0x8c fp=0xc4200a3748 sp=0xc4200a3710
/workspace/go-react-test/vendor/gopkg.in/olebedev/go-duktape%2ev2.setTimeout.func1(0x3ff0000000000000, 0xc420146038, 0x3ff0000000000000)
    /workspace/go-react-test/vendor/gopkg.in/olebedev/go-duktape.v2/timers.go:53 +0xc7 fp=0xc4200a37a8 sp=0xc4200a3748
runtime.goexit()
    /usr/local/go/src/runtime/asm_amd64.s:2086 +0x1 fp=0xc4200a37b0 sp=0xc4200a37a8
created by /workspace/go-react-test/vendor/gopkg.in/olebedev/go-duktape%2ev2.setTimeout
    /workspace/go-react-test/vendor/gopkg.in/olebedev/go-duktape.v2/timers.go:58 +0xab

goroutine 1 [IO wait]:
net.runtime_pollWait(0x4c490d8, 0x72, 0x0)
    /usr/local/go/src/runtime/netpoll.go:160 +0x59
net.(*pollDesc).wait(0xc42015ffe0, 0x72, 0xc42004d518, 0xc4200120a8)
    /usr/local/go/src/net/fd_poll_runtime.go:73 +0x38
net.(*pollDesc).waitRead(0xc42015ffe0, 0x4768180, 0xc4200120a8)
    /usr/local/go/src/net/fd_poll_runtime.go:78 +0x34
net.(*netFD).accept(0xc42015ff80, 0x0, 0x47669c0, 0xc4205bc4c0)
    /usr/local/go/src/net/fd_unix.go:419 +0x238
net.(*TCPListener).accept(0xc42002c0e8, 0x60, 0x78, 0xc4200a4100)
    /usr/local/go/src/net/tcpsock_posix.go:132 +0x2e
net.(*TCPListener).AcceptTCP(0xc42002c0e8, 0x29e8d60800, 0xc42004d640, 0x4020a28)
    /usr/local/go/src/net/tcpsock.go:209 +0x49
/workspace/go-react-test/vendor/github.com/labstack/echo.tcpKeepAliveListener.Accept(0xc42002c0e8, 0x4060aa0, 0xc42004d680, 0xc42004d688, 0xc42004d678)
    /workspace/go-react-test/vendor/github.com/labstack/echo/echo.go:651 +0x2f
/workspace/go-react-test/vendor/github.com/labstack/echo.(*tcpKeepAliveListener).Accept(0xc42002c0f0, 0x45839a0, 0xc4200a4100, 0x476b8c0, 0xc4201d2a50)
    <autogenerated>:103 +0x52
net/http.(*Server).Serve(0xc4200b5200, 0x476ab00, 0xc42002c0f0, 0x0, 0x0)
    /usr/local/go/src/net/http/server.go:2273 +0x1ce
/workspace/go-react-test/vendor/github.com/labstack/echo.(*Echo).StartServer(0xc4200b96c0, 0xc4200b5200, 0xc420166b90, 0x5)
    /workspace/go-react-test/vendor/github.com/labstack/echo/echo.go:586 +0x506
/workspace/go-react-test/vendor/github.com/labstack/echo.(*Echo).Start(0xc4200b96c0, 0xc420166b90, 0x5, 0xc420166a08, 0x4)
    /workspace/go-react-test/vendor/github.com/labstack/echo/echo.go:536 +0x53
main.(*App).Run(0xc4200edd20)
    /workspace/go-react-test/server/app.go:144 +0xbe
main.RunServer(0xc4200bd2c0)
    /workspace/go-react-test/server/main.go:36 +0x4b
/workspace/go-react-test/vendor/github.com/codegangsta/cli.HandleAction(0x44a03e0, 0x4583780, 0xc4200bd2c0, 0xc420070600, 0x0)
    /workspace/go-react-test/vendor/github.com/codegangsta/cli/app.go:487 +0x7c
/workspace/go-react-test/vendor/github.com/codegangsta/cli.Command.Run(0x453ab6c, 0x3, 0x0, 0x0, 0x0, 0x0, 0x0, 0x454075e, 0xb, 0x0, ...)
    /workspace/go-react-test/vendor/github.com/codegangsta/cli/command.go:207 +0xb96
/workspace/go-react-test/vendor/github.com/codegangsta/cli.(*App).Run(0xc4200b9520, 0xc42000c240, 0x2, 0x2, 0x0, 0x0)
    /workspace/go-react-test/vendor/github.com/codegangsta/cli/app.go:250 +0x812
main.Run(0xc42000c240, 0x2, 0x2)
    /workspace/go-react-test/server/main.go:27 +0x10d
main.main()
    /workspace/go-react-test/server/main.go:10 +0x45

goroutine 17 [syscall, locked to thread]:
runtime.goexit()
    /usr/local/go/src/runtime/asm_amd64.s:2086 +0x1

goroutine 16 [runnable]:
/workspace/go-react-test/vendor/gopkg.in/olebedev/go-duktape%2ev2._Cfunc_GoString(0x4f045e0, 0xffffffff, 0x4f045e0)
    /workspace/go-react-test/vendor/gopkg.in/olebedev/go-duktape.v2/_obj/_cgo_gotypes.go:139
/workspace/go-react-test/vendor/gopkg.in/olebedev/go-duktape%2ev2.(*Context).SafeToString(0xc42002c070, 0xffffffffffffffff, 0x453b347, 0x4)
    /workspace/go-react-test/vendor/gopkg.in/olebedev/go-duktape.v2/api.go:1283 +0x98
/workspace/go-react-test/vendor/gopkg.in/olebedev/go-duktape%2ev2.(*Context).castStringToError(0xc42002c070, 0x1, 0x1, 0x0)
    /workspace/go-react-test/vendor/gopkg.in/olebedev/go-duktape.v2/api.go:902 +0x257
/workspace/go-react-test/vendor/gopkg.in/olebedev/go-duktape%2ev2.(*Context).PevalString(0xc42002c070, 0xc42012a0b0, 0xab, 0x0, 0x0)
    /workspace/go-react-test/vendor/gopkg.in/olebedev/go-duktape.v2/api.go:880 +0x103
main.(*ReactVM).Handle(0xc420013760, 0xc4201d6330, 0xc4200496c8)
    /workspace/go-react-test/server/react.go:198 +0x118
main.(*React).Handle(0xc4201d28a0, 0x4771ae0, 0xc4201e40e0, 0x0, 0x0)
    /workspace/go-react-test/server/react.go:69 +0x3a7
main.NewApp.func4.1(0x4771ae0, 0xc4201e40e0, 0x4, 0x44ee140)
    /workspace/go-react-test/server/app.go:131 +0x13e
main.NewApp.func3.1(0x4771ae0, 0xc4201e40e0, 0x3, 0x44be1a0)
    /workspace/go-react-test/server/app.go:94 +0x8d
main.NewApp.func2.1(0x4771ae0, 0xc4201e40e0, 0x4583420, 0x1000)
    /workspace/go-react-test/server/app.go:85 +0x88
/workspace/go-react-test/vendor/github.com/labstack/echo/middleware.RecoverWithConfig.func1.1(0x4771ae0, 0xc4201e40e0, 0x0, 0x0)
    /workspace/go-react-test/vendor/github.com/labstack/echo/middleware/recover.go:82 +0x124
/workspace/go-react-test/vendor/github.com/labstack/echo/middleware.LoggerWithConfig.func2.1(0x4771ae0, 0xc4201e40e0, 0x0, 0x0)
    /workspace/go-react-test/vendor/github.com/labstack/echo/middleware/logger.go:111 +0x186
/workspace/go-react-test/vendor/github.com/labstack/echo.(*Echo).ServeHTTP.func1(0x4771ae0, 0xc4201e40e0, 0xc4200b9718, 0x4533040)
    /workspace/go-react-test/vendor/github.com/labstack/echo/echo.go:519 +0x11e
/workspace/go-react-test/vendor/github.com/labstack/echo.(*Echo).ServeHTTP(0xc4200b96c0, 0x476aec0, 0xc420072270, 0xc4200f03c0)
    /workspace/go-react-test/vendor/github.com/labstack/echo/echo.go:528 +0x229
net/http.serverHandler.ServeHTTP(0xc4200b5200, 0x476aec0, 0xc420072270, 0xc4200f03c0)
    /usr/local/go/src/net/http/server.go:2202 +0x7d
net/http.(*conn).serve(0xc4200a4100, 0x476b800, 0xc4200f4340)
    /usr/local/go/src/net/http/server.go:1579 +0x4b7
created by net/http.(*Server).Serve
    /usr/local/go/src/net/http/server.go:2293 +0x44d

goroutine 41 [semacquire]:
sync.runtime_Semacquire(0xc4205bc604)
    /usr/local/go/src/runtime/sema.go:47 +0x30
sync.(*Mutex).Lock(0xc4205bc600)
    /usr/local/go/src/sync/mutex.go:85 +0xd0
/workspace/go-react-test/vendor/gopkg.in/olebedev/go-duktape%2ev2.setTimeout.func1(0x3ff0000000000000, 0xc420146040, 0x4000000000000000)
    /workspace/go-react-test/vendor/gopkg.in/olebedev/go-duktape.v2/timers.go:45 +0x72
created by /workspace/go-react-test/vendor/gopkg.in/olebedev/go-duktape%2ev2.setTimeout
    /workspace/go-react-test/vendor/gopkg.in/olebedev/go-duktape.v2/timers.go:58 +0xab

Apologies if this is not in the right spot as I'm not sure if it's a bug in my usage, go-starter-kit, the go-duktape binding, or maybe even duktape itself.

olebedev commented 7 years ago

@JulesAU, have a look at this #68