# Troubleshooting


# "Database is locked"

DuckDB uses file-level locking. Only one process can write at a time.

**Symptoms:** `duckdb.IOException: Could not set lock on file`

**Solutions:**

- Wait for the other process to finish (e.g., a running [get](../reference/get.md#spice_kernel_db.get) or [scan](../reference/scan.md#spice_kernel_db.scan)).
- Read-only commands ([check](../reference/check.md#spice_kernel_db.check), `show`, `which`, [stats](../reference/KernelDB.stats.md#spice_kernel_db.KernelDB.stats), `ls`, `coverage`, [doctor](../reference/KernelDB.doctor.md#spice_kernel_db.KernelDB.doctor)) open the database in read-only mode and can run concurrently with a writer.
- If no other process is running, the lock file may be stale. Delete `<db_path>.wal` (the write-ahead log) and retry.


# "No locally acquired metakernels"

You need to configure a mission and download a metakernel first.

``` bash
spice-kernel-db mission add     # set up a mission
spice-kernel-db browse MISSION  # see what's available
spice-kernel-db get             # download one interactively
```


# "matches several tracked metakernels" (exit 2)

The name you gave is a base name and more than one versioned snapshot of it is tracked:

    juice_crema_5_1.tm matches several tracked metakernels:
      juice_crema_5_1_v470_20260101_001.tm
      juice_crema_5_1_v471_20260201_001.tm
    Pass one of them.

Pass the full registry filename, or stop tracking the snapshot you no longer want with `spice-kernel-db rm <filename>`. The same answer comes from every command, so a script can test for exit code 2.


# "needs a terminal for its menu" (exit 2)

A command that needed a metakernel was run without one from a script, a notebook or a pipe. At a terminal it would have shown a picker; without one it lists the tracked metakernels on stderr and exits 2 instead of waiting for input. Pass the metakernel as the argument (see [Naming a metakernel](cli.md#naming-a-metakernel)).


# "Not found" when resolving a kernel

The kernel exists on disk but isn't in the database yet.

``` bash
# Re-index the directory containing the kernel
spice-kernel-db scan /path/to/kernels
```


# `spice-kernel-db get` downloads everything again

By default, the tool skips files that match both the expected size and SHA-256 hash. If files were corrupted or partially downloaded, they'll be re-downloaded. Use `--force` to bypass all skip checks.

If the *same* kernels re-download on every [get](../reference/get.md#spice_kernel_db.get) or `update --execute` although they are byte-correct on disk, run `spice-kernel-db check <metakernel>` or `spice-kernel-db stats`. A non-zero **poisoned rows** count means those kernels are flagged as superseded by content that no longer exists anywhere (typically a pre-0.17.0 interrupted download or a Dropbox conflict copy that was later cleaned up), so resolution skips them. `spice-kernel-db scan <directory>` on the directory holding the files re-registers them and clears the flag.

Before 0.20.0 this could also happen to intact files. The skip check needs the remote size, and a HEAD request that failed was silently recorded as "size unknown" -- which the check reads as "cannot confirm", so the file was fetched again. Such kernels show as `unknown` in the size column. Size queries now retry and reuse connections, so a transient failure no longer costs you a download.


# `Hash mismatch for <name>: computed X, expected Y` during get/update

This was a download bug, fixed by writing each download to a temporary file and atomically replacing the destination (rather than `open(dest, "wb")`, which followed symlinks).

The cause: content-addressed deduplication leaves a symlink at an alias filename pointing to the shared canonical kernel. When the alias and its target later **diverge upstream** (e.g. a mission's predict CK and a measured CK that were once byte-identical), re-downloading wrote the new bytes straight *through* the symlink into the shared file. Parallel downloads of several aliases pointing at one file would clobber each other, and the corruption surfaced as this hash-mismatch error (the integrity check correctly refusing the bad file).

If you hit this on an older version, recover by removing the affected files and re-fetching:

``` bash
# Re-fetch the metakernels that touch the corrupted kernels
spice-kernel-db get <metakernel-url> --force
# Then re-run dedup so identical content is re-linked cleanly
spice-kernel-db dedup
```

To inspect which alias filenames share a physical file, look for cross-name symlinks in the kernel tree:

``` bash
find <kernel-dir> -type l -exec sh -c \
  '[ "$(basename "$1")" != "$(basename "$(readlink "$1")")" ] && echo "$1 -> $(readlink "$1")"' _ {} \;
```


# [get](../reference/get.md#spice_kernel_db.get) sits on "Querying sizes" for a long time

Before running anything, [get](../reference/get.md#spice_kernel_db.get) asks the archive for each kernel's size, one HEAD request per entry in `KERNELS_TO_LOAD`. Large metakernels are therefore slow against a slow server: `juice_crema_5_2.tm` has 102 kernels, and the ESA archive's TLS handshake alone was measured at 1.3 s serial and 3.3 s (median) under concurrency.

Connections are kept alive per worker thread since 0.20.0, so the handshake is paid once per thread rather than once per kernel. What remains is one round trip per kernel, which is the archive's latency rather than anything local. `--force` skips the size query entirely, at the cost of re-downloading everything.


# Metakernel paths don't work with SpiceyPy

SPICE resolves `PATH_VALUES` relative to the **current working directory**, not the metakernel file location. The tool writes absolute `PATH_VALUES` in saved `.tm` files so they work from any directory.

If you see `SPICE(NOSUCHFILE)`, check that the paths in the `.tm` file are absolute:

``` bash
grep PATH_VALUES /path/to/your.tm
```


# Symlinks not supported (Windows)

On Windows, symlink creation requires Developer Mode or administrator privileges. If symlinks fail, the tool still downloads and registers files -- only the symlinks are skipped. The metakernel may not work directly; use `rewrite` to create a local copy with correct paths.


# SpiceyPy not found for `coverage`

The `coverage` command requires the optional `spiceypy` dependency:

``` bash
pip install spice-kernel-db[spice]
```

Or install SpiceyPy separately: `pip install spiceypy`


# Corrupt config file

If `config.toml` does not parse, every command prints `Config file is corrupt: <path>` with the parser's message and exits 78 (the conventional configuration-error code, so a script never mistakes it for one of [check](../reference/check.md#spice_kernel_db.check)'s verdict bits); nothing falls back to setup, so the file is never overwritten behind you (from Python the same case raises [ConfigError](../reference/ConfigError.md#spice_kernel_db.ConfigError)). To fix it:

``` bash
# Show the config file location
spice-kernel-db config

# Re-run setup
spice-kernel-db config --setup
```

Or edit `~/.config/spice-kernel-db/config.toml` directly:

``` toml
[database]
path = "~/.local/share/spice-kernel-db/kernels.duckdb"

[storage]
kernel_dirs = ["~/.local/share/spice-kernel-db/kernels"]
```


# Stale entries after deleting files

If you've deleted kernel files from disk, the database still references them. Use `clean` to clear them out:

``` bash
# Preview what would be removed
spice-kernel-db clean

# Actually remove stale entries
spice-kernel-db clean --execute
```

Without a class flag, `clean` drops the locations whose file is gone and the dangling symlinks left in each mission's download tree. Neither needs the network.


# [get](../reference/get.md#spice_kernel_db.get) fails with "Metakernel unreachable" / HTTP 404

NAIF (and ESA) routinely rotate older versioned metakernel snapshots into a `former_versions/` subdirectory. Once that happens, the original URL stored in your local `metakernel_registry` returns 404, and `spice-kernel-db get <mk>` on that tracked metakernel aborts with a red panel like:

    ╭─── Metakernel unreachable ───╮
    │ The remote metakernel is no longer available.
    │   URL:      …/mk/juice_crema_5_1_…_v462_20260223_001.tm
    │   Status:   HTTP 404
    │ NAIF (and ESA) routinely rotate old versioned snapshots into a
    │ former_versions/ subdirectory, which makes the original URL 404.
    ╰──────────────────────────────╯

The exit code is `2` (distinct from generic LookupError's `1`), so scripts can detect this case specifically.

`spice-kernel-db update` (the walk over every tracked metakernel) does not abort on such a row. It lists the row in its summary table with the note `source gone (HTTP 404)` and goes on to the next one.

**Fix it with `clean --registry`:**

``` bash
# Preview every dead registry row (HEAD-probes each source_url)
spice-kernel-db clean --registry

# Actually remove the dead rows
spice-kernel-db clean --registry --execute

# Also delete the on-disk .tm files (symlink trees are shared across
# metakernels in a mission and are never auto-deleted)
spice-kernel-db clean --registry --execute --delete-files
```

`clean --registry` treats `403`, `404`, and `410` as permanently dead; transient errors (timeouts, DNS failures, `5xx`) deliberately don't classify a row as dead, so going offline never causes accidental deletion.

If you just want to remove the one offending entry:

``` bash
spice-kernel-db rm juice_crema_5_1_…_v462_20260223_001.tm
```

Then re-acquire the current version with `browse` → [get](../reference/get.md#spice_kernel_db.get).

**Want to re-fetch the *archived* version instead?** [get](../reference/get.md#spice_kernel_db.get) automatically retries under `former_versions/` when the live `mk/` URL 404s, so:

``` bash
spice-kernel-db get juice_crema_5_1_…_v462_20260223_001.tm --mission JUICE
```

…will transparently download the archived metakernel and its referenced kernels (which ESA keeps live in `kernels/{spk,fk,…}/` even after a `.tm` is rotated out). This is the recommended way to reproduce analyses against a specific older metakernel version.


# [check](../reference/check.md#spice_kernel_db.check) says the metakernel does not resolve (exit 2)

After a [get](../reference/get.md#spice_kernel_db.get) or `rewrite`, `spice-kernel-db check <mk>` walks every entry through `PATH_VALUES` and compares it with the database (the *resolves* verdict). Common findings:

| Status | What it means | What to do |
|----|----|----|
| `DANGLING` | Symlink target moved or was deleted | Re-run [get](../reference/get.md#spice_kernel_db.get) on this metakernel -- the fixed `_link_existing_kernels` now repairs dangling links. |
| `HASH_MISMATCH` (deep mode) | File was modified after registration | `clean --execute` to drop the location, then re-[get](../reference/get.md#spice_kernel_db.get) to fetch a clean copy. |
| `AMBIGUOUS` | The entry resolves to a path the database has not registered, and two non-superseded `kernels` rows share its filename | A path the database knows is never ambiguous, even when another mission or kernel root holds a different file of the same name. Run [scan](../reference/scan.md#spice_kernel_db.scan) so the path is registered. If the entry should still be ambiguous, use [find_by_filename](../reference/KernelDB.find_by_filename.md#spice_kernel_db.KernelDB.find_by_filename) in the Python API to see the rows, decide which to keep, and `clean` the other. |
| `UNREGISTERED` | File on disk but no `kernels` row | Run `scan <dir>` on the directory containing it. |
| `BAD_PATH_VALUE` | `PATH_VALUES` not absolute | Use [get](../reference/get.md#spice_kernel_db.get) (which writes absolute paths) instead of editing `.tm` files by hand. |
| `TRAVERSAL` | A `KERNELS_TO_LOAD` entry escapes `PATH_VALUES` | The metakernel is malformed or hostile -- don't `furnsh` it. |


# [check](../reference/check.md#spice_kernel_db.check) says `SIZE_MISMATCH` with "expected 0", or [doctor](../reference/KernelDB.doctor.md#spice_kernel_db.KernelDB.doctor) lists a path under more than one hash

One file on disk can have two `locations` rows: one for its real content and one for different content, typically a 0-byte row left by an interrupted download. `skd doctor` lists the path under "Paths registered under more than one hash". [check](../reference/check.md#spice_kernel_db.check) compares the file with the wrong row and reports `SIZE_MISMATCH` with a detail such as `size 31200000 != expected 0`.

Run `skd scan <directory>` on the directory that holds the file. The scan keeps the row that matches the file on disk and marks the other rows superseded. Run `skd doctor` again to confirm the list is empty.


# Mission `SPICE-KERNEL-DB`, or "Cross-mission: 102 kernels" (fixed, \#43)

A metakernel in skd's default store, for example `~/.local/share/spice-kernel-db/kernels/JUICE/mk/x.tm`, was given the mission `SPICE-KERNEL-DB`. The path guess took the directory that holds the store as the mission. [check](../reference/check.md#spice_kernel_db.check) then reported kernels as resolved from another mission, `Cross-mission: 102 kernels` in one case.

Two changes fix it. A tracked metakernel's mission now comes from its registry row (`skd ls` shows it) and is never guessed from the path again. For a file under a configured kernel root, the mission is the first directory below the root (`JUICE`); see [Several kernel roots](cli.md#several-kernel-roots).

A registry row written by an earlier version keeps its wrong mission until the metakernel is imported again. `skd import <path>` replaces the row, and `--mission JUICE` sets the mission explicitly.


# Mission has no metakernel directory at the default path

`mission add` looks for a metakernel directory at `{server_url}{MISSION}/kernels/mk/`. Most NASA missions on NAIF do **not** have one -- they publish individual per-type kernels (`ck/`, `fk/`, `lsk/`, `pck/`, `sclk/`, `spk/`) but no curated mission-wide `.tm` file. An empirical survey of NAIF in 2026 found:

- 8 missions with `kernels/mk/` (mostly ESA-mirror entries -- BEPICOLOMBO, EXOMARS2016, JUICE, LADEE, MEX, ROSETTA, SMART1, VEX)
- 68 missions with no `mk/` subdirectory anywhere on NAIF
- 0 missions using alternative templates like `spice_kernels/mk/`, `data/spice/mk/`, etc.

When the default path 404s, you have two productive options:

1.  **Manual override** -- pass `--mk-dir-url <URL>` to `mission add`. The URL is HEAD-checked but otherwise used as-is. Use this when you've found the metakernel directory yourself -- typically on a PDS node such as `pds-imaging.jpl.nasa.gov` or `pds-smallbodies.astro.umd.edu`.
2.  **planetarypy** -- the [planetarypy](https://github.com/michaelaye/planetarypy) library covers both remaining cases without spice-kernel-db. `plp spice fetch <mission> --start <date> --stop <date>` builds a metakernel for a date range from the mission's PDS SPICE archive, and `plp spice spk <mission>` downloads a live mission's current trajectory from NAIF's operational server (planetarypy 0.84 or newer). Automatic support for missions without `mk/` is tracked in [issue \#2](https://github.com/michaelaye/spice-kernel-db/issues/2).

The curated `mission_registry.toml` exists as an extension point: if you confirm a working metakernel directory (anywhere on the web), please open a PR adding an entry so other users benefit:

``` toml
["YOUR-MISSION"]
candidates = [
  "{server}{m}/some/alt/path/mk/",
  "https://some-pds-node.example/spice/your-mission/mk/",
]
```

Registry entries can use absolute URLs (PDS nodes, mission websites) -- the `{server}`/`{m}` placeholders are convenient but not required.


# `Error: No such option: --miss`

Since 0.22 the CLI no longer accepts abbreviated long options. Earlier versions completed any unambiguous prefix, so `--miss JUICE` meant `--mission JUICE` and `--exec` meant `--execute`. Spell the option out; the error message names the full option.
