cadrumo.application.auth.acquisition_lock module¶
Crash-recoverable auth acquisition locks.
The lock protects live auth flows that can create external state,
especially Cl@ve Movil push petitions. It is intentionally
filesystem-backed so separate CLI processes share the same guard.
The lock file stores an AuthAcquisitionLockRecord and reports
operator-safe state through AuthAcquisitionLockStatus.
Removal goes through unlink_lockfile(): every peer that finds
the lock taken opens the file to read the record, and on Windows that open
refuses the owner’s delete. An abandoned delete would leave a record naming a
live process, blocking acquisition until the record’s TTL expires, so the
removal retries. It never raises – the release runs in a finally where a
raise would mask the auth flow’s own outcome, and the TTL is the backstop.
- class AuthAcquisitionLockState(*values)[source]¶
Bases:
StrEnumObservable states for the auth acquisition lock file.
- ABSENT¶
- HELD¶
- STALE¶
- CORRUPT¶
- class AuthAcquisitionLockRecord(**data)[source]¶
Bases:
BaseModelMetadata written into an auth acquisition lock file.
- Parameters:
data (Any)
- provider_kind: AuthProviderKind¶
- profile_name: str¶
- pid: int¶
- hostname: str¶
- created_at: datetime¶
- expires_at: datetime¶
- operation: str¶
- class AuthAcquisitionLockStatus(**data)[source]¶
Bases:
BaseModelSafe health/status view of an auth acquisition lock.
- Parameters:
data (Any)
- state: AuthAcquisitionLockState¶
- path: Path¶
- record: AuthAcquisitionLockRecord | None¶
- reason: str | None¶
- recoverable: bool¶
- exception AuthAcquisitionLockedError(*, status, translated_message)[source]¶
Bases:
CadrumoErrorRaised when another process is already acquiring AEAT auth.
- Parameters:
status (AuthAcquisitionLockStatus)
translated_message (str)
- Return type:
None
- property terminal_precondition_verdict: PreconditionVerdict¶
Expose the application-owned lock conflict to the generic CLI boundary.
- code¶
- auth_acquisition_lock_path(settings, kind, *, bucket_id=None)[source]¶
Return the profile/provider-scoped lock path.
- Return type:
Path- Parameters:
settings (Settings)
kind (AuthProviderKind)
bucket_id (str | None)
- inspect_auth_acquisition_lock(settings, kind, *, now=None, bucket_id=None)[source]¶
Describe the current acquisition-lock health without mutating it.
Returns an
AuthAcquisitionLockStatus.- Return type:
- Parameters:
settings (Settings)
kind (AuthProviderKind)
now (datetime | None)
bucket_id (str | None)
- clear_auth_acquisition_lock(settings, kind, *, reason='operator-reset', bucket_id=None, allow_held=False)[source]¶
Remove the acquisition lock and return the pre-reset status.
Returns an
AuthAcquisitionLockStatusreflecting the state observed immediately before the file was removed.A HELD lock refuses unless the caller passes
allow_held. This function used to delete on any state but ABSENT, so a live holder’s record was removed under the reasonstale_reclaimand the next acquisition succeeded while the first holder was still authenticating – two processes each believing they held the lock, which is a second petition to AEAT and a second prompt on the taxpayer’s phone. The compare-and-delete below does not catch it: it guards against the bytes CHANGING, and a live holder’s bytes are unchanged.Only a caller that is destroying the profile wholesale may pass
allow_held: there the lock is going with everything else, and refusing would strand the erase. A caller whose profile SURVIVES – an auth reset, orlogin --reset-lock– must not, because nothing there compensates for the aborted acquisition and the duplicate petition follows immediately.- Return type:
- Parameters:
settings (Settings)
kind (AuthProviderKind)
reason (str)
bucket_id (str | None)
allow_held (bool)
- acquire_auth_acquisition_lock(settings, kind, *, ttl_seconds, operation='auth-login')[source]¶
Acquire a crash-recoverable auth lock or raise a typed conflict.
Yields an
AuthAcquisitionLockRecordwhile the lock is held.Stale/corrupt locks are removed automatically before a second atomic-create attempt. A live lock is never waited on or retried: callers fail early so they do not issue a duplicate Cl@ve petition.
- Return type:
- Parameters:
settings (Settings)
kind (AuthProviderKind)
ttl_seconds (int)
operation (str)
- auth_lock_ttl_seconds(settings, kind)[source]¶
Return the acquisition-lock TTL for a provider.
- Return type:
- Parameters:
settings (Settings)
kind (AuthProviderKind)