Skip to content

Commit bd857e9

Browse files
committed
feat(sync): Add inter-process locking for background refresh operations
1 parent bf25a86 commit bd857e9

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

crates/but/src/command/legacy/refresh.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use but_core::sync::LockScope;
12
use std::fmt::Write;
23

34
pub fn handle(
@@ -7,6 +8,12 @@ pub fn handle(
78
prs: bool,
89
ci: bool,
910
) -> anyhow::Result<()> {
11+
// Obtain a lock to prevent concurrent background refreshes
12+
let _exclusive_access = but_core::sync::try_exclusive_inter_process_access(
13+
&ctx.gitdir,
14+
LockScope::BackgroundRefreshOperations,
15+
)?;
16+
1017
if fetch {
1118
out.write_str("\nFetching from remotes...")?;
1219
let fetch_result = but_api::legacy::virtual_branches::fetch_from_remotes(

crates/but/src/init.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use std::fmt::Write;
22

3+
use but_core::sync::LockScope;
34
use but_ctx::Context;
45
use command_group::AsyncCommandGroup;
56

@@ -114,7 +115,15 @@ pub fn init_ctx(
114115
true // Never fetched before, force fetch
115116
};
116117

117-
if should_fetch {
118+
// Check if there is a process still doing background refreshes
119+
let exclusive_access = but_core::sync::try_exclusive_inter_process_access(
120+
&ctx.gitdir,
121+
LockScope::BackgroundRefreshOperations,
122+
)
123+
.ok();
124+
125+
if should_fetch && exclusive_access.is_some() {
126+
drop(exclusive_access); // Release the lock immediately so that the new child process can acquire it
118127
let binary_path = std::env::current_exe().unwrap_or_default();
119128
let proc = tokio::process::Command::new(binary_path.clone())
120129
.arg("-C")

0 commit comments

Comments
 (0)