Skip to content

feat(fs): add tryLock(exclusive?: boolean): Promise<boolean> to FsFile - #31848

Merged
dsherret merged 12 commits into
denoland:mainfrom
mkdev5:feat/trylock-fsfile
Feb 18, 2026
Merged

dsherret merged 12 commits into
denoland:mainfrom
mkdev5:feat/trylock-fsfile

Conversation

@mkdev5

@mkdev5 mkdev5 commented Jan 14, 2026 •

Copy link
Copy Markdown
Contributor

feat(fs): add tryLock method to Deno.FsFile

Closes #31343

Description

This PR adds a non-blocking file lock method to Deno.FsFile. Currently, lock() and lockSync() block until the lock is acquired, which can be problematic when you want to check if a file is available without waiting indefinitely.

The new tryLock() method attempts to acquire a lock and immediately returns a boolean indicating success, rather than blocking.

Changes

Added two new methods to Deno.FsFile:

// Async version - returns Promise<boolean>
await file.tryLock(exclusive?: boolean): Promise<boolean>

// Sync version - returns boolean  
file.tryLockSync(exclusive?: boolean): boolean

Behavior:

  • Returns true if the lock was successfully acquired
  • Returns false if the file is already locked (non-blocking, does not wait)
  • Throws on other errors (e.g., invalid file handle)

Example Usage

const file = await Deno.open("/tmp/myfile.txt", { read: true, write: true });

// Try to acquire an exclusive lock without blocking
if (await file.tryLock(true)) {
  console.log("Lock acquired, doing work...");
  // ... do work ...
  await file.unlock();
} else {
  console.log("File is busy, try again later");
}

file.close();

Implementation Details

  • Uses fs3::FileExt::try_lock_exclusive() and try_lock_shared() under the hood
  • Handles EWOULDBLOCK / WouldBlock errors by returning false instead of throwing
  • Follows the same pattern as existing lock()/lockSync() methods

Test Plan

Added unit tests in tests/unit/files_test.ts:

  • fsFileTryLockSync - verifies sync exclusive lock behavior
  • fsFileTryLockAsync - verifies async exclusive lock behavior
  • fsFileTryLockSharedSync - verifies shared locks can coexist

Checklist

Implements non-blocking file lock attempt for Deno.FsFile.

- Add tryLock() and tryLockSync() methods to FsFile
- Returns true on successful lock acquisition
- Returns false when lock cannot be acquired (non-blocking)
- Uses fs3::try_lock_exclusive/try_lock_shared under the hood

Closes #31343
@CLAassistant

CLAassistant commented Jan 14, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@mkdev5

mkdev5 commented Jan 14, 2026

Copy link
Copy Markdown
Contributor Author

@dsherret could you please have a look at my PR when you get a chance? I'm open to any feedback or suggestions. thanks!

Comment thread ext/fs/30_fs.js Resolved
Comment thread ext/io/lib.rs Resolved
@mkdev5

mkdev5 commented Jan 14, 2026

Copy link
Copy Markdown
Contributor Author

Do you have any other feedback for me?

@dsherret dsherret added this to the 2.7.0 milestone Jan 14, 2026
Comment thread ext/io/lib.rs Resolved

@dsherret dsherret left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, but not approving yet because we'll wait until closer to the 2.7 release to get this merged.

@mkdev5

mkdev5 commented Jan 15, 2026

Copy link
Copy Markdown
Contributor Author

I just saw 2.7.0 is scheduled for Feb 24 — do I have to wait until then?

@dsherret

Copy link
Copy Markdown
Contributor

We might merge it sooner, but we’ll have to wait a bit.

Ok, can I continue working on other issues?

For sure.

@mkdev5
mkdev5 requested a review from dsherret January 18, 2026 23:22
@mkdev5

mkdev5 commented Jan 20, 2026

Copy link
Copy Markdown
Contributor Author

@dsherret will close it for now. please let me know when you are ready for merging so that I can reopen the PR.

@dsherret

Copy link
Copy Markdown
Contributor

Why? It’s in the 2.7 milestone.

@mkdev5

mkdev5 commented Jan 20, 2026

Copy link
Copy Markdown
Contributor Author

I’ve been working on gittensor.io, which incentivizes contributors when their pull requests are merged. However, if a pull request is opened but not merged, I receive a penalty.

@mkdev5

mkdev5 commented Jan 20, 2026

Copy link
Copy Markdown
Contributor Author

@dsherret could you please let me know when you can merge it exactly?

@dsherret

dsherret commented Jan 20, 2026 •

Copy link
Copy Markdown
Contributor

I don't want to change our process for this or create additional an additional administration burden. We sometimes often change when minor releases happen.

You can consider closing this PR, but someone else might open a new PR in the meantime because they see at a glance that this one is closed.

I opened entrius/gittensor#141 to ask for the ability to exclude repositories form the open pr penalty.

@mkdev5

mkdev5 commented Jan 20, 2026

Copy link
Copy Markdown
Contributor Author

Okay, will leave it open for now

@dsherret dsherret left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Thanks!

@dsherret
dsherret enabled auto-merge (squash) February 18, 2026 15:11
@dsherret
dsherret merged commit 1d0c7cc into denoland:main Feb 18, 2026
166 of 169 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add the method tryLock(exclusive?: boolean) : Promise<boolean> into Deno.FsFile

3 participants