Skip to content

fix: PebbleDB and noneSharedKV Close idempotent using sync.Once.#51

Open
sendya wants to merge 1 commit intomainfrom
fix/close-with-once
Open

fix: PebbleDB and noneSharedKV Close idempotent using sync.Once.#51
sendya wants to merge 1 commit intomainfrom
fix/close-with-once

Conversation

@sendya
Copy link
Member

@sendya sendya commented Mar 19, 2026

This pull request introduces a thread-safe mechanism to ensure that the Close() method for both the PebbleDB and noneSharedKV storage implementations is executed only once, even if called multiple times. This prevents potential issues such as double-closing the underlying database, which could lead to panics or resource leaks.

Thread safety and resource management improvements:

  • Added a sync.Once field named closed to both the PebbleDB and noneSharedKV structs to guarantee that the Close() operation is performed only once, regardless of how many times Close() is called. [1] [2] [3] [4]
  • Updated the Close() methods in both pebble.go and nonekv.go to use the sync.Once field, ensuring thread safety and preventing multiple close attempts on the same database instance. [1] [2]
  • Added the necessary import for the sync package in both files to support the new thread-safe logic. [1] [2]

Copilot AI review requested due to automatic review settings March 19, 2026 07:02
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR aims to make Close() idempotent and thread-safe for the Pebble-backed IndexDB (PebbleDB) and the Pebble-backed SharedKV (noneSharedKV) by guarding shutdown with sync.Once, preventing double-close of the underlying Pebble DB.

Changes:

  • Add a sync.Once field (closed) to PebbleDB and noneSharedKV.
  • Update both Close() implementations to execute the underlying Pebble Flush()/Close() only once.
  • Add sync imports to support the new behavior.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
storage/sharedkv/nonekv.go Adds sync.Once guard to make noneSharedKV.Close() run only once.
storage/indexdb/pebble/pebble.go Adds sync.Once guard to make PebbleDB.Close() run only once (including a one-time flush).

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

You can also share your feedback on Copilot code review. Take the survey.

Comment on lines 20 to +25
func (r *noneSharedKV) Close() error {
return r.db.Close()
var err error
r.closed.Do(func() {
err = r.db.Close()
})
return err
Comment on lines 116 to +123
func (p *PebbleDB) Close() error {
// force flush data to disk
_ = p.db.Flush()
return p.db.Close()
var err error
p.closed.Do(func() {
_ = p.db.Flush()
// force flush data to disk
err = p.db.Close()
})
return err
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants