From 4661c539980bafd6c41982ec923971c2caaa4bb6 Mon Sep 17 00:00:00 2001 From: mark roddy Date: Thu, 27 Nov 2025 08:48:48 -0500 Subject: [PATCH 1/3] fix deadlock, maybe, but add more logging too. --- versionfiles.ps1 | 30 ++++++++++++++++++++++++------ 1 file changed, 24 insertions(+), 6 deletions(-) diff --git a/versionfiles.ps1 b/versionfiles.ps1 index 9f45b5e..9c65cec 100644 --- a/versionfiles.ps1 +++ b/versionfiles.ps1 @@ -149,12 +149,19 @@ if (!(test-path -path $incPath)) { $verpropfile = "$($incPath)\version.props" $buildpropfile = "$($incPath)\buildnumber.props" [bool] $createdMutex = $false -[bool] $mutexHeld = $false $mutexName = ($incPath -replace "\\","-") -replace ":","" $lock = new-object System.Threading.Mutex($false, $mutexName, [ref] $createdMutex); try { - $mutexHeld = $lock.WaitOne() + if (!$createdMutex) { + $lock.WaitOne() + log "mutex acquired after WaitOne." + $mutexHeld = $true + } else { + log "created mutex $mutexName" + $mutexHeld = $true + } if ($generateProps -and !(test-path $verpropfile)) { + log "creating $verpropfile" $verprops | Set-Content $verpropfile } [string] $current = "" @@ -173,8 +180,15 @@ try { log "creating $buildpropfile" $buildNumberProps | Set-Content $buildpropfile } else { - log "updating $buildpropfile" - $buildNumberProps | Set-Content $buildpropfile -Force + $current = (get-content $buildpropfile -raw) + if ($current.Length) { + $current = $current.Trim() + } + $buildNumberProps = $buildNumberProps.Trim() + if (($current.Length -ne $buildNumberProps.Length) -or ($current -cne $buildNumberProps)) { + log "updating $buildpropfile" + $buildNumberProps | Set-Content $buildpropfile -Force + } } $mutexHeld = $false $lock.ReleaseMutex() @@ -182,8 +196,12 @@ try { catch { } finally { - if ($mutexHeld) { - $lock.ReleaseMutex(); + if ($lock -and $lock.WaitOne(0)) { + $lock.ReleaseMutex() + Write-Host "Mutex released." + } + if ($lock) { + $lock.Dispose() } } $verfile = "$incPath\version.h" From 666ebef392df4939b8d6ad5631881bd0c16878ad Mon Sep 17 00:00:00 2001 From: mark roddy Date: Thu, 15 Jan 2026 13:42:57 -0500 Subject: [PATCH 2/3] Fix mutex creation bug. Add more logging and validation. --- versionfiles.ps1 | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/versionfiles.ps1 b/versionfiles.ps1 index 9c65cec..6928ec4 100644 --- a/versionfiles.ps1 +++ b/versionfiles.ps1 @@ -23,7 +23,7 @@ if (! [string]::IsNullOrEmpty($BuildString)) { $verBuildString += " `"-$BuildString`"" } -$contents=@" +$NtVerpContents=@" #pragma once // // Generated file: DO NOT EDIT! @@ -150,7 +150,7 @@ $verpropfile = "$($incPath)\version.props" $buildpropfile = "$($incPath)\buildnumber.props" [bool] $createdMutex = $false $mutexName = ($incPath -replace "\\","-") -replace ":","" -$lock = new-object System.Threading.Mutex($false, $mutexName, [ref] $createdMutex); +$lock = new-object System.Threading.Mutex($true, $mutexName, [ref] $createdMutex); try { if (!$createdMutex) { $lock.WaitOne() @@ -171,10 +171,19 @@ try { $current = $current.Trim() } } - $tempContents = $contents.Trim() + $tempContents = $NtVerpContents.Trim() if (($current.Length -ne $tempContents.Length) -or ($current -cne $tempContents)) { - log "creating $($incPath)\ntverp.h" - $contents | set-content "$($incPath)\ntverp.h" + log "creating $($incPath)\ntverp.h current: $($current.Length) new: $($tempContents.Length)" + Remove-Item $incPath\ntverp.h -Force + $tempContents | Set-Content "$($incPath)\ntverp.h" -Force + $current2 = (get-content "$($incPath)\ntverp.h" -raw) + if ($current2.Length) { + $current2 = $current2.Trim() + } + log "New $($incPath)\ntverp.h length $($current2.Length)" + if ($current2.Length -ne $tempContents.Length) { + log "Error truncated $($incPath)\ntverp.h now: $($current2.Length) tempContents: $($tempContents.Length)" + } } if (!(test-path $buildpropfile)) { log "creating $buildpropfile" @@ -194,11 +203,12 @@ try { $lock.ReleaseMutex() } catch { + Write-Output $_ | Format-List * -Force | Out-String } finally { if ($lock -and $lock.WaitOne(0)) { $lock.ReleaseMutex() - Write-Host "Mutex released." + log "Mutex released." } if ($lock) { $lock.Dispose() From c56fe054385a9c08b181636398531ab411f392b7 Mon Sep 17 00:00:00 2001 From: mark roddy Date: Thu, 15 Jan 2026 14:38:29 -0500 Subject: [PATCH 3/3] code review changes. --- versionfiles.ps1 | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/versionfiles.ps1 b/versionfiles.ps1 index 6928ec4..f20add9 100644 --- a/versionfiles.ps1 +++ b/versionfiles.ps1 @@ -155,10 +155,8 @@ try { if (!$createdMutex) { $lock.WaitOne() log "mutex acquired after WaitOne." - $mutexHeld = $true } else { log "created mutex $mutexName" - $mutexHeld = $true } if ($generateProps -and !(test-path $verpropfile)) { log "creating $verpropfile" @@ -174,8 +172,8 @@ try { $tempContents = $NtVerpContents.Trim() if (($current.Length -ne $tempContents.Length) -or ($current -cne $tempContents)) { log "creating $($incPath)\ntverp.h current: $($current.Length) new: $($tempContents.Length)" - Remove-Item $incPath\ntverp.h -Force - $tempContents | Set-Content "$($incPath)\ntverp.h" -Force + Remove-Item $incPath\ntverp.h -Force -ErrorAction SilentlyContinue + $tempContents | Set-Content "$($incPath)\ntverp.h" -Force $current2 = (get-content "$($incPath)\ntverp.h" -raw) if ($current2.Length) { $current2 = $current2.Trim() @@ -199,17 +197,13 @@ try { $buildNumberProps | Set-Content $buildpropfile -Force } } - $mutexHeld = $false $lock.ReleaseMutex() + log "mutex released." } catch { Write-Output $_ | Format-List * -Force | Out-String } finally { - if ($lock -and $lock.WaitOne(0)) { - $lock.ReleaseMutex() - log "Mutex released." - } if ($lock) { $lock.Dispose() }