Skip to content

[BUG] Txn iterator constructors panic on caller misuseΒ #61

@feichai0017

Description

@feichai0017

πŸ› Bug Description

Txn.NewIterator / Txn.NewKeyIterator panic on invalid runtime state and invalid options instead of returning a normal error.

Affected code:

  • txn_iterator.go:113
  • txn_iterator.go:170

Current panic cases include:

  • transaction already discarded
  • DB already closed
  • NewKeyIterator called with non-empty opt.Prefix

πŸ›  Reproduction Steps

Steps to reproduce the behavior:

  1. Create a transaction and call Discard().
  2. Call txn.NewIterator(...) or txn.NewKeyIterator(...).
  3. Or call txn.NewKeyIterator(key, IteratorOptions{Prefix: []byte("x")}).
  4. Observe process panic.

πŸ“‹ Expected Behavior

Iterator construction should fail with an explicit error path (or safe API variant) instead of panicking and potentially crashing the service process.

πŸ“Έ Screenshots / Logs

Observed panic messages:

  • Transaction has already been discarded
  • opt.Prefix should be nil for NewKeyIterator.

πŸ’» Environment

  • OS: macOS (arm64)
  • Go Version: 1.26
  • NoKV Version: main (2026-02-14)

🧐 Additional Context

This is an API robustness issue. panic may be acceptable for internal invariants, but these paths are easy to trigger from caller misuse and can crash long-running services.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingstatus/reproducedBug has been reproduced

    Projects

    Status

    Todo

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions