First forays into the sneakerweb
Today I spent a couple hours familiarizing myself with the sneakerweb code base. One thing that annoyed me as a user the first time
I experimented with it was that you can't have the CLI serve the sneakerweb, and at the same time import or export anything. Trying to do so would result in an error FjallError: Locked.
It turns out this error comes from the key-value database called Fjall that is used as a persistence store for Willow (on which the sneakerweb is build). This database locks itself when a process has opened it, so no other processes can open it when it's currently opened by another process. I believe this limitation exists because it can't currently guarantee there is only one process that writes to it (unlike sqlite which can do this).
I started looking into how I could make it so that we can serve and import/export at the same time. I quickly found that we open the database when we start the server process, which means that for the entire duration that
sneakerweb serve is running the database is locked. Immediately I thought it should probably only open the database during a request and close it when the request is done. This creates some more churn but then you
don't leave the database opened while nothing is even reading it. From experience I thought of the concept of lazy initialization, where we defer the initialization/opening of the database to once we're inside the request handling.
So I started looking at things like LazyCell, this was a bit of a detour because I couldn't get the compiler happy about it. Then I thought I'd write a little lazy wrapper struct myself which got me further along but I was still
struggling to get the compiler to like it.
Then I sat back and took a look at the assumption that I needed to create the PersistentStore in the first place, which turned out to not be necessary that early on. Instead we can pass along the directory in which the
data is actually stored, and initialize the PersistentStore during request handling. This results exactly in the behavior I wanted, the database isn't locked during sneakerweb serve, and we can freely export and
import sites while that is running.
SNAG!!
Unfortunately things weren't as rosy for the request handling itself, the front page which shows all of the sites I have only loads itself and the first iframe. It looks like the other iframes are racing for a lock on the database and end up
bailing out. So I can't see the previews of other sites, those now error out with that same FjallError: Locked.
Below you'll find my current patch on main, bugs included. I'd like to see if I can work around this problem too but I kind of doubt it.
From 4119168f4aefc2ff6c8c236a891cdc83d6acd9a6 Mon Sep 17 00:00:00 2001
From: Peter Maatman
Date: Fri, 4 Dec 2026 23:47:22 +0100
Subject: [PATCH] Initialize PersistentStore during request handling
---
src/serve/front_page.rs | 18 +++++++++++++++++-
src/serve/mod.rs | 13 ++++++-------
src/serve/user_domain.rs | 18 +++++++++++++++++-
3 files changed, 40 insertions(+), 9 deletions(-)
diff --git a/src/serve/front_page.rs b/src/serve/front_page.rs
index acd6db0..25a3e8a 100644
--- a/src/serve/front_page.rs
+++ b/src/serve/front_page.rs
@@ -8,6 +8,8 @@ use hypertext::Buffer;
use hypertext::component as htcomponent;
use hypertext::prelude::*;
use std::cmp::Reverse;
+use std::path::PathBuf;
+use std::rc::Rc;
use std::str::FromStr;
use tokio::sync::mpsc::Sender;
use tokio::sync::oneshot::{self, Sender as Reply};
@@ -32,12 +34,26 @@ pub async fn serve_front_page(
reply_channel,
order,
}: FrontPageRequest,
- mut store: PersistentStore,
+ store_dir: Rc
) {
// Render the homepage.
let mut buffer = Buffer::new();
+ let mut store = match PersistentStore::new(::clone(&*store_dir)).await {
+ Ok(store) => store,
+ Err(err) => {
+ let _ = reply_channel.send(Err((
+ StatusCode::INTERNAL_SERVER_ERROR,
+ format!(
+ "the sneakerweb store encountered an error while retrieving this content: {err}"
+ ),
+ )
+ .into_response()));
+ return;
+ }
+ };
+
let response = match render_frontpage(&mut store, order, &mut buffer).await {
Ok(_) => {
let mut response_headers = HeaderMap::new();
diff --git a/src/serve/mod.rs b/src/serve/mod.rs
index ec16280..a0a7fbf 100644
--- a/src/serve/mod.rs
+++ b/src/serve/mod.rs
@@ -1,4 +1,5 @@
use std::path::PathBuf;
+use std::rc::Rc;
use axum::{
Router, ServiceExt,
@@ -17,7 +18,6 @@ use tokio::{
task::{self, LocalSet},
};
use tower::{Layer, util::MapRequestLayer};
-use willow25::storage::PersistentStore;
use crate::util::*;
@@ -47,11 +47,9 @@ pub async fn serve(args: &ServeArgs) -> Result<()> {
let (sender, receiver) = channel(32);
let dir = sneakerweb_dir(args.collection.as_ref()).await?;
- let store = PersistentStore::new(dir).await?;
-
let local = LocalSet::new();
- local.spawn_local(process_requests(receiver, store));
+ local.spawn_local(process_requests(receiver, Rc::new(dir)));
let user_domain_router = Router::>::new()
.route("/", get(request_user_domain))
@@ -83,14 +81,15 @@ pub async fn serve(args: &ServeArgs) -> Result<()> {
Ok(())
}
-async fn process_requests(mut r: Receiver, store: PersistentStore) {
+async fn process_requests(mut r: Receiver, dir: Rc)
+{
while let Some(request) = r.recv().await {
match request {
SneakerwebRequest::UserDomain(domain_request) => {
- task::spawn_local(serve_domain(domain_request, store.clone()));
+ task::spawn_local(serve_domain(domain_request, dir.clone()));
}
SneakerwebRequest::FrontPage(front_page_request) => {
- task::spawn_local(serve_front_page(front_page_request, store.clone()));
+ task::spawn_local(serve_front_page(front_page_request, dir.clone()));
}
}
}
diff --git a/src/serve/user_domain.rs b/src/serve/user_domain.rs
index d96f098..836f6e6 100644
--- a/src/serve/user_domain.rs
+++ b/src/serve/user_domain.rs
@@ -1,4 +1,6 @@
use std::io;
+use std::path::PathBuf;
+use std::rc::Rc;
use super::request::*;
use crate::util::{BASE32_ALPHABET, NAMESPACE_ID};
@@ -206,11 +208,25 @@ pub async fn serve_domain(
fallback_path,
ranges,
}: DomainRequest,
- mut store: PersistentStore,
+ store_dir: Rc,
) {
let fallback_key = fallback_path.map(|path| (subspace.clone(), path));
let key = (subspace, path);
+ let mut store = match PersistentStore::new(::clone(&*store_dir)).await {
+ Ok(store) => store,
+ Err(err) => {
+ let _ = reply_channel.send(Err((
+ StatusCode::INTERNAL_SERVER_ERROR,
+ format!(
+ "the sneakerweb store encountered an error while retrieving this content: {err}"
+ ),
+ )
+ .into_response()));
+ return;
+ }
+ };
+
let entry = match store.get_entry(&NAMESPACE_ID, &key, None).await {
Ok(Some(entry)) => entry,
Ok(None) if let Some(fallback) = fallback_key => {
--
2.56.1