Skip to content

fix: EventLog handle leak and StreamWriter crash in WinBGP CLI#7

Open
Daymarvi wants to merge 1 commit into
webalexeu:mainfrom
Daymarvi:fix/winbgp-cli-eventlog-and-pipe
Open

fix: EventLog handle leak and StreamWriter crash in WinBGP CLI#7
Daymarvi wants to merge 1 commit into
webalexeu:mainfrom
Daymarvi:fix/winbgp-cli-eventlog-and-pipe

Conversation

@Daymarvi

Copy link
Copy Markdown

Summary

Fix two resource management issues in WinBGP.ps1 (CLI):

1. EventLog handle leak in Write-Log

[System.Diagnostics.EventLog]::new() implements IDisposable but was never disposed after writing events with AdditionalFields. In a 24/7 service context, this causes gradual handle exhaustion.

Fix: Wrap EventLog usage in try/finally with Dispose().

2. StreamWriter crash on unconnected pipe in Send-PipeMessage

StreamWriter was created before pipe.Connect(). If the connection failed or timed out, Dispose() on the StreamWriter would throw an unhandled .NET exception because the underlying pipe was never connected.

Fix: Move StreamWriter creation after successful pipe.Connect(1000).

Testing

Tested on production server my.server.net.

Functional tests

Command Result
WinBGP.ps1 -Status Running
WinBGP.ps1 -BGPStatus ✅ 3 routes displayed (route1, route2, route3) all up
WinBGP.ps1 -RouteName my.server.net -StartMaintenance Success
WinBGP.ps1 -RouteName my.server.net -StopMaintenance Success
WinBGP.ps1 -RouteName my.server.net -StopRoute Success
WinBGP.ps1 -RouteName my.server.net -StartRoute Success

Handle leak test

$svcPid = (Get-CimInstance Win32_Service -Filter "Name='WinBGP'").ProcessId
Get-Process -Id $svcPid | Select-Object HandleCount
1..50 | ForEach-Object { & "C:\Program Files\WinBGP\WinBGP.ps1" -Status }
Get-Process -Id $svcPid | Select-Object HandleCount
Metric Value
HandleCount before (PID 1264) 214
50x consecutive -Status calls All returned Running
HandleCount after 214
Result ✅ No handle leak

- Fix EventLog handle leak: Dispose() EventLog objects in Write-Log
  when using AdditionalFields (prevents handle exhaustion in 24/7 service)
- Fix StreamWriter on unconnected pipe: create StreamWriter after
  pipe.Connect() to avoid Dispose() crash when service is not running
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.

1 participant