Skip to content

Panic when trying to serve http with embedded assets #43761

@Cookie04DE

Description

@Cookie04DE

What version of Go are you using (go version)?

$ go version
go version go1.16beta1 linux/amd64

Does this issue reproduce with the latest release?

Yes.

What operating system and processor architecture are you using (go env)?

go env
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/conrad/.cache/go-build"
GOENV="/home/conrad/.config/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/media/conrad/Seagate/Programmieren/Go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/media/conrad/Seagate/Programmieren/Go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/media/conrad/Seagate/Installationen/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/media/conrad/Seagate/Installationen/go/pkg/tool/linux_amd64"
GCCGO="gccgo"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD=""
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build184147953=/tmp/go-build -gno-record-gcc-switches"

What did you do?

package main

import (
	"embed"
	"net/http"
)

//go: embed assets/*
var assets embed.FS

func main() {
	fs := http.FileServer(http.FS(assets))
	http.ListenAndServe(":48784", fs)
}

I created a folder called 'assets' containing only index.html with the following content:

<html>
    <head>
    </head>
    <body>
        <h>Hello World!</h>
    </body>
</html>

I then ran go1.16beta1 run .

What did you expect to see?

No panic and the static site being served on localhost:48784.

What did you see instead?

This panic:

2021/01/18 21:26:57 http: panic serving [::1]:55146: runtime error: invalid memory address or nil pointer dereference
goroutine 6 [running]:
net/http.(*conn).serve.func1(0xc00010e960)
        /home/conrad/sdk/go1.16beta1/src/net/http/server.go:1824 +0x153
panic(0x663180, 0x84ba30)
        /home/conrad/sdk/go1.16beta1/src/runtime/panic.go:971 +0x47a
embed.FS.readDir(0x0, 0x6a6905, 0x1, 0x84d580, 0x7f12ea36a3a0, 0x0)
        /home/conrad/sdk/go1.16beta1/src/embed/embed.go:266 +0x26
embed.FS.Open(0x0, 0x6a6905, 0x1, 0x8, 0x889f28, 0x44ed8c, 0x203000)
        /home/conrad/sdk/go1.16beta1/src/embed/embed.go:285 +0xce
net/http.ioFS.Open(0x6faa40, 0x0, 0xc000016384, 0x1, 0xc000168000, 0x7f12ea36a3a0, 0xc0001179c0, 0x40f27b)
        /home/conrad/sdk/go1.16beta1/src/net/http/fs.go:760 +0x68
net/http.serveFile(0x6fde30, 0xc0001480e0, 0xc000162000, 0x6fad20, 0xc000012c10, 0xc000016384, 0x1, 0xc000100e01)
        /home/conrad/sdk/go1.16beta1/src/net/http/fs.go:597 +0x84
net/http.(*fileHandler).ServeHTTP(0xc000012c20, 0x6fde30, 0xc0001480e0, 0xc000162000)
        /home/conrad/sdk/go1.16beta1/src/net/http/fs.go:848 +0x9c
net/http.serverHandler.ServeHTTP(0xc000148000, 0x6fde30, 0xc0001480e0, 0xc000162000)
        /home/conrad/sdk/go1.16beta1/src/net/http/server.go:2887 +0xa3
net/http.(*conn).serve(0xc00010e960, 0x6fe2a0, 0xc000028280)
        /home/conrad/sdk/go1.16beta1/src/net/http/server.go:1952 +0x8ed
created by net/http.(*Server).Serve
        /home/conrad/sdk/go1.16beta1/src/net/http/server.go:3013 +0x39b
2021/01/18 21:26:58 http: panic serving [::1]:55190: runtime error: invalid memory address or nil pointer dereference
goroutine 18 [running]:
net/http.(*conn).serve.func1(0xc000190000)
        /home/conrad/sdk/go1.16beta1/src/net/http/server.go:1824 +0x153
panic(0x663180, 0x84ba30)
        /home/conrad/sdk/go1.16beta1/src/runtime/panic.go:971 +0x47a
