Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
57 commits
Select commit Hold shift + click to select a range
7af3067
fix(node): close the advisory-lock probe's session if it is dropped m…
beardthelion Jul 29, 2026
edcf0f2
fix(node): add a dedicated, lazily-connected advisory-lock pool
beardthelion Jul 29, 2026
5ce3c7f
fix(node): hold the lock-owning connection in RepoWriteGuard (#279)
beardthelion Jul 29, 2026
c49d3cd
fix(node): free the advisory lock when a write guard dies without rel…
beardthelion Jul 29, 2026
98f2daf
fix(node): read the advisory unlock's result instead of discarding it
beardthelion Jul 29, 2026
9321d3b
fix(node): bound the object-storage transfers that run under the writ…
beardthelion Jul 29, 2026
9da0939
fix(node): authorize before taking the write lock in close_issue
beardthelion Jul 29, 2026
5d254fb
fix(node): address the review findings on the advisory-lock series
beardthelion Jul 30, 2026
d346bde
fix(node): log lock-pool saturation in the request path, not via read…
beardthelion Jul 30, 2026
82143ad
fix(node): refuse a write when object storage is unknowable, and shed…
beardthelion Jul 30, 2026
8cf6b7a
fix(node): re-authorize close_issue under the guard instead of only r…
beardthelion Jul 30, 2026
af5948b
style(node): rustfmt the new contention test
beardthelion Jul 30, 2026
e5558fb
fix(node): shed an unrefreshable write as a retryable 503 with a fixe…
beardthelion Aug 3, 2026
63a9ef8
fix(node): refuse a fresh-copy write when the archive HEAD cannot be …
beardthelion Aug 3, 2026
10230ac
fix(node): log expected transient acquire failures at warn, not error
beardthelion Aug 3, 2026
259d587
fix(node): log the read failure the close_issue pre-check folds into …
beardthelion Aug 3, 2026
999ee72
test(node): poll for the closed session instead of sleeping a fixed 3…
beardthelion Aug 3, 2026
5fdb902
fix(node): raise a failed fresh download with no local copy as RepoUn…
beardthelion Aug 9, 2026
f81ebbe
test(node): assert the second writer sheds as RepoBusy instead of tim…
beardthelion Aug 9, 2026
c4dbd8a
fix(node): read the close_issue author pre-check from a non-mutating …
beardthelion Aug 10, 2026
9f313b9
fix(node): bound every await in the lock-acquire loop by the deadline
beardthelion Aug 10, 2026
60d70c7
test(node): add an S3 mock that really enforces conditional writes
beardthelion Aug 10, 2026
31f90c4
fix(node): stop pretending a held lock fences a late upload
beardthelion Aug 10, 2026
3eabf79
feat(node): let the storage client express and detect a conditional w…
beardthelion Aug 10, 2026
317841e
fix(node): fence the release publish so an abandoned upload cannot win
beardthelion Aug 10, 2026
3d803ff
test(node): drive the abandoned-upload race end to end, and probe the…
beardthelion Aug 10, 2026
2a92a1f
fix(node): reconcile F-series tests and test seams with the rebased g…
beardthelion Aug 18, 2026
9f35dca
fix(node): address the 2026-08-10 review round on the advisory-lock s…
beardthelion Aug 18, 2026
9dd71e9
fix(node): record why the close-issue read gate cannot carry a synced…
beardthelion Aug 18, 2026
4cee7e0
merge origin/main into fix/279-advisory-lock-session-affinity
beardthelion Aug 26, 2026
b35cb31
fix(node): address jatmn review on merged advisory-lock head
beardthelion Aug 26, 2026
3c20148
fix(node): cap close_issue snapshot work on the read pool
beardthelion Aug 26, 2026
7ba0fef
fix(node): mark close_issue mirror visibility gate for push hook
beardthelion Aug 26, 2026
3df71c9
fix: unblock pre-push after main merge
beardthelion Aug 26, 2026
98fe670
fix(node): carry transient acquire failures through every write handler
beardthelion Aug 28, 2026
4f1c1bb
fix(node): drop unused logcap helper after drain_faults seam
beardthelion Aug 28, 2026
4eb0c44
fix(node): route fork disk paths through validated_repo_disk_path
beardthelion Aug 28, 2026
d1ce2e3
fix(node): harden release-upload outcomes and replication tail gating
beardthelion Aug 28, 2026
8e66e77
fix(node): close jatmn round on publish authority and durability gating
beardthelion Aug 29, 2026
1911291
fix(node): harden issue close/create and fork publish paths on #285
beardthelion Aug 29, 2026
5d3a4ea
fix(node): close write-attempt ownership gaps on advisory-lock release
beardthelion Aug 29, 2026
2ad2f12
fix(node): gate replication tail on publish durability and split clos…
beardthelion Aug 29, 2026
487687c
fix(node): add ValidatedRepoDiskPath for CodeQL and reconcile tail du…
beardthelion Aug 29, 2026
9d02139
fix(node): close three jatmn findings on advisory-lock PR
beardthelion Aug 30, 2026
85c6f48
fix(node): verify adversarial findings on advisory-lock PR
beardthelion Aug 30, 2026
5cd6703
fix(node): compensate under the write lock and harden tail durability…
beardthelion Aug 30, 2026
22da2d6
fix(node): refuse stale under-lock refresh and gate tail durability o…
beardthelion Aug 30, 2026
c306933
style(node): rustfmt advisory-lock gap-closure commits
beardthelion Aug 30, 2026
3a82ccb
fix(node): satisfy clippy on stale-local download test path join
beardthelion Aug 30, 2026
c40f299
feat(node): add a publish attempt/stage boundary
kevincodex1 Aug 31, 2026
21646bc
feat(node): stamp uploads with an attempt id and preserve dispatch am…
kevincodex1 Aug 31, 2026
5e548d2
fix(node): key durability, cache validity and fork cleanup on the att…
kevincodex1 Aug 31, 2026
535c5dc
fix(node): settle the cache contract on the abandoned path and the at…
beardthelion Sep 7, 2026
f048a1e
fix(node): settle the abandoned tree at the disconnect, not at reaper…
beardthelion Sep 7, 2026
957d50b
fix(node): let exactly one classifier settle an abandoned tree
beardthelion Sep 7, 2026
892c752
Merge origin/main into fix/279-advisory-lock-session-affinity
beardthelion Nov 4, 2026
28f3fc2
test(node): use the test's own pool now that Db::pool is gone
beardthelion Nov 4, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fix(node): read the close_issue author pre-check from a non-mutating …
…snapshot

The non-owner author fallback refreshed through acquire_fresh, which publishes
into the live repo directory: its extract step removes the existing directory
and renames the new one into place. That runs with no write lock held, so any
signed non-owner could trigger a directory swap underneath an in-flight
guarded write on the same path.

read_snapshot downloads to a throwaway temp dir and hands back a RepoSnapshot
that removes it on drop, so the pre-check still sees fresh data and the live
path is never touched. download_to grows a publish flag to serve both shapes
from one path.

The wedge invariant still holds: a stranger is refused without waiting on the
write lock.
  • Loading branch information
beardthelion committed Aug 18, 2026
commit c4dbd8a7976cfe3f645c019c8cb8ad1e6cfabc44
37 changes: 21 additions & 16 deletions crates/gitlawb-node/src/api/issues.rs
Original file line number Diff line number Diff line change
Expand Up @@ -239,27 +239,32 @@ pub async fn close_issue(
// Not the owner, so the author fallback decides it, and the author lives in
// the issue's git-JSON blob rather than a DB column.
//
// Read it WITHOUT the write lock. The justification is NOT that authorship
// is immutable — it is not: `refs/gitlawb/**` is pushable, so a forged
// author blob can be pushed (tracked separately; it is what makes this
// fallback only as trustworthy as push authorization). The justification is
// that this read is only a PRE-CHECK, deciding whether to take the lock at
// all. It is NOT the authorization decision: `acquire_write` re-downloads the
// archive after locking, so the tree that gets mutated is routinely not this
// one, and the authoritative owner-or-author check runs again under the guard
// below. Refusing here early just keeps a caller who is already visibly
// Read it WITHOUT the write lock, from a NON-MUTATING SNAPSHOT. The
// justification is NOT that authorship is immutable — it is not:
// `refs/gitlawb/**` is pushable, so a forged author blob can be pushed
// (tracked separately; it is what makes this fallback only as trustworthy
// as push authorization). The justification is that this read is only a
// PRE-CHECK, deciding whether to take the lock at all. It is NOT the
// authorization decision: `acquire_write` re-downloads the archive after
// locking, so the tree that gets mutated is routinely not this one, and the
// authoritative owner-or-author check runs again under the guard below.
// Refusing here early just keeps a caller who is already visibly
// unauthorized from reaching the lock.
//
// `acquire_fresh`, not `acquire`: acquire's fast path returns as soon as the
// directory exists and never contacts object storage, so on a node with a
// stale copy the author's own issue would be invisible and the
// `read_snapshot`, not `acquire_fresh`: acquire's fast path returns as soon
// as the directory exists and never contacts object storage, so on a node
// with a stale copy the author's own issue would be invisible and the
// cannot-establish-authorship arm below would 403 a legitimate author.
// acquire_fresh refreshes first and still takes no lock.
let disk_path = state
// read_snapshot refreshes the same way, but unpacks into a throwaway temp
// dir instead of publishing into the live repo path — an unlocked
// pre-check must not delete or swap the directory under a concurrent
// guarded write on the same path.
let snapshot = state
.repo_store
.acquire_fresh(&record.owner_did, &record.name)
.read_snapshot(&record.owner_did, &record.name)
.await?;
let author_did: Option<String> = match git_issues::get_issue(&disk_path, &issue_id) {
let snapshot_path = snapshot.path().to_path_buf();
let author_did: Option<String> = match git_issues::get_issue(&snapshot_path, &issue_id) {
Ok(Some(raw)) => serde_json::from_str::<IssueRecord>(&raw)
.ok()
.and_then(|i| i.author),
Expand Down
188 changes: 188 additions & 0 deletions crates/gitlawb-node/src/git/repo_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,59 @@ impl RepoStore {
Ok(local_path)
}

/// Non-mutating snapshot of a repo's **latest** Tigris state, for reads that
/// must see fresh data but must NOT write into the live repo path.
///
/// Unlike `acquire_fresh`, which downloads and PUBLISHES into the live
/// directory (removing the existing dir and renaming the extract into
/// place), this unpacks into a throwaway temp dir and returns it. The live
/// path is never touched, so an unlocked caller cannot delete or swap the
/// directory under a concurrent guarded write.
///
/// The returned snapshot owns its temp dir and removes it on drop; when
/// there is no Tigris backend (or no archive), the snapshot borrows the live
/// local path and owns nothing. A HEAD failure refuses rather than guessing,
/// matching `acquire_fresh` and the under-lock refresh path: a transient
/// storage blip must be a retryable refusal (`RepoUnavailable`), not a 500
/// or a silently stale read.
pub async fn read_snapshot(&self, owner_did: &str, repo_name: &str) -> Result<RepoSnapshot> {
let (owner_slug, local_path) = self.local_path(owner_did, repo_name)?;

if let Some(ref tigris) = self.tigris {
match tigris.exists(&owner_slug, repo_name).await {
Ok(true) => {
// Snapshot form: unpack into a temp dir, never the live path.
let snapshot = tigris
.download_to(&owner_slug, repo_name, &local_path, false)
.await
.map_err(|e| {
anyhow::Error::new(RepoUnavailable).context(format!(
"tigris snapshot download failed during read_snapshot for {owner_slug}/{repo_name}: {e:#}"
))
})?;
return Ok(RepoSnapshot {
path: snapshot.clone(),
owned: true,
});
}
Ok(false) => {}
Err(e) => {
warn!(repo = %repo_name, err = %e,
"read_snapshot: tigris HEAD failed — refusing rather than guessing the archive is absent");
return Err(anyhow::Error::new(RepoUnavailable).context(format!(
"tigris HEAD failed during read_snapshot for {owner_slug}/{repo_name}"
)));
}
}
}

// Tigris disabled or repo not in Tigris — fall back to local.
Ok(RepoSnapshot {
path: local_path,
owned: false,
})
}

/// Take a write lock (Postgres advisory lock), ensure repo is local, return guard.
///
/// # Cross-machine guarantee
Expand Down Expand Up @@ -830,6 +883,29 @@ impl Drop for LockProbe {
}
}

/// Non-mutating snapshot of a repo's latest Tigris state. Owns the throwaway
/// temp dir it was unpacked into and removes it on drop; a snapshot that
/// borrowed the live local path owns nothing and drops as a no-op.
pub struct RepoSnapshot {
path: PathBuf,
owned: bool,
}

impl RepoSnapshot {
/// Path to the snapshot's bare repo directory.
pub fn path(&self) -> &Path {
&self.path
}
}

impl Drop for RepoSnapshot {
fn drop(&mut self) {
if self.owned {
let _ = std::fs::remove_dir_all(&self.path);
}
}
}

/// Guard returned by `acquire_write()`. Holds the Postgres advisory lock and
/// uploads to Tigris + releases the lock on `release()`.
pub struct RepoWriteGuard {
Expand Down Expand Up @@ -3003,4 +3079,116 @@ mod tests {
])
.is_err());
}

/// P1a: the non-owner pre-check must refresh from a NON-MUTATING snapshot.
/// A snapshot download must unpack into a throwaway temp dir and leave the
/// live repo path untouched, so an unlocked pre-check cannot delete or swap
/// the directory under a concurrent guarded write.
///
/// Real S3 server (not a mock): upload an archive, then `read_snapshot` it,
/// and assert the snapshot path is a fresh temp dir distinct from the live
/// path, that the live path was never created, and that the snapshot reads
/// the same content.
#[sqlx::test]
async fn read_snapshot_is_non_mutating(pool: PgPool) {
use axum::response::IntoResponse;

// A real in-process S3-compatible server via the SDK against an axum
// router is more plumbing than this test needs; instead, upload through
// the real Tigris client against an axum server that stores the object
// in memory, then snapshot through the same store.
//
// Simpler and equally load-bearing: build the archive bytes, serve them
// with a real HTTP server that answers HEAD 200 and GET with the bytes,
// then call read_snapshot and assert the live path is untouched and the
// snapshot content matches.
let mut archive_bytes = Vec::new();
{
let dir =
std::env::temp_dir().join(format!("gitlawb-snap-src-{}", uuid::Uuid::new_v4()));
std::fs::create_dir_all(dir.join("objects/info")).unwrap();
std::fs::create_dir_all(dir.join("refs/heads")).unwrap();
std::fs::write(dir.join("HEAD"), "ref: refs/heads/main\n").unwrap();
std::fs::write(dir.join("objects/info/packs"), "").unwrap();
let encoder = zstd::stream::Encoder::new(&mut archive_bytes, 3).unwrap();
let mut tar = tar::Builder::new(encoder);
tar.append_dir_all(".", &dir).unwrap();
tar.into_inner().unwrap().finish().unwrap();
std::fs::remove_dir_all(&dir).unwrap();
}
let archive = std::sync::Arc::new(archive_bytes);

let app = axum::Router::new().route(
"/{*key}",
axum::routing::any(move |method: axum::http::Method| {
let archive = archive.clone();
async move {
match method {
axum::http::Method::HEAD => axum::http::StatusCode::OK.into_response(),
axum::http::Method::GET => {
use axum::body::Body;
(
[(axum::http::header::CONTENT_TYPE, "application/zstd")],
Body::from(archive.as_ref().clone()),
)
.into_response()
}
_ => axum::http::StatusCode::METHOD_NOT_ALLOWED.into_response(),
}
}
}),
);
let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
let endpoint = format!("http://127.0.0.1:{}", listener.local_addr().unwrap().port());
let server = tokio::spawn(async move {
axum::serve(listener, app).await.unwrap();
});

let opts = (*pool.connect_options()).clone();
let lock_pool = no_reap_pool(&opts, 2).await;
let store = RepoStore::for_testing_with_tigris(
PathBuf::from("/tmp/gitlawb-snapshot-nonmut"),
lock_pool,
TigrisClient::for_testing_with_endpoint("test-bucket", &endpoint),
);

let owner_did = "did:key:z6MkSnap";
let (owner_slug, live_path) = store.local_path(owner_did, "snaprepo").unwrap();
assert!(
!live_path.exists(),
"the live path must not exist before the snapshot"
);

let snap = store
.read_snapshot(owner_did, "snaprepo")
.await
.expect("snapshot reads the archive");
let snap_path = snap.path().to_path_buf();
assert_ne!(
snap_path, live_path,
"the snapshot must unpack into a temp dir, not the live path"
);
assert!(
snap_path.starts_with(live_path.parent().unwrap()),
"the snapshot temp dir must live under the repo parent"
);
assert!(
!live_path.exists(),
"the live path must remain untouched by a snapshot read"
);
assert_eq!(
std::fs::read_to_string(snap_path.join("HEAD")).unwrap(),
"ref: refs/heads/main\n",
"the snapshot must contain the archive's content"
);
drop(snap);
assert!(
!snap_path.exists(),
"dropping the snapshot must clean up its temp dir"
);
let _ = owner_slug;

server.abort();
}

}
63 changes: 56 additions & 7 deletions crates/gitlawb-node/src/git/tigris.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,28 @@ impl TigrisClient {
repo_name: &str,
local_path: &Path,
) -> Result<()> {
self.download_to(owner_slug, repo_name, local_path, true)
.await
.map(|_| ())
}

