登录
首页 >  Golang >  Go问答

Golang 运行 main.go 时频繁崩溃,出现 0xc0000005 异常

来源:stackoverflow

时间:2024-03-07 18:09:24 344浏览 收藏

Golang小白一枚,正在不断学习积累知识,现将学习到的知识记录一下,也是将我的所得分享给大家!而今天这篇文章《Golang 运行 main.go 时频繁崩溃,出现 0xc0000005 异常》带大家来了解一下##content_title##,希望对大家的知识积累有所帮助,从而弥补自己的不足,助力实战开发!


问题内容

我是 golang 新手,已经使用 gin 和 gorm 开发了一个基本的 rest api。 api 工作正常,代码中没有错误。 main.go 文件如下:

package main
import (
    "fmt"
    "os"

    "github.com/gin-gonic/gin"
    "github.com/joho/godotenv"
    "github.com/usman2661/todo-crud-go/controllers"
    "github.com/usman2661/todo-crud-go/models"

    // "github.com/gin-contrib/cors"
    "net/http"
)

func godotenvvariable(key string) string {

    // load .env file
    err := godotenv.load(".env")

    if err != nil {
        fmt.println("error loading .env file")
    }

    return os.getenv(key)
}

// cors middleware
func cors(c *gin.context) {

    // first, we add the headers with need to enable cors
    // make sure to adjust these headers to your needs
    c.header("access-control-allow-origin", "http://localhost:4200")
    c.header("access-control-allow-methods", "*")
    c.header("access-control-allow-headers", "*")
    c.header("content-type", "application/json")

    // second, we handle the options problem
    if c.request.method != "options" {

        c.next()

    } else {

        // everytime we receive an options request,
        // we just return an http 200 status code
        // like this, angular can now do the real
        // request using any other method than options
        c.abortwithstatus(http.statusok)
    }
}

func main() {

    router := gin.default()

    host := godotenvvariable("host")
    port := godotenvvariable("dbport")
    user := godotenvvariable("user")
    dbname := godotenvvariable("dbname")
    password := godotenvvariable("password")
    sslmode := godotenvvariable("sslmode")

    models.connectdatabase(host, port, user, dbname, password, sslmode)

    router.use(cors)

    router.get("/todos", controllers.gettodos)          //  get all the todos
    router.post("/todos", controllers.createtodo)       // create a new todo
    router.get("/todos/:id", controllers.gettodo)       // fetch a single todo using id
    router.put("/todos/:id", controllers.updatetodo)    // update and existing todo id has to be passed
    router.delete("/todos/:id", controllers.deletetodo) // delete a todo using the id

    router.get("/catagory/:username", controllers.getcatagories) //  get the different catagories

    router.run()
}

但是,偶尔它会在运行时不断崩溃,之后它就不再工作,我必须重新启动我的机器来修复它然后它才能工作。它抛出以下异常

exception 0xc0000005 0x0 0x7fff91b70fff 0x1a34e3f0000
pc=0x1a34e3f0000

runtime: unknown pc 0x1a34e3f0000

我在网上进行了大量研究,但找不到解决此问题的答案。是否有任何解决方案可以永久解决此问题而不是重新启动电脑。

完全异常:

Exception 0xc0000005 0x0 0x7fff91b72fff 0x16f74c80000
PC=0x16f74c80000

syscall.Syscall6(0x7fff91794c70, 0x4, 0xc0002545c0, 0x0, 0xc000485f18, 0xc000485e90, 0x0, 0x0, 0x0, 0x0, ...)
        c:/go/src/runtime/syscall_windows.go:201 +0xf2
syscall.GetAddrInfoW(0xc0002545c0, 0x0, 0xc000485f18, 0xc000485e90, 0xa, 0x0)
        c:/go/src/syscall/zsyscall_windows.go:1718 +0xe5
net.(*Resolver).lookupIP.func1(0x0, 0x0, 0x0, 0x0, 0x0)
        c:/go/src/net/lookup_windows.go:109 +0x259
net.(*Resolver).lookupIP.func2(0xc00046afa0, 0xc0003f7f80)
        c:/go/src/net/lookup_windows.go:146 +0x32
created by net.(*Resolver).lookupIP
        c:/go/src/net/lookup_windows.go:145 +0x41b

goroutine 1 [select]:
net.(*Resolver).lookupIPAddr(0x168f080, 0x13839c0, 0xc0000200e0, 0x12b530e, 0x3, 0xc0004509a0, 0x9, 0x1538, 0x0, 0x0, ...)
        c:/go/src/net/lookup.go:299 +0x685
net.(*Resolver).internetAddrList(0x168f080, 0x13839c0, 0xc0000200e0, 0x12b530e, 0x3, 0xc0004509a0, 0xe, 0x0, 0x0, 0x0, ...)
        c:/go/src/net/ipsock.go:280 +0x4d4
net.(*Resolver).resolveAddrList(0x168f080, 0x13839c0, 0xc0000200e0, 0x12b5910, 0x4, 0x12b530e, 0x3, 0xc0004509a0, 0xe, 0x0, ...)
        c:/go/src/net/dial.go:221 +0x49d
net.(*Dialer).DialContext(0xc00029f6e8, 0x13839c0, 0xc0000200e0, 0x12b530e, 0x3, 0xc0004509a0, 0xe, 0x0, 0x0, 0x0, ...)
        c:/go/src/net/dial.go:403 +0x23c
github.com/lib/pq.defaultDialer.DialContext(...)
        C:/Users/Usman Ali/go/pkg/mod/github.com/lib/[email protected]/conn.go:112
github.com/lib/pq.dial(0x13839c0, 0xc0000200e0, 0x137ed00, 0xc0003f7e60, 0xc00045e240, 0x0, 0x0, 0x0, 0x0)
        C:/Users/Usman Ali/go/pkg/mod/github.com/lib/[email protected]/conn.go:361 +0x52f
github.com/lib/pq.(*Connector).open(0xc00046af60, 0x13839c0, 0xc0000200e0, 0xc0000c5340, 0x0, 0x0)
        C:/Users/Usman Ali/go/pkg/mod/github.com/lib/[email protected]/conn.go:297 +0x193
github.com/lib/pq.DialOpen(0x137ed00, 0xc0003f7e60, 0xc000444600, 0x5f, 0x30001, 0x400, 0x255554aaaa, 0x39)
        C:/Users/Usman Ali/go/pkg/mod/github.com/lib/[email protected]/conn.go:275 +0x99
github.com/lib/pq.Open(...)
        C:/Users/Usman Ali/go/pkg/mod/github.com/lib/[email protected]/conn.go:265
github.com/lib/pq.(*Driver).Open(0x16c3050, 0xc000444600, 0x5f, 0x0, 0x0, 0x0, 0x0)
        C:/Users/Usman Ali/go/pkg/mod/github.com/lib/[email protected]/conn.go:48 +0xa5
database/sql.dsnConnector.Connect(...)
        c:/go/src/database/sql/sql.go:707
database/sql.(*DB).conn(0xc000380ea0, 0x13839c0, 0xc0000200e0, 0xc00038d401, 0x167c770, 0x10, 0xa)
        c:/go/src/database/sql/sql.go:1294 +0x214
database/sql.(*DB).PingContext(0xc000380ea0, 0x13839c0, 0xc0000200e0, 0xc00038d490, 0x138d920)
        c:/go/src/database/sql/sql.go:799 +0x9f
database/sql.(*DB).Ping(...)
        c:/go/src/database/sql/sql.go:817
github.com/jinzhu/gorm.Open(0x12b9e13, 0x8, 0xc00029fdd8, 0x1, 0x1, 0x5f, 0xc000450750, 0xc00029fe00)
        C:/Users/Usman Ali/go/pkg/mod/github.com/jinzhu/[email protected]/main.go:97 +0x28a
github.com/usman2661/Todo-Crud-Go/models.ConnectDataBase(0xc000365370, 0x9, 0xc000365de0, 0x4, 0xc0003fc840, 0x8, 0xc0003fd2a0, 0xb, 0xc0003fdcf0, 0xc, ...)       
        C:/Users/Usman Ali/go/src/github.com/usman2661/Golang-CRUD-API/models/setup.go:18 +0x268
main.main()
        C:/Users/Usman Ali/go/src/github.com/usman2661/Golang-CRUD-API/main.go:64 +0x1e6

goroutine 18 [select]:
database/sql.(*DB).connectionOpener(0xc000380ea0, 0x1383980, 0xc00043eb80)
        c:/go/src/database/sql/sql.go:1126 +0xf5
created by database/sql.OpenDB
        c:/go/src/database/sql/sql.go:740 +0x132

goroutine 19 [select]:
net.(*Resolver).lookupIP(0x168f080, 0x1383980, 0xc00043ebc0, 0x12b530e, 0x3, 0xc0004509a0, 0x9, 0x0, 0x0, 0x0, ...)
        c:/go/src/net/lookup_windows.go:151 +0x1b9
net.glob..func1(0x1383980, 0xc00043ebc0, 0xc00038d4a0, 0x12b530e, 0x3, 0xc0004509a0, 0x9, 0x0, 0x0, 0x0, ...)
        c:/go/src/net/hook.go:23 +0x79
net.(*Resolver).lookupIPAddr.func1(0x0, 0x0, 0x0, 0x0)
        c:/go/src/net/lookup.go:293 +0xc2
internal/singleflight.(*Group).doCall(0x168f090, 0xc00041d6d0, 0xc0004509b0, 0xd, 0xc00043ec00)
        c:/go/src/internal/singleflight/singleflight.go:95 +0x35
created by internal/singleflight.(*Group).DoChan
        c:/go/src/internal/singleflight/singleflight.go:88 +0x2cc
rax     0x7fff8e09d84a
rbx     0x7fff8e09d848
rcx     0x77
rdi     0xffffffffffbadd11
rsi     0x0
rbp     0x16f4dec2f90
rsp     0x505affeaf0
r8      0x94b
r9      0x94b
r10     0x94b
r11     0x94b
r12     0x7fff91980000
r13     [gin] Building...
[gin] Build finished
[GIN-debug] [WARNING] Creating an Engine instance with the Logger and Recovery middleware already attached.

[GIN-debug] [WARNING] Running in "debug" mode. Switch to "release" mode in production.
 - using env:   export GIN_MODE=release
 - using code:  gin.SetMode(gin.ReleaseMode)


Connected to the Database!!!
[GIN-debug] GET    /todos                    --> github.com/usman2661/Todo-Crud-Go/controllers.GetTodos (4 handlers)
[GIN-debug] POST   /todos                    --> github.com/usman2661/Todo-Crud-Go/controllers.CreateTodo (4 handlers)
[GIN-debug] GET    /todos/:id                --> github.com/usman2661/Todo-Crud-Go/controllers.GetTodo (4 handlers)

Usman Ali@DRAX-LAP-UA MINGW64 ~/go/src/github.com/usman2661/Golang-CRUD-API (master)
$ gin --appPort 8080 --all -i run main.go
[gin] Listening on port 3000
[gin] Building...
[gin] Build finished
[GIN-debug] [WARNING] Creating an Engine instance with the Logger and Recovery middleware already attached.

[GIN-debug] [WARNING] Running in "debug" mode. Switch to "release" mode in production.
 - using env:   export GIN_MODE=release
 - using code:  gin.SetMode(gin.ReleaseMode)

Exception 0xc0000005 0x0 0x7fff91b72fff 0x262e38e0000
PC=0x262e38e0000

syscall.Syscall6(0x7fff91794c70, 0x4, 0xc000444e20, 0x0, 0xc00048bf18, 0xc00048be90, 0x0, 0x0, 0x0, 0x0, ...)
        c:/go/src/runtime/syscall_windows.go:201 +0xf2
syscall.GetAddrInfoW(0xc000444e20, 0x0, 0xc00048bf18, 0xc00048be90, 0xa, 0x0)
        c:/go/src/syscall/zsyscall_windows.go:1718 +0xe5
net.(*Resolver).lookupIP.func1(0x0, 0x0, 0x0, 0x0, 0x0)
        c:/go/src/net/lookup_windows.go:109 +0x259
net.(*Resolver).lookupIP.func2(0xc00045dbe0, 0xc00041f260)
        c:/go/src/net/lookup_windows.go:146 +0x32
created by net.(*Resolver).lookupIP
        c:/go/src/net/lookup_windows.go:145 +0x41b

goroutine 1 [select]:
net.(*Resolver).lookupIPAddr(0x168f080, 0x13839c0, 0xc0000a2078, 0x12b530e, 0x3, 0xc000454ad0, 0x9, 0x1538, 0x0, 0x0, ...)
        c:/go/src/net/lookup.go:299 +0x685
net.(*Resolver).internetAddrList(0x168f080, 0x13839c0, 0xc0000a2078, 0x12b530e, 0x3, 0xc000454ad0, 0xe, 0x0, 0x0, 0x0, ...)
        c:/go/src/net/ipsock.go:280 +0x4d4
net.(*Resolver).resolveAddrList(0x168f080, 0x13839c0, 0xc0000a2078, 0x12b5910, 0x4, 0x12b530e, 0x3, 0xc000454ad0, 0xe, 0x0, ...)
        c:/go/src/net/dial.go:221 +0x49d
net.(*Dialer).DialContext(0xc00021f6e8, 0x13839c0, 0xc0000a2078, 0x12b530e, 0x3, 0xc000454ad0, 0xe, 0x0, 0x0, 0x0, ...)
        c:/go/src/net/dial.go:403 +0x23c
github.com/lib/pq.defaultDialer.DialContext(...)
        C:/Users/Usman Ali/go/pkg/mod/github.com/lib/[email protected]/conn.go:112
github.com/lib/pq.dial(0x13839c0, 0xc0000a2078, 0x137ed00, 0xc00041f140, 0xc000425650, 0x0, 0x0, 0x0, 0x0)
        C:/Users/Usman Ali/go/pkg/mod/github.com/lib/[email protected]/conn.go:361 +0x52f
github.com/lib/pq.(*Connector).open(0xc00045dba0, 0x13839c0, 0xc0000a2078, 0xc0003122c0, 0x0, 0x0)
        C:/Users/Usman Ali/go/pkg/mod/github.com/lib/[email protected]/conn.go:297 +0x193
github.com/lib/pq.DialOpen(0x137ed00, 0xc00041f140, 0xc00042cae0, 0x5f, 0x30001, 0x262bcd6edb8, 0x255554aaaa, 0x39)
        C:/Users/Usman Ali/go/pkg/mod/github.com/lib/[email protected]/conn.go:275 +0x99
github.com/lib/pq.Open(...)
        C:/Users/Usman Ali/go/pkg/mod/github.com/lib/[email protected]/conn.go:265
github.com/lib/pq.(*Driver).Open(0x16c3050, 0xc00042cae0, 0x5f, 0x0, 0x0, 0x0, 0x0)
        C:/Users/Usman Ali/go/pkg/mod/github.com/lib/[email protected]/conn.go:48 +0xa5
database/sql.dsnConnector.Connect(...)
        c:/go/src/database/sql/sql.go:707
database/sql.(*DB).conn(0xc00038b380, 0x13839c0, 0xc0000a2078, 0xc000395901, 0x167c770, 0x10, 0xa)
        c:/go/src/database/sql/sql.go:1294 +0x214
database/sql.(*DB).PingContext(0xc00038b380, 0x13839c0, 0xc0000a2078, 0xc000395940, 0x138d920)
        c:/go/src/database/sql/sql.go:799 +0x9f
database/sql.(*DB).Ping(...)
        c:/go/src/database/sql/sql.go:817
github.com/jinzhu/gorm.Open(0x12b9e13, 0x8, 0xc00021fdd8, 0x1, 0x1, 0x5f, 0xc000454880, 0xc00021fe00)
        C:/Users/Usman Ali/go/pkg/mod/github.com/jinzhu/[email protected]/main.go:97 +0x28a
github.com/usman2661/Todo-Crud-Go/models.ConnectDataBase(0xc0003774a0, 0x9, 0xc000377f10, 0x4, 0xc0003f0970, 0x8, 0xc0003f13d0, 0xb, 0xc0003f1e20, 0xc, ...)       
        C:/Users/Usman Ali/go/src/github.com/usman2661/Golang-CRUD-API/models/setup.go:18 +0x268
