Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

panic: runtime error: invalid memory address or nil pointer dereference #17

Closed
deluan opened this issue Apr 14, 2020 · 9 comments
Closed
Labels

Comments

@deluan
Copy link

deluan commented Apr 14, 2020

I'm not totally sure when and why this happens, but one user of my app is reporting this error:

panic: runtime error: invalid memory address or nil pointer dereference

 -> github.com/djherbis/fscache.(*handleCounter).inc
 ->   /go/pkg/mod/github.com/djherbis/fscache@v0.10.0/fscache.go:327

    github.com/djherbis/fscache.(*FSCache).newFile
      /go/pkg/mod/github.com/djherbis/fscache@v0.10.0/fscache.go:227
    github.com/djherbis/fscache.(*FSCache).Get
      /go/pkg/mod/github.com/djherbis/fscache@v0.10.0/fscache.go:154
    github.com/deluan/navidrome/engine.(*cover).Get
      /github/workspace/engine/cover.go:50
    github.com/deluan/navidrome/server/subsonic.(*MediaRetrievalController).GetCoverArt
      /github/workspace/server/subsonic/media_retrieval.go:43
    github.com/deluan/navidrome/server/subsonic.H.func1
      /github/workspace/server/subsonic/api.go:138
    net/http.HandlerFunc.ServeHTTP
      /usr/local/go/src/net/http/server.go:2012
    github.com/go-chi/chi.(*ChainHandler).ServeHTTP
      /go/pkg/mod/github.com/go-chi/chi@v4.1.0+incompatible/chain.go:31
    github.com/go-chi/chi.(*Mux).routeHTTP
      /go/pkg/mod/github.com/go-chi/chi@v4.1.0+incompatible/mux.go:431
    net/http.HandlerFunc.ServeHTTP
      /usr/local/go/src/net/http/server.go:2012
    github.com/deluan/navidrome/server/subsonic.authenticate.func1.1
      /github/workspace/server/subsonic/middlewares.go:93
    net/http.HandlerFunc.ServeHTTP
      /usr/local/go/src/net/http/server.go:2012
    github.com/deluan/navidrome/server/subsonic.checkRequiredParameters.func1
      /github/workspace/server/subsonic/middlewares.go:63
    net/http.HandlerFunc.ServeHTTP
      /usr/local/go/src/net/http/server.go:2012
    github.com/deluan/navidrome/server/subsonic.postFormToQueryParams.func1
      /github/workspace/server/subsonic/middlewares.go:36
    net/http.HandlerFunc.ServeHTTP
      /usr/local/go/src/net/http/server.go:2012
    github.com/go-chi/chi.(*Mux).ServeHTTP
      /go/pkg/mod/github.com/go-chi/chi@v4.1.0+incompatible/mux.go:70
    github.com/deluan/navidrome/server/subsonic.(*Router).ServeHTTP
      /github/workspace/server/subsonic/api.go:48
    github.com/go-chi/chi.(*Mux).Mount.func1
      /go/pkg/mod/github.com/go-chi/chi@v4.1.0+incompatible/mux.go:298
    net/http.HandlerFunc.ServeHTTP
      /usr/local/go/src/net/http/server.go:2012
    github.com/deluan/navidrome/server.RequestLogger.func1
      /github/workspace/server/middlewares.go:21
    net/http.HandlerFunc.ServeHTTP

You can see in the original report that the key files are being created, but the cache files are created with size 0.

Maybe I'm using it wrongly?
https://github.com/deluan/navidrome/blob/master/engine/cover.go#L50

What other information can I give you to help pin this error down?

Original report: navidrome/navidrome#177

@djherbis
Copy link
Owner

Taking a look, this is very weird though, newFIle creates the cachedFile object (so it's not nil):

cf := &cachedFile{

And then calls inc, where it's failing on nil deref on:

atomic.AddInt64(&h.cnt, 1)

But that should not be nil because we're in newFile and literally just initialized it.

Is this reproducible with a specific setup? I'd like to do some debugging on it to figure out how this is possible.

@djherbis
Copy link
Owner

djherbis commented Apr 14, 2020

I suspect this is the problem:
https://stackoverflow.com/questions/28670232/atomic-addint64-causes-invalid-memory-address-or-nil-pointer-dereference

What's the specific Go build constraints that produces this situation? I'm guessing alignment is messed up. Though it's odd that it would be considering it's the first arg.

I suspect now maybe even though it's the first arg of handleCounter, because that is embedded in cachedFile that might mess up the alignment. This makes me sad because it breaks some of the 'easiness' of Go when a failure can cascade like this.

Related:
golang/go#599
golang/go#36606

@0xERR0R
Copy link

0xERR0R commented Apr 14, 2020

This error occurs on arm (raspberry pi 3)

@djherbis
Copy link
Owner

djherbis commented Apr 14, 2020

@0xERR0R Can you dump "go env" so I can match your build setup?
Go version would be helpful too.

@0xERR0R
Copy link

0xERR0R commented Apr 14, 2020

I used arm/v7 build from navidrome's release page. I guess, this will be built by goreleaser

@djherbis
Copy link
Owner

I gotta focus on my day job for awhile, but now that I've got enough details I work on fixing this tonight.

@0xERR0R
Copy link

0xERR0R commented Apr 14, 2020

If you want to perform some test -> please contact me, I would be glad to help

@djherbis
Copy link
Owner

@0xERR0R @deluan I fixed this by adjusting the atomic field alignment as suspected.

To verify, I pulled the old version on my pi, ran go test and got the same panic. I fixed the alignment and re-ran go test a few times, no panic.

Let me know if this fixes things for you too :)

@djherbis djherbis added the bug label Apr 15, 2020
@deluan
Copy link
Author

deluan commented Apr 15, 2020

Thanks for the quick turnaround on this issue!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants