atuin: Pool timed out while waiting for an open connection, ZFS

Edit by @ellie:

This has become the canonical issue for Atuin/ZFS issues

If you’re using ZFS with Atuin, you have likely noticed an error such as the following:

Error: pool timed out while waiting for an open connection

Location:
    /home/runner/work/atuin/atuin/crates/atuin-client/src/record/sqlite_store.rs:48:20

This is due to an issue with ZFS and SQLite. See: https://github.com/openzfs/zfs/issues/14290

There are two workarounds

  1. Use the Atuin daemon

This has not yet been released as stable, however is mostly without issue. The daemon takes all SQLite writes off of the hot path, therefore avoiding the issue.

Follow the steps here: https://github.com/atuinsh/atuin/issues/952#issuecomment-2121671620

  1. Create an ext4 zvol for Atuin

Follow the following steps: https://github.com/atuinsh/atuin/issues/952#issuecomment-1902164562


I’ve just begun using atuin, and I absolutely love it so far. However, there’s been a recurring issue for me, which I’ve found hard to diagnose:

My prompt regularly blocks for between 500ms to 5s whenever I run a command. I’ve narrowed this down to the _atuin_preexec function, by manually importing the shell hook generated from atuin init zsh and annotating it with logging and time calls. Here’s a sample time call from a time where it hang:

Running pre-exec for cd ~

0.00user 0.00system 0:04.93elapsed 0%CPU (0avgtext+0avgdata 8192maxresident)k
52036inputs+1064outputs (15major+512minor)pagefaults 0swaps

Pre-exec done for cd ~

Here’s how I modified the hook to get the result:

_atuin_preexec() {
    log "Running pre-exec for $1\n" >> /tmp/atuin.log
    local id
    id=$(/usr/bin/time -a -o /tmp/atuin.log atuin history start -- "$1")
    export ATUIN_HISTORY_ID="$id"
    echo "\nPre-exec done for $1" >> /tmp/atuin.log
}

I’ve tried to replicate the behavior in cli use outside of the hook using hyperfine, and was successful:

» hyperfine -r 1000 "atuin search --limit 5"
Benchmark 1: atuin search --limit 5
  Time (mean ± σ):      18.3 ms ± 114.8 ms    [User: 4.9 ms, System: 8.2 ms]
  Range (min … max):    12.5 ms … 2587.9 ms    1000 runs

This does not happen on every benchmark, even with 1000 runs. My initial thought was that this has to be contention on the database file, but I saw that you’re already using WAL, so concurrent writes/reads should not be a problem. I can also trigger the delay by repeatedly opening the search widget, which should not even be doing writes to the database, which confuses me even more.

Do you have any idea on how I could gather further data on this?

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Reactions: 6
  • Comments: 68 (30 by maintainers)

Commits related to this issue

Most upvoted comments

If you want to stay on zfs a workaround is to create a small zvol, format it with ext4 and that fixes the performance problems for me too.

