> ## Documentation Index
> Fetch the complete documentation index at: https://second.tech/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Run wallet maintenance

> Let Bark's background daemon refresh VTXOs before they expire: what it runs, how to keep it running on each surface, and how to tune and monitor refreshes.

The Ark server can sweep a VTXO after its `expiry_height` ([VTXO lifetime](https://second.tech/docs/learn/lifetime.md) explains the mechanics). Users should never have to think about the deadline.

## Let the background daemon run maintenance

Bark's background daemon runs wallet maintenance with no call from your wallet app. While it runs, it:

* Syncs the on-chain wallet and every pending board, payment, and round. A sync runs every 60 seconds by default (`daemon_sync_interval_secs`) and on every new block.
* Joins the next round whenever a VTXO is due for a refresh, then completes the refresh once the round transaction confirms.
* Scans for forced exits on every sync pass and progresses emergency exits ([forced exits](https://second.tech/docs/patterns/forced-exits.md#keep-the-response-running-and-funded) covers the response).

How the daemon starts depends on the surface:

* **Bark SDK:** The library starts the daemon when your app opens the wallet (`runDaemon` in the open arguments, `run_daemon` in Rust, on by default). In Go, set `RunDaemon: true` explicitly, since an unset `bool` field is `false`. The daemon runs for as long as your app holds the open wallet. In a browser, that is for as long as the tab stays open.
* **Barkd:** `barkd` starts the daemon and runs it for as long as the process lives. Run `barkd` under a service manager, such as systemd, so it starts again after a crash or a reboot.
* **iOS and Android:** iOS and Android suspend your app in the background. Use delegated refreshes, which the Ark server carries through the round without your app online. Run `maintenanceDelegated()` on background wakes and foreground opens ([background refresh](https://second.tech/docs/patterns/background-refresh.md) covers the setup).
* **Bark CLI:** No daemon runs between `bark` commands. For a wallet that maintains itself, run Barkd. To stay on the CLI, [schedule the maintenance pass](https://second.tech/docs/patterns/maintenance.md#run-maintenance-at-least-daily).

> [!NOTE]
>
> **The background daemon stops when your process does**
>
> Neither Barkd nor an SDK app can run maintenance while its process is stopped. A reboot without a service manager, a suspended mobile app, or a closed browser tab all pause maintenance until the process starts again.

## Run maintenance at least daily

Without a daemon, your wallet app runs the maintenance pass itself. Run `bark maintain` on the CLI. In an SDK app that opens the wallet with `runDaemon: false`, call `maintenance()` (`Wallet::maintenance()` in Rust). A pass syncs the wallet, progresses pending rounds, and refreshes the VTXOs that are due.

A maintenance pass refreshes any VTXO within `vtxo_refresh_expiry_threshold` blocks of its expiry, 144 blocks (about 24 hours) by default on mainnet. A daily pass is therefore the floor, not a guarantee. A VTXO that enters the window just after one pass reaches its expiry around the next one. Its refresh then still waits for a round. Running every 6 or 12 hours leaves margin for downtime, missed passes, and fast block stretches.

![Diagram comparing one maintenance pass a day with a pass every 6 hours over the last 144 blocks before Alice's VTXO expires](https://mintcdn.com/second-0659a37d/tQnT1N_qr7W8s6kQ/images/maintenance-pass-cadence.svg?fit=max\&auto=format\&n=tQnT1N_qr7W8s6kQ\&q=85\&s=f02b8c8e9519b44274cb156f0e6bc1c5)

With one pass a day, a VTXO that enters the refresh window just after a pass is reached by the next pass only at its expiry, while a pass every 6 hours reaches it with 108 blocks left

> [tab] Bark SDK
>
> With `runDaemon: false`, run `maintenance()` when the app starts or comes to the foreground, and every 6 to 12 hours. On iOS and Android, run `maintenanceDelegated()` instead ([background refresh](https://second.tech/docs/patterns/background-refresh.md#run-one-pass-per-wake) covers the pass). While `hasPendingExits()` returns true, run `progressExits()` every few minutes ([exit handling](https://second.tech/docs/patterns/exits.md) covers what that needs).

> [tab] Bark CLI
>
> Nothing runs between commands, so nothing moves an exit forward either. Schedule `bark maintain` and `bark exit progress` with cron or a systemd timer:
>
> ```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
> # Maintenance every 6 hours, exit progress every 10 minutes, under one lock.
> # Cron sets a minimal PATH, so point at the bark binary directly.
> 0 */6 * * * flock "$HOME/.bark-cron.lock" /usr/local/bin/bark maintain >> "$HOME/bark-maintain.log" 2>&1
> */10 * * * * flock -n "$HOME/.bark-cron.lock" /usr/local/bin/bark exit progress >> "$HOME/bark-exit.log" 2>&1
> ```
>
> Each `bark` command that opens the wallet locks the data directory, so a command that starts while another runs fails. When a refresh is due, `bark maintain` holds that lock until the next round ends, which can take an hour with Second's hourly rounds. Both jobs take the same `flock` file, so `bark maintain` waits for a running `bark exit progress`. `flock -n` skips `bark exit progress` while maintenance runs. Write the logs and the `flock` file outside `~/.bark`, because `bark create` refuses a data directory with unexpected files in it.

## Tune the refresh policy

How early to refresh is a policy your app exposes to users, not a protocol constant. Refreshing early improves security sooner and raises operating costs. Refreshing late saves fees. [Ark payments](https://second.tech/docs/learn/payments.md) explains the trade-off. [Liquidity](https://second.tech/docs/learn/liquidity.md) explains what each refresh costs.

One setting sets the window: `vtxo_refresh_expiry_threshold` in `config.toml` for the CLI and Barkd ([configuration reference](https://second.tech/docs/getting-started/bark-cli/configuration.md)), or the same field in the SDK's `Config` (`vtxoRefreshExpiryThreshold` in TypeScript):

```toml theme={"theme":{"light":"github-light","dark":"github-dark"}}
# Refresh any VTXO within this many blocks of expiry (mainnet default: 144)
vtxo_refresh_expiry_threshold = 144
```

On mainnet, set the threshold below half the Ark server's VTXO lifetime, or Bark refuses to connect to that server. For a single run, `bark refresh --threshold-blocks <n>` or `--threshold-hours <n>` overrides the window. [Refresh options](https://second.tech/docs/patterns/refresh-options.md#refresh-interactively-when-the-app-stays-online) lists the other selections. A maintenance refresh can include more VTXOs than the one that triggered it and charge a fee ([show refresh fees](https://second.tech/docs/patterns/refresh-fees.md) covers what to display).

## Stay online through round participation

Refreshes settle in [rounds](https://second.tech/docs/learn/rounds.md), which are interactive. Bark submits the inputs and co-signs the new transaction tree while the round runs. It signs the forfeits once the round transaction confirms. Keep the process alive from submission until the round ends. Have it running again after the round transaction confirms and before the old VTXOs expire.

* **An interrupted participation starts over:** Bark drops an interactive participation that stalls. It refreshes the same VTXOs in a later round.
* **Pending participations are listable:** `bark round list`, `GET /api/v1/wallet/rounds`, and the SDK's `pendingRoundStates()` show each one with its `state`.
* **Mobile suspends apps mid-round:** Submit a [delegated refresh](https://second.tech/docs/patterns/refresh-options.md#delegate-the-round-when-the-app-cannot-stay-online) so the round completes without your app staying online. An interactive refresh first waits for the next round to start, up to an hour on Second's Ark server. [Background refresh](https://second.tech/docs/patterns/background-refresh.md) covers the mobile setup.

## Expose the next refresh deadline

Expose the deadline instead of guessing at it:

> [tab] Bark SDK
>
> * **Next required refresh:** `getNextRequiredRefreshBlockheight` (`get_next_required_refresh_blockheight` in Rust) returns the height at which the next refresh becomes required, the first expiry minus the threshold.
> * **First expiry:** `getFirstExpiringVtxoBlockheight` (`get_first_expiring_vtxo_blockheight` in Rust) returns that first expiry itself.
> * **With a round pending:** Also take the lowest `expiryHeight` among the VTXOs that `pendingRoundInputVtxos()` returns ([background refresh](https://second.tech/docs/patterns/background-refresh.md#catch-devices-that-stop-waking) covers the call).
> * **Pushed events:** `notifications().nextNotification()` (`Wallet::subscribe_notifications()` in Rust) streams movement events as they happen. Reread `history()` when a `ChannelLagging` event arrives. Read the two heights above on your own schedule, for example each time the app comes to the foreground.

> [tab] Bark CLI
>
> `bark vtxos` prints each VTXO's `expiry_height`. Subtract your `vtxo_refresh_expiry_threshold` from the lowest one. `bark watch` streams wallet notifications as JSON and runs the background daemon for as long as the command runs.

> [tab] Barkd
>
> `GET /api/v1/wallet/vtxos` returns each VTXO's `expiry_height`. Subtract your `vtxo_refresh_expiry_threshold` from the lowest `expiry_height` among spendable VTXOs to get the next required refresh. For pushed movement events, fetch a single-use WebSocket ticket from `GET /api/v1/notifications/ws/ticket` (valid for 10 minutes), then connect to `/api/v1/notifications/ws?ticket=<ticket>`. `GET /api/v1/notifications/wait` offers the same as a long poll. Call `GET /api/v1/history` when a `channel-lagging` event arrives.

[Expired VTXOs](https://second.tech/docs/patterns/expired-vtxos.md#name-the-lifecycle-states) covers how to turn these heights into user-facing state.

## Plan around platform limits

* **Background execution is best effort:** Mobile operating systems throttle background work. No scheduler API guarantees that your pass runs at the requested time. Run the pass on foreground opens as well. Give users who stop getting wakes a way back. [Background refresh](https://second.tech/docs/patterns/background-refresh.md) covers how to wake a mobile app and catch devices that stop waking.
* **A browser wallet app runs only while its tab is open:** The daemon resumes maintenance when the user opens the app. Warn users who return after a long absence to keep the tab open until the refresh completes.
* **A missed refresh still leaves room:** Expiry is the deadline, not your schedule. A VTXO that missed a refresh window remains yours to refresh until its `expiry_height` passes. [Expired VTXOs](https://second.tech/docs/patterns/expired-vtxos.md) covers what happens after that.

> [accordion] How Bark Wallet does it
>
> * **Two versions, two paths:** In the installed version (Umbrel, Start9, or self-hosted), a `barkd` daemon runs alongside the app. It refreshes VTXOs and progresses exits whether or not a browser tab is open. In the web version (WASM), the wallet runs in the browser. The app submits its own refreshes as delegated participations. A worker loop that syncs every 20 seconds while a tab is open completes them, on the next open if the tab closed first.
> * **The policy as two settings:** [Bark Wallet](https://second.tech/docs/bark-wallet.md) turns the policy into two settings, [Auto-refresh threshold and Refresh on receive](https://second.tech/docs/bark-wallet/settings.md#refresh-behavior). While the dashboard is open, it refreshes every spendable VTXO inside the chosen threshold that is not already in a round, at most once a minute for the same set. In the installed version the `barkd` daemon applies its own `vtxo_refresh_expiry_threshold` to its background refreshes.
> * **Options priced by the server:** The threshold options come from the server's refresh fee table, each labeled with its time and price and capped at half the VTXO lifetime. Until the user selects one, the app uses the first option, "1 day before expiry · free" on Second's Ark server.
> * **The deadline shown as a round:** The dashboard shows a ["Next round" countdown](https://second.tech/docs/bark-wallet/dashboard.md#the-next-round-countdown) computed from the server's round schedule. The user cannot select a VTXO already committed to a round in the VTXOs table. In the web version, "Refresh all" leaves it out. "Refresh all" is disabled only while its own request runs or when no other VTXO is left to refresh.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.