Golang 在运行 main.go 时不断崩溃,异常 0xc0000005

标签 go go-gorm golang-migrate

我是 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
我在网上进行了很多研究,但找不到解决此问题的答案。是否有任何解决方案可以永久解决此问题,而不是重新启动 PC。
完全异常(exception):
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/pq@v1.1.1/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/pq@v1.1.1/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/pq@v1.1.1/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/pq@v1.1.1/conn.go:275 +0x99
github.com/lib/pq.Open(...)
        C:/Users/Usman Ali/go/pkg/mod/github.com/lib/pq@v1.1.1/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/pq@v1.1.1/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/gorm@v1.9.16/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)

<nil>
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/pq@v1.1.1/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/pq@v1.1.1/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/pq@v1.1.1/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/pq@v1.1.1/conn.go:275 +0x99
github.com/lib/pq.Open(...)
        C:/Users/Usman Ali/go/pkg/mod/github.com/lib/pq@v1.1.1/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/pq@v1.1.1/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/gorm@v1.9.16/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,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64050104/

相关文章:

sql - 如何在golang gorm中执行嵌套查询

go - 将 gorm 日志存储在文件中

postgresql - golang使用终端命令迁移未知驱动程序postgresql

file - 如何将文件保存在内存中——在服务器之间传输文件而不将它们存储在本地

Go模板删除范围循环中的最后一个逗号

Go 的 git 库

sockets - 如何从Golang中的socket io获取ip地址

http - 计算在 Go 中调用(请求处理程序)函数的次数

go - 如何使用GORM为归一化表格进行适当的模型关联?

dictionary - 在结构中声明和使用映射