Repository navigation
test: verify automatic flush on exit without Stop-Sentry #138
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| # Regression script for https://github.com/getsentry/sentry-powershell/issues/38 | ||
| # | ||
| # Starts Sentry, captures a message, and exits WITHOUT calling Stop-Sentry to verify that | ||
| # events are still flushed/delivered automatically on process exit (and that the process | ||
| # exits cleanly without hanging). | ||
| # | ||
| # A file-writing transport (FileTransport, defined in utils.ps1) is used instead of a network | ||
| # call so that delivery can be observed from the parent process after this one exits, without | ||
| # relying on networking/open ports in CI. | ||
| # | ||
| # Usage: pwsh -File exit-flush-test-script.ps1 <output-file> | ||
| param( | ||
| [Parameter(Mandatory)] | ||
| [string] $OutputFile | ||
| ) | ||
|
|
||
| Set-StrictMode -Version latest | ||
| $ErrorActionPreference = 'Stop' | ||
|
|
||
| Import-Module "$PSScriptRoot/../modules/Sentry/Sentry.psd1" | ||
| . "$PSScriptRoot/utils.ps1" | ||
|
|
||
| Start-Sentry { | ||
| $_.Dsn = 'https://key@127.0.0.1/1' | ||
| $_.Transport = [FileTransport]::new($OutputFile) | ||
| } | ||
|
|
||
| $null = [Sentry.SentrySdk]::CaptureMessage('hello-from-exit-flush-test') | ||
|
|
||
| # Intentionally NO Stop-Sentry call here - delivery must happen automatically on exit. | ||
| Write-Host 'CHILD_DONE' | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| # Regression tests for https://github.com/getsentry/sentry-powershell/issues/38 | ||
| # | ||
| # Verifies that a script which captures an event and exits WITHOUT calling Stop-Sentry: | ||
| # * exits cleanly (exit code 0) within a timeout (i.e. does not hang on exit), and | ||
| # * still delivers the captured event. | ||
| # | ||
| # Historically the .NET SDK's automatic flush-on-exit hook (AppDomain.ProcessExit) was disabled | ||
| # in this module because it could hang/crash the process (sentry-dotnet#3141). That was fixed and | ||
| # the workaround was removed in #85, so omitting Stop-Sentry must now Just Work. | ||
|
|
||
| BeforeAll { | ||
| $script:childScript = "$PSScriptRoot$([IO.Path]::DirectorySeparatorChar)exit-flush-test-script.ps1" | ||
|
|
||
| # Runs the exit-flush child script in a separate process and returns whether it exited cleanly | ||
| # within the timeout, its exit code, and the delivered envelope content. | ||
| function Invoke-ExitFlushChild { | ||
| param( | ||
| [Parameter(Mandatory)] [string] $Executable, | ||
| [int] $TimeoutSeconds = 60 | ||
| ) | ||
|
|
||
| $outputFile = [IO.Path]::GetTempFileName() | ||
| $stdout = [IO.Path]::GetTempFileName() | ||
| $stderr = [IO.Path]::GetTempFileName() | ||
| # FileTransport appends; start from an empty file. | ||
| Remove-Item $outputFile -ErrorAction SilentlyContinue | ||
|
|
||
| try { | ||
| $proc = Start-Process -FilePath $Executable ` | ||
| -ArgumentList @('-NoProfile', '-File', $script:childScript, $outputFile) ` | ||
| -PassThru -NoNewWindow ` | ||
| -RedirectStandardOutput $stdout -RedirectStandardError $stderr | ||
|
|
||
| $exited = $proc.WaitForExit($TimeoutSeconds * 1000) | ||
| if (-not $exited) { | ||
| try { $proc.Kill() } catch {} | ||
|
github-advanced-security[bot] marked this conversation as resolved.
Fixed
|
||
| return [PSCustomObject]@{ | ||
| Exited = $false | ||
| ExitCode = $null | ||
| Envelope = '' | ||
| StdOut = (Get-Content -Raw $stdout -ErrorAction SilentlyContinue) | ||
| StdErr = (Get-Content -Raw $stderr -ErrorAction SilentlyContinue) | ||
| } | ||
| } | ||
|
|
||
| return [PSCustomObject]@{ | ||
| Exited = $true | ||
| ExitCode = $proc.ExitCode | ||
| Envelope = (Get-Content -Raw $outputFile -ErrorAction SilentlyContinue) | ||
| StdOut = (Get-Content -Raw $stdout -ErrorAction SilentlyContinue) | ||
| StdErr = (Get-Content -Raw $stderr -ErrorAction SilentlyContinue) | ||
| } | ||
| } finally { | ||
| Remove-Item $outputFile, $stdout, $stderr -ErrorAction SilentlyContinue | ||
| } | ||
| } | ||
| } | ||
|
|
||
| Describe 'Automatic flush on exit (without Stop-Sentry)' { | ||
| It 'Windows PowerShell' -Skip:($env:OS -ne 'Windows_NT') { | ||
| $result = Invoke-ExitFlushChild -Executable 'powershell.exe' | ||
| $result.Exited | Should -BeTrue -Because "the process must not hang on exit. StdErr: $($result.StdErr)" | ||
| $result.ExitCode | Should -Be 0 -Because "the process must exit cleanly. StdErr: $($result.StdErr)" | ||
| $result.Envelope | Should -Match 'hello-from-exit-flush-test' -Because 'the captured event must be delivered even without Stop-Sentry' | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This assertion (and line 83) can't fail for the reason the test exists. Adding What it does prove is worth keeping: an event is delivered without |
||
| } | ||
|
|
||
| It 'PowerShell' { | ||
| $result = Invoke-ExitFlushChild -Executable 'pwsh' | ||
| $result.Exited | Should -BeTrue -Because "the process must not hang on exit. StdErr: $($result.StdErr)" | ||
| $result.ExitCode | Should -Be 0 -Because "the process must exit cleanly. StdErr: $($result.StdErr)" | ||
| $result.Envelope | Should -Match 'hello-from-exit-flush-test' -Because 'the captured event must be delivered even without Stop-Sentry' | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,6 +11,26 @@ class RecordingTransport:Sentry.Extensibility.ITransport { | |
| } | ||
| } | ||
|
|
||
| class FileTransport:Sentry.Extensibility.ITransport { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The class BlockingTransport:Sentry.Extensibility.ITransport {
[System.Threading.Tasks.TaskCompletionSource[bool]] $tcs = [System.Threading.Tasks.TaskCompletionSource[bool]]::new()
# Never completes on its own; the test decides when the send finishes.
[System.Threading.Tasks.Task]SendEnvelopeAsync([Sentry.Protocol.Envelopes.Envelope] $envelope, [System.Threading.CancellationToken] $cancellationToken) {
return $this.tcs.Task
}
}and the test (this passes): $options.FlushTimeout = [TimeSpan]::FromMilliseconds(1)
$options.Transport = $transport = [BlockingTransport]::new()
$worker = [SynchronousWorker]::new($options)
$worker.EnqueueEnvelope($envelope) | Should -BeTrue
$worker.QueuedItems | Should -Be 1 # send didn't finish within FlushTimeout -> deferred
$transport.tcs.SetResult($true)
$null = $worker.FlushAsync([TimeSpan]::FromSeconds(5))
$worker.QueuedItems | Should -Be 0 # flush drained itReach the private class via |
||
| [string] $path | ||
|
|
||
| FileTransport([string] $path) { | ||
| $this.path = $path | ||
| } | ||
|
|
||
| # Serializes every envelope it's asked to send to a file on disk so that delivery can be | ||
| # observed from a parent process after the sending process exits (used by the exit-flush test). | ||
| [System.Threading.Tasks.Task]SendEnvelopeAsync([Sentry.Protocol.Envelopes.Envelope] $envelope, [System.Threading.CancellationToken] $cancellationToken) { | ||
| $stream = [System.IO.File]::Open($this.path, [System.IO.FileMode]::Append, [System.IO.FileAccess]::Write, [System.IO.FileShare]::ReadWrite) | ||
| try { | ||
| $envelope.Serialize($stream, $null) | ||
| } finally { | ||
| $stream.Dispose() | ||
| } | ||
| return [System.Threading.Tasks.Task]::CompletedTask | ||
| } | ||
| } | ||
|
|
||
| class TestLogger:Sentry.Infrastructure.DiagnosticLogger { | ||
| TestLogger([Sentry.SentryLevel]$level) : base($level) {} | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This also displaces
SynchronousTransport, the component that actually hung in getsentry/sentry-dotnet#3141, so$result.Exitedcan't reproduce that failure either:FileTransportreturnsTask.CompletedTaskandFlushAsyncthen waits on an empty task list.Worth a separate case that keeps the real transport and just points the DSN at a closed port:
Confirmed that path is live: with
$_.Debug = $truethe child logsRegistering integration: 'AppDomainProcessExitIntegration'and the shutdown stack isAppDomainAdapter.OnProcessExit→HandleProcessExit→ScriptBlock.InvokeWithPipe. Exits 0 in ~0.8s. Needs a small-Transport File|Httpparameter here.