/// Download a repo archive from Tigris and extract it, returning the
/// directory that was populated.
///
/// `publish` controls whether the extract is swapped into `target` in place
/// (the live-path mutation used by writes; returns `target`) or unpacked
/// into a fresh temp directory under `target`'s parent (a non-mutating
/// snapshot read; returns the temp dir, which the caller owns and cleans
/// up). The snapshot form never touches the live repo path.
pub async fn download_to(
&self,
owner_slug: &str,
repo_name: &str,
target: &Path,
publish: bool,
) -> Result<PathBuf> {
let key = Self::repo_key(owner_slug, repo_name);
debug!(key = %key, path = %local_path.display(), "downloading repo from tigris");
debug!(key = %key, path = %target.display(), "downloading repo from tigris");

let resp = self
.s3
Expand All @@ -141,17 +161,46 @@ impl TigrisClient {
.context("reading tigris response body")?
.into_bytes();

// Extract tar.zst to local path
tokio::task::spawn_blocking({
let local_path = local_path.to_path_buf();
move || decompress_repo(&data, &local_path)
// Extract tar.zst to a directory.
let extracted = tokio::task::spawn_blocking({
let target = target.to_path_buf();
move || -> Result<PathBuf> {
if publish {
decompress_repo(&data, &target)?;
return Ok(target);
}
// Non-mutating snapshot: unpack into a fresh temp dir under the
// target's parent. The live repo path is never touched.
let parent = target.parent().context("snapshot path has no parent")?;
std::fs::create_dir_all(parent).context("creating parent dir")?;
let file_name = target
.file_name()
.context("snapshot path has no file name")?
.to_string_lossy();
let tmp_dir = parent.join(format!(
".{file_name}.tmp-snapshot.{}",
uuid::Uuid::new_v4()
));
std::fs::create_dir_all(&tmp_dir).context("creating temp extract dir")?;
let unpack = (|| -> Result<()> {
let decoder = zstd::stream::Decoder::new(&data[..])?;
let mut archive = tar::Archive::new(decoder);
archive.unpack(&tmp_dir).context("unpacking tar.zst")?;
Ok(())
})();
if let Err(e) = unpack {
let _ = std::fs::remove_dir_all(&tmp_dir);
return Err(e);
}
Ok(tmp_dir)
}
})
.await
.context("extract task panicked")?
.context("extracting repo")?;

info!(key = %key, path = %local_path.display(), "downloaded repo from tigris");
Ok(())
info!(key = %key, path = %target.display(), "downloaded repo from tigris");
Ok(extracted)
}

/// Delete a repo archive from Tigris.
Expand Down