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:20This is due to an issue with ZFS and SQLite. See: https://github.com/openzfs/zfs/issues/14290
There are two workarounds
- 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
- 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
- Add configuration flag to control SQLite synchronous mode To help work around https://github.com/ellie/atuin/issues/952 — committed to mattico/atuin by mattico 3 years ago
- Add configuration flag to control SQLite synchronous mode To help work around https://github.com/ellie/atuin/issues/952 — committed to sysedwinistrator/atuin by mattico 3 years ago
- Apply ZFS patch https://github.com/Mic92/dotfiles/blob/9f8bc505fee6eb8aad175a987bee4cb84970d03d/home-manager/pkgs/atuin/0001-make-atuin-on-zfs-fast-again.patch https://github.com/atuinsh/atuin/issue... — committed to ednolan/atuin by enolan-maystreet 3 years ago
- Apply ZFS patch https://github.com/Mic92/dotfiles/blob/9f8bc505fee6eb8aad175a987bee4cb84970d03d/home-manager/pkgs/atuin/0001-make-atuin-on-zfs-fast-again.patch https://github.com/atuinsh/atuin/issue... — committed to ednolan/atuin by enolan-maystreet 3 years ago
- Improve performance on ZFS Ongoing conversation: https://github.com/atuinsh/atuin/issues/952 — committed to bohrshaw/atuin by bohrshaw 3 years ago
- Apply ZFS patch https://github.com/Mic92/dotfiles/blob/9f8bc505fee6eb8aad175a987bee4cb84970d03d/home-manager/pkgs/atuin/0001-make-atuin-on-zfs-fast-again.patch https://github.com/atuinsh/atuin/issue... — committed to ednolan/atuin by ednolan 3 years ago
- Apply ZFS patch https://github.com/Mic92/dotfiles/blob/9f8bc505fee6eb8aad175a987bee4cb84970d03d/home-manager/pkgs/atuin/0001-make-atuin-on-zfs-fast-again.patch https://github.com/atuinsh/atuin/issue... — committed to ednolan/atuin by ednolan 3 years ago
- Apply ZFS patch https://github.com/Mic92/dotfiles/blob/9f8bc505fee6eb8aad175a987bee4cb84970d03d/home-manager/pkgs/atuin/0001-make-atuin-on-zfs-fast-again.patch https://github.com/atuinsh/atuin/issue... — committed to ednolan/atuin by ednolan 3 years ago
- fix(zfs/sqlite): try workaround zfs ftruncate bug that trips up sqlite https://github.com/atuinsh/atuin/issues/952 https://github.com/openzfs/zfs/issues/14290 https://github.com/Mic92/dotfiles/blob... — committed to pweldon/atuin by pweldon 2 years ago
- fix(zfs/sqlite): try workaround zfs ftruncate bug that trips up sqlite https://github.com/atuinsh/atuin/issues/952 https://github.com/openzfs/zfs/issues/14290 https://github.com/Mic92/dotfiles/blob... — committed to pweldon/atuin by pweldon 2 years ago
- fix(zfs/sqlite): try workaround zfs ftruncate bug that trips up sqlite https://github.com/atuinsh/atuin/issues/952 https://github.com/openzfs/zfs/issues/14290 https://github.com/Mic92/dotfiles/blob... — committed to geedrius/atuin by pweldon 2 years ago
- Apply ZFS patch https://github.com/Mic92/dotfiles/blob/9f8bc505fee6eb8aad175a987bee4cb84970d03d/home-manager/pkgs/atuin/0001-make-atuin-on-zfs-fast-again.patch https://github.com/atuinsh/atuin/issue... — committed to ednolan/atuin by ednolan 3 years ago
- feat: re-provision Brillig using ext4 This is because https://github.com/atuinsh/atuin/issues/952 has revealed some issues with ZFS and SQLite that make me realise I don't have the time to learn a ne... — committed to bjeanes/dotfiles by bjeanes 2 years ago
- Apply ZFS patch https://github.com/Mic92/dotfiles/blob/9f8bc505fee6eb8aad175a987bee4cb84970d03d/home-manager/pkgs/atuin/0001-make-atuin-on-zfs-fast-again.patch https://github.com/atuinsh/atuin/issue... — committed to ednolan/atuin by enolan-maystreet 3 years ago
- Apply ZFS patch https://github.com/Mic92/dotfiles/blob/9f8bc505fee6eb8aad175a987bee4cb84970d03d/home-manager/pkgs/atuin/0001-make-atuin-on-zfs-fast-again.patch https://github.com/atuinsh/atuin/issue... — committed to ednolan/atuin by enolan-maystreet 3 years ago
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.
Thank you! Always nice to hear that ❤️
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
to your config file
atuin daemonsomewhere. 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 hereComplete 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
Conditional Configuration
In my shell startup script:
Systemd Service Setup Script (
scripts/setup-atuin-daemon.sh)Config Files
Standard config (
configs/atuin/config.toml)ZFS-specific config (
configs/atuin/zfs/config.toml)How to Use
scripts/setup-atuin-daemon.shto set up and start the daemonThis 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.
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:
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.
😭
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) 😭
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
Let us know if we can do anything to help!
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 variouszpool iostatsI 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.
It’s out now as part of v18.3.0! Still experimental, but safe to use
Docs: https://docs.atuin.sh/reference/daemon/
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
Oh yea, I should also point out that since this is caused by ZFS sync then the easy solution is to just set
sync=disabledfor 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
ftruncatecall onhistory.db-shmthat 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=disabledjust 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.
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 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:
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.
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
litestreamandscreen, changed by atuin config to point to/tmp/${USER}-atuin-db/history.db, then modified my.bashrcfromto
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
synchronousafter 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.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
sync=disablednosync_writessomewhere 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.
After two days of testing, I no longer see hangs with https://github.com/Mic92/dotfiles/blob/a7004dd28161bf9bda37d0e7b11a114e90e69155/home-manager/pkgs/atuin/0001-make-atuin-on-zfs-fast-again.patch
Hey @dbaynard, I resolved the issue by creating a
tmpfson 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.