main.main()
        C:/Users/Usman Ali/go/src/github.com/usman2661/Golang-CRUD-API/main.go:64 +0x1e6

goroutine 20 [select]:
database/sql.(*DB).connectionOpener(0xc00038b380, 0x1383980, 0xc000415c80)
        c:/go/src/database/sql/sql.go:1126 +0xf5
created by database/sql.OpenDB
        c:/go/src/database/sql/sql.go:740 +0x132

goroutine 21 [select]:
net.(*Resolver).lookupIP(0x168f080, 0x1383980, 0xc000415cc0, 0x12b530e, 0x3, 0xc000454ad0, 0x9, 0x0, 0x0, 0x0, ...)
        c:/go/src/net/lookup_windows.go:151 +0x1b9
net.glob..func1(0x1383980, 0xc000415cc0, 0xc000395950, 0x12b530e, 0x3, 0xc000454ad0, 0x9, 0x0, 0x0, 0x0, ...)
        c:/go/src/net/hook.go:23 +0x79
net.(*Resolver).lookupIPAddr.func1(0x0, 0x0, 0x0, 0x0)
        c:/go/src/net/lookup.go:293 +0xc2
internal/singleflight.(*Group).doCall(0x168f090, 0xc000458640, 0xc000454ae0, 0xd, 0xc000415d00)
        c:/go/src/internal/singleflight/singleflight.go:95 +0x35
created by internal/singleflight.(*Group).DoChan
        c:/go/src/internal/singleflight/singleflight.go:88 +0x2cc
rax     0x7fff8e09d84a
rbx     0x7fff8e09d848
rcx     0x77
rdi     0xffffffffffbadd11
rsi     0x0
rbp     0x262bcb03f40
rsp     0xe0199fe670
r8      0x94b
r9      0x94b
r10     0x94b
r11     0x94b
r12     0x7fff91980000
r13     0x0
r14     0x7fff8e09d84a
r15     0xc000007a
rip     0x262e38e0000
rflags  0x10202
cs      0x33[gin] Building...
[gin] Build finished
Exception 0xc0000005 0x0 0x7fff91b70fff 0x1c8e0110000
PC=0x1c8e0110000

runtime: unknown pc 0x1c8e0110000
stack: frame={sp:0x455b3ff0c0, fp:0x0} stack=[0x0,0x455b3ff920)
000000455b3fefc0:  000001c8dffd8dc0  000000455b3ff030
000000455b3fefd0:  0000000000000000  000000455b3ff0c8
000000455b3fefe0:  000001c8dffc7820  000000455b3ff020
000000455b3feff0:  0000000000000000  00007fff8e850108
000000455b3ff000:  0000000000000000  000001c8dffc4570
000000455b3ff010:  000000455b3ff158  00007fff8ef2a8a1
000000455b3ff020:  00007fff8ef2a89b  00007fff9199ba6f
000000455b3ff030:  00007fff919800d8  000001c8dfff1b60
000000455b3ff040:  0000000000000005  000000455b3ff090
000000455b3ff050:  00007fff8ef28cd0  00007fff919b1810
000000455b3ff060:  000001c8dffc4570  0000000000000000
000000455b3ff070:  000001c800050005  00007fff8ef2a89b
000000455b3ff080:  000000000000001b  0000000000000000
000000455b3ff090:  0000000000000000  00007fff919800d8
000000455b3ff0a0:  0000000000000000  00007fff8ecd0000
000000455b3ff0b0:  0000000000000001  00007fff9199c734
000000455b3ff0c0: <000001c800000001  0000000000000000
000000455b3ff0d0:  00007fff0000ebb0  000000455b3ff1c8
000000455b3ff0e0:  000001c8dffc78f0  000000455b3ff148
000000455b3ff0f0:  0000000000250024  00007fff8e86b816
000000455b3ff100:  000001c8dffd7f80  00007fff91acc500
000000455b3ff110:  000001c8dffc4570  00007fff91acea5c
000000455b3ff120:  000001c80000094c  00007fff8e84b2d8
000000455b3ff130:  00007fff8e849138  00007fff91acc528
000000455b3ff140:  00007fff91adec6a  00007fff8e840000
000000455b3ff150:  00007fff91ad0f8c  00007fff91a00d80
000000455b3ff160:  000000451c1dc789  000001c8dffd8e18
000000455b3ff170:  000000455b3ff4d0  00000000c0000135
000000455b3ff180:  0000000000000000  0000000000000040
000000455b3ff190:  0000000000000004  00007fff91ae52f0
000000455b3ff1a0:  0000000000000001  000000455b3ff300
000000455b3ff1b0:  000001c8dfff1b60  00007fff919ae2a8
runtime: unknown pc 0x1c8e0110000
stack: frame={sp:0x455b3ff0c0, fp:0x0} stack=[0x0,0x455b3ff920)
000000455b3fefc0:  000001c8dffd8dc0  000000455b3ff030
000000455b3fefd0:  0000000000000000  000000455b3ff0c8
000000455b3fefe0:  000001c8dffc7820  000000455b3ff020
000000455b3feff0:  0000000000000000  00007fff8e850108
000000455b3ff000:  0000000000000000  000001c8dffc4570
000000455b3ff010:  000000455b3ff158  00007fff8ef2a8a1
000000455b3ff020:  00007fff8ef2a89b  00007fff9199ba6f
000000455b3ff030:  00007fff919800d8  000001c8dfff1b60
000000455b3ff040:  0000000000000005  000000455b3ff090
000000455b3ff050:  00007fff8ef28cd0  00007fff919b1810
000000455b3ff060:  000001c8dffc4570  0000000000000000
000000455b3ff070:  000001c800050005  00007fff8ef2a89b
000000455b3ff080:  000000000000001b  0000000000000000
000000455b3ff090:  0000000000000000  00007fff919800d8
000000455b3ff0a0:  0000000000000000  00007fff8ecd0000
000000455b3ff0b0:  0000000000000001  00007fff9199c734
000000455b3ff0c0: <000001c800000001  0000000000000000
000000455b3ff0d0:  00007fff0000ebb0  000000455b3ff1c8
000000455b3ff0e0:  000001c8dffc78f0  000000455b3ff148
000000455b3ff0f0:  0000000000250024  00007fff8e86b816
000000455b3ff100:  000001c8dffd7f80  00007fff91acc500
000000455b3ff110:  000001c8dffc4570  00007fff91acea5c
000000455b3ff120:  000001c80000094c  00007fff8e84b2d8
000000455b3ff130:  00007fff8e849138  00007fff91acc528
000000455b3ff140:  00007fff91adec6a  00007fff8e840000
000000455b3ff150:  00007fff91ad0f8c  00007fff91a00d80
000000455b3ff160:  000000451c1dc789  000001c8dffd8e18
000000455b3ff170:  000000455b3ff4d0  00000000c0000135
000000455b3ff180:  0000000000000000  0000000000000040
000000455b3ff190:  0000000000000004  00007fff91ae52f0
000000455b3ff1a0:  0000000000000001  000000455b3ff300
000000455b3ff1b0:  000001c8dfff1b60  00007fff919ae2a8
rax     0x7fff8e84be7c
rbx     0x7fff8e84be7a
rcx     0x41
rdi     0xffffffffffbadd11
rsi     0x0
rbp     0x455b3ff300
rsp     0x455b3ff0c0
r8      0x0
r9      0x0
r10     0x0
r11     0x94b
r12     0x7fff91980000
r13     0x0
r14     0x7fff8e84be7c
r15     0xc000007a
rip     0x1c8e0110000
rflags  0x10202
cs      0x33
fs      0x53
gs      0x2b

解决方案


这看起来像是一个 goroutine 死锁。 如果发生死锁,程序中运行的所有 goroutine 将退出并生成一个堆栈跟踪,就像您看到的那样。

程序将被go运行时终止。即使您的 http 服务器可能具有恢复功能,也无法从死锁中恢复。

本篇关于《Golang 运行 main.go 时频繁崩溃,出现 0xc0000005 异常》的介绍就到此结束啦,但是学无止境,想要了解学习更多关于Golang的相关知识,请关注golang学习网公众号!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>