You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Severity: Enhancement (callers cannot initialize holder diagnostics after ownership without private descriptor access)
Summary
I would like filelock to provide a narrow post-acquisition hook that runs after
the native lock succeeds and before acquire() returns. The hook should receive
a borrowed descriptor while filelock retains ownership of its lifecycle.
This complements issue #592 and open PR #595. An opener callback controls
descriptor creation before ownership, but it cannot safely write PID,
generation, or diagnostic data that must belong only to the successful holder.
AcquireReturnProxy
returns the lock, not a documented borrowed file object or descriptor.
Open PR #595 proposes a pre-lock FileOpener(path, flags, mode) -> fd
contract. It does not add a post-lock initialization point.
Suggested fix
Add a keyword-only callback such as on_acquired(fd) with these invariants:
Invoke it only after the backend has acquired and validated the native lock.
Treat the descriptor as borrowed. Filelock remains responsible for unlock
and close.
If the callback fails, release the just-acquired lock, close the descriptor,
restore logical state, and propagate both errors if cleanup also fails.
Invoke it once per physical acquisition, not once per nested counter step.
Define sync and async behavior explicitly.
Document that the descriptor is borrowed and must not be closed by the
callback. If the callback may change file position or file contents, define
those semantics explicitly.
A higher-level metadata-writer API is also suitable if maintainers prefer not
to expose a borrowed descriptor at all.
Verification
The hook runs after a successful OS lock and before acquire() returns.
A timed-out contender never runs the hook.
Nested acquisition does not rewrite holder data.
Hook failure leaves no live lock or descriptor.
Holder diagnostics remain intact when another process times out.
I did not find an existing upstream issue or PR covering this exact post-lock
hook. Targeted searches for post acquisition descriptor hook and on_acquired descriptor found no duplicate. A search for FileOpener found
PR #595, which is related but covers pre-lock descriptor creation rather than
post-lock holder initialization.
Current workaround
Read the private _context.lock_file_fd after acquisition or duplicate the
backend _acquire() method. The first relies on an internal field; the second
duplicates contention, cleanup, and platform behavior.
Severity: Enhancement (callers cannot initialize holder diagnostics after ownership without private descriptor access)
Summary
I would like filelock to provide a narrow post-acquisition hook that runs after
the native lock succeeds and before
acquire()returns. The hook should receivea borrowed descriptor while filelock retains ownership of its lifecycle.
This complements issue #592 and open PR #595. An opener callback controls
descriptor creation before ownership, but it cannot safely write PID,
generation, or diagnostic data that must belong only to the successful holder.
Environment
filelock 3.29.71efb8932c08789deb08ad93a91d8996cc36bb9ccFileLockcallers to provide a backward-compatible file opener #592 and open PR ✨ feat(api): add optional lock-file opener hook #595Reproduction
FileLock.callback that is guaranteed to run after the OS lock succeeds.
acquire()returns a proxy or lock object, while the descriptorexists only in the private
_context.lock_file_fdfield.ownership and therefore cannot provide the missing post-lock timing.
Observed vs expected
Observed:
Expected:
Root cause (verified in source, 3.29.7)
FileLockContext.lock_file_fdis the only stored native descriptor and is part of the private context.
AcquireReturnProxyreturns the lock, not a documented borrowed file object or descriptor.
Open PR #595 proposes a pre-lock
FileOpener(path, flags, mode) -> fdcontract. It does not add a post-lock initialization point.
Suggested fix
Add a keyword-only callback such as
on_acquired(fd)with these invariants:and close.
restore logical state, and propagate both errors if cleanup also fails.
callback. If the callback may change file position or file contents, define
those semantics explicitly.
A higher-level metadata-writer API is also suitable if maintainers prefer not
to expose a borrowed descriptor at all.
Verification
acquire()returns.I did not find an existing upstream issue or PR covering this exact post-lock
hook. Targeted searches for
post acquisition descriptor hookandon_acquired descriptorfound no duplicate. A search forFileOpenerfoundPR #595, which is related but covers pre-lock descriptor creation rather than
post-lock holder initialization.
Current workaround
Read the private
_context.lock_file_fdafter acquisition or duplicate thebackend
_acquire()method. The first relies on an internal field; the secondduplicates contention, cleanup, and platform behavior.