#1282·bbolt

`MoveBucket` into the moved bucket itself or one of its sub-buckets succeeds and leaves the bucket unreachable

Author: DRMacIverCreated Sep 23, 2026Updated Sep 23, 2026

Moving a bucket into itself, or into one of its own sub-buckets, returns nil and commits; afterwards the bucket is not listed at the top level any more and tx.Check() reports its page as unreachable. Moving the same bucket into the parent it is already in is refused with ErrSameBuckets:

go
package main

import (
	"fmt"
	"os"
	"path/filepath"

	bolt "go.etcd.io/bbolt"
)

func main() {
	dir, _ := os.MkdirTemp("", "bbolt")
	defer os.RemoveAll(dir)

	for i, dest := range []string{"the root", "itself", "its child a/b"} {
		db, err := bolt.Open(filepath.Join(dir, fmt.Sprintf("%d.db", i)), 0600, nil)
		if err != nil {
			panic(err)
		}
		db.Update(func(tx *bolt.Tx) error {
			a, _ := tx.CreateBucket([]byte("a"))
			b, _ := a.CreateBucket([]byte("b"))
			b.Put([]byte("k"), []byte("v"))
			return a.Put([]byte("x"), []byte("y"))
		})

		err = db.Update(func(tx *bolt.Tx) error {
			a := tx.Bucket([]byte("a"))
			var dst *bolt.Bucket // nil: the root, where "a" already is
			switch dest {
			case "itself":
				dst = a
			case "its child a/b":
				dst = a.Bucket([]byte("b"))
			}
			return tx.MoveBucket([]byte("a"), nil, dst)
		})
		fmt.Printf("move a into %s: err=%v\n", dest, err)

		db.View(func(tx *bolt.Tx) error {
			var names []string
			tx.ForEach(func(name []byte, _ *bolt.Bucket) error {
				names = append(names, string(name))
				return nil
			})
			fmt.Printf("  top-level buckets: %q\n", names)
			for e := range tx.Check() {
				fmt.Printf("  tx.Check: %v\n", e)
			}
			return nil
		})
		db.Close()
	}
}

Output:

move a into the root: err=the source and target are the same bucket
  top-level buckets: ["a"]
move a into itself: err=<nil>
  top-level buckets: []
  tx.Check: page 4: unreachable unfreed
move a into its child a/b: err=<nil>
  top-level buckets: []
  tx.Check: page 4: unreachable unfreed

In the second and third cases the transaction commits without error, a (with a/b and both keys) is no longer reachable from any bucket, and tx.Check() flags the page. I expected a destination that is the bucket being moved, or lies inside it, to be rejected the way the same-bucket destination is.

Tested on bbolt v1.5.0 and on current main (4dc08f7).

BTW, this was found by an automated program that writes property-based tests for various open source projects using hegel (but it has been reviewed by hand before reporting). You can see the tests at https://github.com/hegeldev/hegel-zoo/tree/main/targets/go/bbolt. Let us know if you would like us to contribute the tests. NB the tests are currently LLM generated and probably not yet suitable for inclusion as is, but we're happy to help get them into a better state if you want them.