< Summary - Kestrun — Combined Coverage

Information
Class: Public.Locks.Use-KrLock
Assembly: Kestrun.PowerShell.Public
File(s): /home/runner/work/Kestrun/Kestrun/src/PowerShell/Kestrun/Public/Locks/Use-KrLock.ps1
Tag: Kestrun/Kestrun@971dd53fc1f17b61ce476aa4cec36c172d4f02e4
Line coverage
91%
Covered lines: 11
Uncovered lines: 1
Coverable lines: 12
Total lines: 77
Line coverage: 91.6%
Branch coverage
N/A
Covered branches: 0
Total branches: 0
Branch coverage: N/A
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Coverage history

Coverage history 0 25 50 75 100 04/08/2026 - 23:41:36 Line coverage: 91.6% (11/12) Total lines: 77 Tag: Kestrun/Kestrun@971dd53fc1f17b61ce476aa4cec36c172d4f02e4

Metrics

Method
Use-KrLock()

File(s)

/home/runner/work/Kestrun/Kestrun/src/PowerShell/Kestrun/Public/Locks/Use-KrLock.ps1

#LineLine coverage
 1<#
 2.SYNOPSIS
 3    Executes a script block while holding a named lock to ensure exclusive access to a resource.
 4.DESCRIPTION
 5    The Use-KrLock function allows you to execute a script block while holding a lock associated with a specified key.
 6    This is useful for synchronizing access to shared resources within the current application instance.
 7    The function retrieves a lock object using the lock registry and attempts to acquire the lock before executing the s
 8    If a timeout is specified and the lock cannot be acquired within that time frame, an error is thrown.
 9    After the script block is executed, the lock is released in a finally block to ensure that it happens even if an err
 10.PARAMETER Key
 11    The unique identifier for the lock. This key is used to retrieve the corresponding lock object from the lock registr
 12    If a lock object does not already exist for this key, a new one will be created. The key should be a string that uni
 13    section that the lock is intended to protect. It is important to use consistent keys across the application to ensur
 14.PARAMETER ScriptBlock
 15    The script block to execute while holding the lock. This is the code that will be run with exclusive access to the r
 16    The script block can contain any valid PowerShell code and can utilize the protected resource safely, knowing that i
 17.PARAMETER TimeoutMilliseconds
 18    The maximum time in milliseconds to wait for the lock before timing out. The default value is -1, which means to wai
 19    If a positive value is specified and the lock cannot be acquired within that time frame, an error will be thrown.
 20    This parameter allows you to control how long the function should wait for the lock, which can be useful in scenario
 21    indefinitely and prefer to handle lock acquisition failures gracefully.
 22.EXAMPLE
 23    Use-KrLock -Key "MyResourceLock" -ScriptBlock {
 24        # Code to execute while holding the lock
 25        Write-Host "This code is running with exclusive access to MyResourceLock."
 26    }
 27    This example demonstrates how to use the Use-KrLock function to execute a script block while holding a lock associat
 28    The code within the script block will have exclusive access to the resource protected by "MyResourceLock", ensuring 
 29.NOTES
 30    This function is part of the Kestrun framework and is used to manage locks for synchronizing access to shared resour
 31    It relies on a lock registry to store and retrieve lock objects based on their associated keys.
 32    The locks returned by this function can be used in conjunction with synchronization primitives such as Monitor, Mute
 33    Semaphore to control access to critical sections of code or shared resources in a thread-safe manner.
 34    It is important to ensure that the keys used with this function are consistent and unique to avoid unintended lockin
 35    Additionally, proper handling of lock acquisition and release is crucial to prevent deadlocks and ensure the smooth 
 36#>
 37function Use-KrLock {
 38    [KestrunRuntimeApi('Everywhere')]
 39    [CmdletBinding()]
 40    param(
 41        [Parameter(Mandatory = $true, Position = 0)]
 42        [ValidateNotNullOrEmpty()]
 43        [string]$Key,
 44
 45        [Parameter(Mandatory = $true, Position = 1)]
 46        [scriptblock]$ScriptBlock,
 47
 48        [Parameter()]
 49        [int]$TimeoutMilliseconds = -1
 50    )
 51
 152    $lock = [Kestrun.Utilities.KestrunLockRegistry]::GetOrCreate($Key)
 53
 154    $acquired = $false
 55
 56    try {
 157        if ($TimeoutMilliseconds -lt 0) {
 158            $lock.Wait()
 159            $acquired = $true
 60        } else {
 161            $acquired = $lock.Wait($TimeoutMilliseconds)
 162            if (-not $acquired) {
 163                throw "Timeout acquiring lock '$Key'"
 64            }
 65        }
 66
 167        return & $ScriptBlock
 68    } finally {
 169        if ($acquired) {
 70            try {
 171                $null = $lock.Release()
 72            } catch {
 073                Write-KrLog -Level Verbose -Message "Failed to release mutex '{Key}'" -Values $Key -Exception $_
 74            }
 75        }
 76    }
 77}

Methods/Properties

Use-KrLock()