sudo zfs create -V 500MB rpool/nixos/atuin
sudo zfs list -o name,encryption | grep atuin # ensure encryption for the zvol
sudo mkfs.ext4 /dev/zvol/rpool/nixos/atuin
mv ~/.local/share/atuin ~/.local/share/atuin-backup
mkdir ~/.local/share/atuin
sudo mount /dev/zvol/rpool/nixos/atuin ~/.local/share/atuin
sudo chown -R ${USER}: ~/.local/share/atuin
cp -rv ~/.local/share/atuin-backup/* ~/.local/share/atuin 

Also @ellie, while you’re on the thread; thanks for Atuin! I absolutely love it, and it’s a game changer I’d have a hard time parting with 😄

Thank you! Always nice to hear that ❤️

I couldn’t say as I haven’t dug through the sqlite source more and a quick scan.

The docs detail it pretty nicely: https://www.sqlite.org/lang_transaction.html

“Any command that accesses the database (basically, any SQL command, except a few PRAGMA statements) will automatically start a transaction if one is not already in effect. Automatically started transactions are committed when the last SQL statement finishes.”

Anyway, there is now a workaround on main (and now in v18.3.0-prerelease.1) that should solve this for everyone. We’ve introduced Atuin daemon which makes this problem a non-issue. Essentially, we take all of the writes off of the hot path, and make sure that they happen in the background. The only remaining writes are to a unix socket, which should be fast regardless of fs.

You can read more here: https://forum.atuin.sh/t/moving-atuin-to-a-daemon/78

If you’d like to try it, please

  1. Run v18.3.0-prerelease.1. If you’re not comfortable doing this, wait until the next stable release 😃
  2. Add
[daemon]
enabled = true

to your config file

  1. Run atuin daemon somewhere. You could add a systemd unit file or similar. In the future we will handle this for you, but for now it’s up to the user. You can read some more about it here

Complete Solution for Atuin on ZFS with identical dotfiles on many machines

I’d like to share my solution for managing Atuin across multiple machines with both ZFS and non-ZFS filesystems using a single dotfiles repository:

File Structure

dotfiles/
├── configs/
│   └── atuin/
│       ├── config.toml       # Standard config (daemon disabled)
│       └── zfs/
│           └── config.toml   # ZFS-specific config (daemon enabled)
├── scripts/
│   └── setup-atuin-daemon.sh # Systemd service setup script

Conditional Configuration

In my shell startup script:

# -- Atuin daemon management
# On ZFS filesystems, we need to use Atuin's daemon mode to avoid the SQLite/ZFS 
# performance bug (https://github.com/atuinsh/atuin/issues/952).
# If the Atuin daemon process is running, we point to a ZFS-specific
# config that has daemon=true enabled. This ensures that all Atuin commands
# communicate with the running daemon rather than attempting direct database access.
# The setup-atuin-daemon.sh script should be run on ZFS systems to create and 
# start the systemd service for the daemon.
if pgrep -f "atuin daemon" > /dev/null; then
  export ATUIN_CONFIG_DIR="$HOME/.config/atuin/zfs"
fi

# Initialize Atuin
eval "$(atuin init bash)" # or zsh

Systemd Service Setup Script (scripts/setup-atuin-daemon.sh)

#!/bin/bash
# Sets up systemd user service for Atuin daemon on ZFS systems

# Create systemd user directory if it doesn't exist
mkdir -p ~/.config/systemd/user/

# Create the service file with the correct path to atuin
cat > ~/.config/systemd/user/atuin.service << EOF
[Unit]
Description=Atuin daemon service
Documentation=https://docs.atuin.sh
After=network.target
StartLimitIntervalSec=0

[Service]
Type=simple
Restart=always
RestartSec=1
ExecStart=$(which atuin) daemon

[Install]
WantedBy=default.target
EOF

# Reload systemd user daemon
systemctl --user daemon-reload

# Enable and start the service
systemctl --user enable atuin.service
systemctl --user start atuin.service

echo "Atuin daemon is now set up with systemd and running"

Config Files

Standard config (configs/atuin/config.toml)

# Regular config for non-ZFS systems
sync_address = "https://api.atuin.sh"
# ... other settings

ZFS-specific config (configs/atuin/zfs/config.toml)

# Identical to main config but with daemon enabled
sync_address = "https://api.atuin.sh"
# ... other settings

[daemon]
enabled = true

How to Use

  1. Install dotfiles using dotbot which symlinks the entire atuin folder
  2. On ZFS systems, run scripts/setup-atuin-daemon.sh to set up and start the daemon
  3. The shell script automatically detects if the daemon is running and uses the appropriate config

This solution gives me consistent behavior across all my machines while automatically adapting to ZFS systems without manual configuration on each installation.

The key insight was to use the presence of the Atuin daemon process as a reliable indicator of whether to use the ZFS-specific config, rather than modifying the main config. This works seamlessly across both Linux and macOS systems.

Excited for the new daemon so I can start using Atuin again! 👍

It’s out now as part of v18.3.0! Still experimental, but safe to use

Docs: https://docs.atuin.sh/reference/daemon/

Testing it now and it’s working great so far!

BTW for others who are using NixOS I found this forum post very helpful in setting up the daemon: https://forum.atuin.sh/t/getting-the-daemon-working-on-nixos/334/3

And then I enabled the atuin daemon setting in home-manager like this:

programs.atuin.settings = {
  daemon = {
    enabled = true;
  };
};

Ahhh I see, ZFS.

We’ve had a few people have issues there

https://github.com/NixOS/nixpkgs/issues/169457 is relevant, I believe

I don’t think this is specific to atuin at all. This pretty much normal sqlite behaviour, when doing a transaction. I tracked it down to the sqlite source code. ZFS just doesn’t perform well with sqlite from the looks of it.

@hvisage I’m afraid that your requirements aren’t really something we can support. If OpenZFS fixes their bug, then all should be fine - but until that point, some sort of workaround is required.

😭

so it does become a, challenge with the daemon popping up everywhere now

Could you elaborate a bit here? We technically already run a short-lived background process on every single command, the daemon is just formalizing that a little. I’m not too sure how it’s now “popping up everywhere”.

I would need a deamon for each user using it, and then also for each service account (like root/mendix/postgres/etc.) when I do switch users - yes, it had been a great help for things in the various user and service accounts… just, I use ZFS everywhere (other than my desktop/laptop that’s macOS) 😭

If blocking for 5s, running the daemon, or creating a zvol is unacceptable - you could patch the code.

everytime I type, and then in between searches, it’s actually becoming unusable where I don’t expect it to be unusable 😭

Okay.

Let me just say: Ellie Thank you, it is a GREAT TOOL! (where there is no ZFS)

I’ll have to deal/find a way to handle this challenge when I have time available 😭

Thank you otherwise for attempting to find work arounds, I might have a more unique setup/style, so will not bother further on this issue.

This shouldn’t be an issue after the daemon feature is released, so that all the DB writes are handled off of the critical path. #2006

but I’m not a contributor (yet!)

Let us know if we can do anything to help!

so I don’t know the culture, etc, around this sort of thing.

I’m not that keen on having this in the codebase, but I do agree that as a workaround it’s ok for now. As soon as the ZFS bug is resolved I’d like to deprecate our workaround though.

We will have to make sure it’s clear that this is a workaround, and should only be enabled if creating a separate dataset is undesireable for whatever reason.

This somehow seems to be related to zfs_txg_timeout (https://openzfs.github.io/openzfs-docs/Performance and Tuning/ZIO Scheduler.html), when changing that to 20s I observed the delays start being random between 0-20s. (But watching various zpool iostats I never saw any queued transaction groups).

Sorta unrelated to this issue: YES! The quality of communication and willingness to look at peoples’ interesting setups is really something.

Excited for the new daemon so I can start using Atuin again! 👍

It’s out now as part of v18.3.0! Still experimental, but safe to use

Docs: https://docs.atuin.sh/reference/daemon/

Oh yea, I should also point out that since this is caused by ZFS sync then the easy solution is to just set sync=disabled for the dataset that the local atuin DB resides on. It’s not something Atuin can do itself but if you’re willing to fix it on your end then it should be fairly easy.

I’m not sure exactly what’s going on (since my assumption was that ZFS would prioritize the synchronous write, but maybe that’s wrong) but disabling sync does seem to resolve the problem for me. Here’s an article if you want to get a better understanding of what that actually is: https://jrs-s.net/2019/05/02/zfs-sync-async-zil-slog/

Because it does cost some safety, you’ll probably want a separate dataset just for the atuin DB to limit exposure in case of a failure.

I’m seeing this as well. I did an strace on the shell and there’s an ftruncate call on history.db-shm that sometimes takes several seconds, and so seems to be the culprit.

I am using ZFS as well, and moving the history db to a tmpfs resolves. Switching to a Kernel with PREEMPT=y did not solve. There is a ZFS bug report already about SQLite and ftruncate (https://github.com/openzfs/zfs/issues/14290).

I think it’s reasonable to conclude that this isn’t Atuin’s doing.

@ellie All good, I would just have expected to be notified about it in some way.

@happenslol - sorry about that, i didn’t realise it would hide the edit attribution 😬

The edit was mostly just because I had to keep referring people to this issue, and keep reiterating that there’s not really anything we can do short of suggesting they use the daemon

@softwarecreations Hey, I’m sorry to hear that. That suggestion did actually not come from me, as you can see in the edit history it was added by the author of this project. I do not use atuin anymore, so I can’t give you any pointers, and I don’t wanna overwrite the edits that the project maintainer made to my post. I’ll edit my post to clarify that those are not my suggestions.

When I last used atuin, I did use a completely separate dataset with sync=disabled just for the atuin database, and that worked fine.

@hvisage I’m afraid that your requirements aren’t really something we can support. If OpenZFS fixes their bug, then all should be fine - but until that point, some sort of workaround is required.

so it does become a, challenge with the daemon popping up everywhere now

Could you elaborate a bit here? We technically already run a short-lived background process on every single command, the daemon is just formalizing that a little. I’m not too sure how it’s now “popping up everywhere”.

If there is a way to force a PRAGMA=asyncronous/unsafe type to not have the synchronous writes (and here I will content that shell history is an assistance, not a requirement, besides the idea for me is much more to get the sync to a server going, there it’ll make sense/simple to have ext4 on it)

If blocking for 5s, running the daemon, or creating a zvol is unacceptable - you could patch the code.

It’s a one/two line change, many examples in this thread. I’m not willing to have any combination of options that leads to corrupt databases and data loss though so you will have to maintain that patch. Note that the risk there isn’t that the database vanishes and resyncs from time to time, but more that it will just halt and error out until you manually delete it.

Reading the documentation to me it reads that at some point atuin will be able to autostart itself as a daemon, so in that case you wouldn’t need to starting daemons yourself anymore - also you can probably put this in your shell configuration as of today already. With autostart, it would be nice if atuin could also autostop itself. This might be a bit tricky to do race condition free. The best way I know is using socket socket activation as supported in systemd on Linux and launchd on macOS. Here is how it works:

  1. The supervisor* binds to the atuin socket
  2. When we have a client, the supervisor will start the atuin daemon
  3. The atuin daemon will stop after a timeout when there are no more requests coming in

The crucial thing is that the supervisor keeps the socket open at all time and connection attempts are cached until the daemon responds, which has the advantage that no client will ever run into the case where a socket is not responsive. Autostopping also gives us a nice way to upgrade atuin when there is a new version around.

*systemd or launchd

UPDATE: nice systemd socket activation is already implemented: https://github.com/atuinsh/atuin/pull/2039

Excited for the new daemon so I can start using Atuin again! 👍

@ellie I’m not @timlinux, but I’ve been using the workaround from https://github.com/atuinsh/atuin/issues/952#issuecomment-1902164562 for a few months and it’s been working great.

I resolved the issue by creating a tmpfs on which the database is saved, so I’m just bypassing ZFS by saving it in RAM. Then, I have a litestream service running in the background, which syncs the database back to the filesystem continuously and restores it on first startup. Probably a little overkill, but it was really easy to set up. Hit me up if you want to see how I did it.

For those interested in this approach, here’s what I did to replicate this. No guarantees it works for you or on your system. I installed litestream and screen, changed by atuin config to point to /tmp/${USER}-atuin-db/history.db, then modified my .bashrc from

[[ -f ~/.bash-preexec.sh ]] && source ~/.bash-preexec.sh
eval "$(atuin init bash)"

to

function fix_atuin_zfs() {
  # Can remove after https://github.com/openzfs/zfs/issues/14290 gets fixed
  # also need to set ~/.config/atuin/config.toml:db_path = /tmp/$USER-atuin-db/history.db (replace $USER with your username in config.toml)
  tmpfs_db_path="/tmp/$USER-atuin-db"
  tmpfs_db_file="$tmpfs_db_path/history.db"
  litestream_backup_path="$HOME/.local/share/atuin/history-db-litestream"
  if mkdir "$tmpfs_db_path" 2>/dev/null; then
    # Need to copy over the DB to tmp dir + run litestream
    if [ -d "$litestream_backup_path" ]; then
      # We've already been using litestream, use it as the source of truth for history.db
      litestream restore -o "$tmpfs_db_file" "file://$litestream_backup_path" > /dev/null 2>&1
    else
      # Migrate over the initial history.db from atuin to tmpfs
      cp ~/.local/share/atuin/history.db* "$tmpfs_db_path/"
    fi
    # Run litestream replication in the background
    screen -S litestream-atuin-$USER -dm litestream replicate "$tmpfs_db_file" "file://$litestream_backup_path"
  fi
}

fix_atuin_zfs
[[ -f ~/.bash-preexec.sh ]] && source ~/.bash-preexec.sh
eval "$(atuin init bash)"

No kink-shaming, but this is what I am testing now: https://github.com/Mic92/dotfiles/blob/a7004dd28161bf9bda37d0e7b11a114e90e69155/home-manager/pkgs/atuin/0001-make-atuin-on-zfs-fast-again.patch

Actually synchronous was not enough the problem comes from the WAL journal, which is the only operation that seems to use ftruncate in sqlite. After switching to the in-memory journal option responsiveness becomes better: https://github.com/Mic92/dotfiles/blob/main/home-manager/pkgs/atuin/0001-make-atuin-on-zfs-fast-again.patch I haven’t tried re-enabling synchronous after that. I personally don’t mind potentially corrupting my database this way as I have autosnapshots every 15 min, but I can see how as a project maintainer you don’t have want have to deal with issues coming from using weird data corruptions.

Ah that’s the setting I wasn’t able to find. That should be doing the exact same thing as sync=disabled, just on the atuin side.

@ellie @conradludgate - would you be willing to take this change after some more testing? As an explanation from the sqlite docs

With synchronous OFF (0), SQLite continues without syncing as soon as it has handed data off to the operating system. If the application running SQLite crashes, the data will be safe, but the database might become corrupted if the operating system crashes or the computer loses power before that data has been written to the disk surface. On the other hand, commits can be orders of magnitude faster with synchronous OFF.

In my view there is limited risk in the database becoming corrupt, because the client can always resync from the server.

I’d rather not enable this for everyone - reducing the reliability of Atuin even in edge cases isn’t something I’d be that happy with. Particularly as ZFS users are in the minority, and honestly my experience with ZFS is that you do need to configure things for various applications and things don’t always work out of the box. I feel extra strongly about this given that this is due to a ZFS bug.

There are two options as I see it, doing both would work too

  1. We document this clearly, state the workaround @arcuru listed before of sync=disabled
  2. We add a nosync_writes somewhere in the config to enable this change. Make it opt in.

But otherwise, I am quite against compromising on safety unless there is no other choice. With the usage numbers we have, this will almost certainly affect at least a few people who have not synced, and I’d like to avoid losing history wherever possible. Also bear in mind not all systems running Atuin have reliable power, internet, etc.

Hey @dbaynard, I resolved the issue by creating a tmpfs on which the database is saved, so I’m just bypassing ZFS by saving it in RAM. Then, I have a litestream service running in the background, which syncs the database back to the filesystem continuously and restores it on first startup. Probably a little overkill, but it was really easy to set up. Hit me up if you want to see how I did it.

However, recently, the issue came back and I haven’t had the time to debug it yet. No idea what’s causing it, since my database is still in RAM - either it’s not actually writing to that database, or litestream is causing some delays, which I don’t think is the case… I can post another response here when I’ve had the time to properly debug it, though.