-
Notifications
You must be signed in to change notification settings - Fork 336
-
Notifications
You must be signed in to change notification settings - Fork 336
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
runtime error: invalid memory address or nil pointer dereference #165
Comments
@Oceanchang What is the configuration of your nutsdb startup? Can you give me a complete code that you can remove the business code and reproduce the problem? |
|
如果方便的话,可否直接使用 Markdown 语法粘贴代码,这样方便复制粘贴代码到本地运行,否则只能照着代码一个个手输。 |
我尝试本地运行了一下,没有 panic,这是测试的代码(截图中的代码无法直接运行,因此贴出的代码在截图中代码的基础上做了一定修改): package main
import (
"fmt"
"github.com/xujiajun/nutsdb"
)
var bucket string
var db *nutsdb.DB
func init() {
opt := nutsdb.DefaultOptions
fileDir := "./nutsdb"
opt.Dir = fileDir
opt.SegmentSize = 1024 * 1024
db, _ = nutsdb.Open(opt)
bucket = "bucketForString"
}
func main() {
username := "test"
pid, err := read(username + "_pid")
fmt.Println(pid, "---", err)
// 输出结果:--- bucket not found:bucket:bucketForString,key:test_pid
}
func read(k1 string) (string, error) {
var out string
if err := db.View(
func(tx *nutsdb.Tx) error {
key := []byte(k1)
e, err := tx.Get(bucket, key)
if err != nil {
return err
}
out = string(e.Value)
return nil
}); err != nil {
return "", err
}
return out, nil
} 运行环境:MacOS,go 1.17,nutsdb@v0.8.0 |
谢谢,忽然想到是否是因为没有defer db.Close() 导致有时程序退出时错误,所以在每次read时执行db的open和close,使用曾出现异常的文件运行时,报错如下: 2022/05/28 15:05:01 db.buildIndexes error: when build activeDataIndex readAt err: crc error func read(k1 string) (string, error) {
db, err = nutsdb.Open(opt)
if err != nil {
log.Fatal(err)
}
defer db.Close()
var out string
if err := db.View(
func(tx *nutsdb.Tx) error {
key := []byte(k1)
e, err := tx.Get(bucket, key)
if err != nil {
return err
}
out = string(e.Value)
return nil
}); err != nil {
return "", err
}
return out, nil
} |
Open 是个很重的动作,你可以在main里面初始化的一个db实例就好了,不用每次read都open 和 close,后面用这个db实例。 |
之前就是有初始化一个db这样使用,结果是有时候程序killed,再次起来就报标题的错误了,如果遇到open提示crc error是否可以在程序层面处理? |
crc error 是数据有异常,不知道你是怎么搞出来的这个错误,一般不会发生,程序层面貌似不能处理,首先要弄清楚什么情况下出现了crc error |
知道问题出在什么地方了,有可能是多个地方open这个文件,或反复open导致的,集中到一个package中做初始化打开一个db应该可以避免这个问题再次发生,thanx |
遇到了同样的问题
使用的是 Caddy cache 插件 ,cache 部分的配置文件如下,在中等流量条件下几钟内就会 panic 退出。 {
"log_level": "WARN",
"allowed_http_verbs": ["GET", "HEAD"],
"headers": ["Content-Type", "Authorization"],
"key": {
"disable_host": true,
"disable_body": true,
"disable_method": false,
"headers": ["Content-Type"]
},
"nuts": {
"configuration": {
"Dir": "/var/cache/caddy",
"EntryIdxMode": "HintKeyAndRAMIdxMode",
"RWMode": "FileIO",
"SegmentSize": "256MB",
"NodeNum": 42,
"SyncEnable": false,
"StartFileLoadingMode": "MMap"
}
},
"ttl": "7200s"
} |
Describe the bug
A clear and concise description of what the bug is.
runtime error: invalid memory address or nil pointer dereference
/usr/local/go/src/runtime/panic.go:220 (0x404c295)
panicmem: panic(memoryError)
/usr/local/go/src/runtime/signal_unix.go:818 (0x404c265)
sigpanic: panicmem()
//pkg/mod/github.com/xujiajun/nutsdb@v0.8.0/tx.go:123 (0x48d6518)
(Tx).getTxID: node, err := snowflake.NewNode(tx.db.opt.NodeNum)
//pkg/mod/github.com/xujiajun/nutsdb@v0.8.0/tx.go:111 (0x48d649e)
newTx: txID, err = tx.getTxID()
/*/pkg/mod/github.com/xujiajun/nutsdb@v0.8.0/tx.go:85 (0x48d6364)
(*DB).Begin: tx, err = newTx(db, writable)
/*r/pkg/mod/github.com/xujiajun/nutsdb@v0.8.0/db.go:936 (0x48d5184)
(DB).managed: tx, err := db.Begin(writable)
//pkg/mod/github.com/xujiajun/nutsdb@v0.8.0/db.go:293 (0x4969490)
(*DB).View: return db.managed(false, fn)
To Reproduce
Steps to reproduce the behavior(Be specific!):
Give sample code if you can.
Expected behavior
A clear and concise description of what you expected to happen.
key不存在不会导致异常panic,可以返回错误信息进行处理
What actually happens
A clear and concise description of what actually happens.
Screenshots
If applicable, add screenshots to help explain your problem.
please complete the following information :
Mac OSX
github.com/xujiajun/nutsdb v0.8.0
Additional context
Add any other context about the problem here.
The text was updated successfully, but these errors were encountered: