Question: more resilient to panic while db is closed but there may be some ongoing write transactions

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

$ go version
go version go1.13.15 darwin/amd64

What operating system are you using?

macOS big sur 11.0.1

What version of Badger are you using?

1.6.2

Does this issue reproduce with the latest master?

Not sure

What Badger options were set?

default option.

Hi there, I’ve been trying badgerDB in one of my projects.
Basically I have some object A that contains a db reference and will create write transactions, etc.

However, at any time, there could be a user who will trigger an event to get rid of object A. This in turns should trigger a db’s close and terminates any on going db work.
However, some of the db work is done in the background and won’t be tracked.

If such background work tries to use a write transaction while the db is already closed, we will get a panic in the application.
Therefore, I am wondering if there is a way to say detect that connection is closed, don’t panic in those background work, but just clean up and exit gracefully?

As a comparison, If I use sqlite3 as my db store instead, I will just get connection closed error in the background work logic and can exit cleanly.

Thanks

Hey @hu13, you can use the db.IsClosed() API in your background routines.

There is still a chance that the transaction could panic and you should use Defer, Panic, and Recover - The Go Blog to recover from it. It’s hard to ensure that the transaction remains valid when the DB is being closed.