MCPcopy Create free account
hub / github.com/atomicdotdev/atomic / hold_turn_publication_lock

Function hold_turn_publication_lock

atomic-cli/src/commands/agent/hooks.rs:508–547  ·  view source on GitHub ↗
(
    repository_root: &std::path::Path,
)

Source from the content-addressed store, hash-verified

506/// killed Stop must release it by process death alone.
507pub(crate) struct TurnPublicationLock(std::fs::File);
508pub(crate) fn hold_turn_publication_lock(
509 repository_root: &std::path::Path,
510) -> CliResult<Option<TurnPublicationLock>> {
511 use fs2::FileExt;
512 let canonical = match atomic_repository::Repository::canonical_dot_dir(repository_root) {
513 Ok(dot) => dot,
514 Err(_) => return Ok(None),
515 };
516 let file = std::fs::OpenOptions::new()
517 .create(true)
518 .write(true)
519 .truncate(false)
520 .open(canonical.join("turn-publication.lock"))
521 .map_err(|error| CliError::Internal(anyhow!("failed to open publication lock: {error}")))?;
522 let start = std::time::Instant::now();
523 // This outer guard bypasses the orchestrator's lock acquisition. Use the
524 // same budget so both entry points honor the publication timeout setting.
525 let timeout = atomic_agent::turn::orchestrator::publication_timeout();
526 loop {
527 match file.try_lock_exclusive() {
528 Ok(()) => return Ok(Some(TurnPublicationLock(file))),
529 Err(error)
530 if error.kind() == std::io::ErrorKind::WouldBlock
531 || error.raw_os_error() == fs2::lock_contended_error().raw_os_error() =>
532 {
533 if start.elapsed() >= timeout {
534 return Err(CliError::Internal(anyhow!(
535 "timed out waiting for another Stop to publish; retry this Stop"
536 )));
537 }
538 std::thread::sleep(std::time::Duration::from_millis(10));
539 }
540 Err(error) => {
541 return Err(CliError::Internal(anyhow!(
542 "publication lock unavailable: {error}"
543 )))
544 }
545 }
546 }
547}
548
549pub(crate) fn guard_agent_boundary(
550 repository_root: &std::path::Path,

Callers 1

runMethod · 0.85

Calls 7

publication_timeoutFunction · 0.85
TurnPublicationLockClass · 0.85
truncateMethod · 0.80
openMethod · 0.45
writeMethod · 0.45
createMethod · 0.45
kindMethod · 0.45

Tested by

no test coverage detected