embed.FS.readDir(0x0, 0x6a6905, 0x1, 0x84d580, 0x7f12c1eee538, 0x0)
        /home/conrad/sdk/go1.16beta1/src/embed/embed.go:266 +0x26
embed.FS.Open(0x0, 0x6a6905, 0x1, 0x8, 0x889f28, 0x64, 0x203000)
        /home/conrad/sdk/go1.16beta1/src/embed/embed.go:285 +0xce
net/http.ioFS.Open(0x6faa40, 0x0, 0xc000194024, 0x1, 0x24, 0x7f12c1eee538, 0xc0000509c0, 0x40f27b)
        /home/conrad/sdk/go1.16beta1/src/net/http/fs.go:760 +0x68
net/http.serveFile(0x6fde30, 0xc0001b0000, 0xc00019e000, 0x6fad20, 0xc000012c10, 0xc000194024, 0x1, 0xc00018c001)
        /home/conrad/sdk/go1.16beta1/src/net/http/fs.go:597 +0x84
net/http.(*fileHandler).ServeHTTP(0xc000012c20, 0x6fde30, 0xc0001b0000, 0xc00019e000)
        /home/conrad/sdk/go1.16beta1/src/net/http/fs.go:848 +0x9c
net/http.serverHandler.ServeHTTP(0xc000148000, 0x6fde30, 0xc0001b0000, 0xc00019e000)
        /home/conrad/sdk/go1.16beta1/src/net/http/server.go:2887 +0xa3
net/http.(*conn).serve(0xc000190000, 0x6fe2a0, 0xc000196000)
        /home/conrad/sdk/go1.16beta1/src/net/http/server.go:1952 +0x8ed
created by net/http.(*Server).Serve
        /home/conrad/sdk/go1.16beta1/src/net/http/server.go:3013 +0x39b
2021/01/18 21:27:03 http: panic serving [::1]:55398: runtime error: invalid memory address or nil pointer dereference
goroutine 8 [running]:
net/http.(*conn).serve.func1(0xc00010ea00)
        /home/conrad/sdk/go1.16beta1/src/net/http/server.go:1824 +0x153
panic(0x663180, 0x84ba30)
        /home/conrad/sdk/go1.16beta1/src/runtime/panic.go:971 +0x47a
embed.FS.readDir(0x0, 0x6a6905, 0x1, 0x84d580, 0x0, 0x0)
        /home/conrad/sdk/go1.16beta1/src/embed/embed.go:266 +0x26
embed.FS.Open(0x0, 0x6a6905, 0x1, 0x8, 0x7f12ea36a538, 0x90, 0x203000)
        /home/conrad/sdk/go1.16beta1/src/embed/embed.go:285 +0xce
net/http.ioFS.Open(0x6faa40, 0x0, 0xc000016494, 0x1, 0x203000, 0xc0001641b8, 0xc0000559c0, 0x40f27b)
        /home/conrad/sdk/go1.16beta1/src/net/http/fs.go:760 +0x68
net/http.serveFile(0x6fde30, 0xc0001481c0, 0xc000162100, 0x6fad20, 0xc000012c10, 0xc000016494, 0x1, 0xc000101001)
        /home/conrad/sdk/go1.16beta1/src/net/http/fs.go:597 +0x84
net/http.(*fileHandler).ServeHTTP(0xc000012c20, 0x6fde30, 0xc0001481c0, 0xc000162100)
        /home/conrad/sdk/go1.16beta1/src/net/http/fs.go:848 +0x9c
net/http.serverHandler.ServeHTTP(0xc000148000, 0x6fde30, 0xc0001481c0, 0xc000162100)
        /home/conrad/sdk/go1.16beta1/src/net/http/server.go:2887 +0xa3
net/http.(*conn).serve(0xc00010ea00, 0x6fe2a0, 0xc000028400)
        /home/conrad/sdk/go1.16beta1/src/net/http/server.go:1952 +0x8ed
created by net/http.(*Server).Serve
        /home/conrad/sdk/go1.16beta1/src/net/http/server.go:3013 +0x39b

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions