Skip to content

debug windows stdio test flakiness #1158

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

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
142 changes: 142 additions & 0 deletions scripts/windows-debug/check-tee-command.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
#!/usr/bin/env pwsh
# Script to check if tee command is available on Windows
# Usage: .\check-tee-command.ps1

Write-Host "Checking for 'tee' command availability on Windows..." -ForegroundColor Cyan
Write-Host ""

# Store original PATH
$originalPath = $env:PATH

# Method 1: Using where.exe
Write-Host "Method 1: Using where.exe" -ForegroundColor Yellow
try {
$whereResult = where.exe tee 2>&1
if ($LASTEXITCODE -eq 0) {
Write-Host " Found tee at: $whereResult" -ForegroundColor Green
} else {
Write-Host " tee not found via where.exe" -ForegroundColor Red
}
} catch {
Write-Host " Error checking with where.exe: $_" -ForegroundColor Red
}

Write-Host ""

# Method 2: Using Get-Command
Write-Host "Method 2: Using Get-Command" -ForegroundColor Yellow
try {
$getCommandResult = Get-Command tee -ErrorAction Stop
Write-Host " Found tee:" -ForegroundColor Green
Write-Host " Name: $($getCommandResult.Name)"
Write-Host " CommandType: $($getCommandResult.CommandType)"
Write-Host " Source: $($getCommandResult.Source)"
Write-Host " Version: $($getCommandResult.Version)"
} catch {
Write-Host " tee not found via Get-Command" -ForegroundColor Red
}

Write-Host ""

# Method 3: Check common locations
Write-Host "Method 3: Checking common locations" -ForegroundColor Yellow
$commonPaths = @(
"C:\Program Files\Git\usr\bin\tee.exe",
"C:\Program Files (x86)\Git\usr\bin\tee.exe",
"C:\tools\msys64\usr\bin\tee.exe",
"C:\msys64\usr\bin\tee.exe",
"C:\cygwin64\bin\tee.exe",
"C:\cygwin\bin\tee.exe"
)

$found = $false
foreach ($path in $commonPaths) {
if (Test-Path $path) {
Write-Host " Found at: $path" -ForegroundColor Green
$found = $true
}
}

if (-not $found) {
Write-Host " tee not found in common locations" -ForegroundColor Red
}

Write-Host ""

# Method 4: Check if it's a PowerShell alias
Write-Host "Method 4: Checking PowerShell aliases" -ForegroundColor Yellow
$alias = Get-Alias tee -ErrorAction SilentlyContinue
if ($alias) {
Write-Host " Found PowerShell alias:" -ForegroundColor Green
Write-Host " Name: $($alias.Name)"
Write-Host " Definition: $($alias.Definition)"
} else {
Write-Host " No PowerShell alias for tee" -ForegroundColor Yellow
}

Write-Host ""

# Method 5: Python check (what the test uses)
Write-Host "Method 5: Python shutil.which() check" -ForegroundColor Yellow
$pythonCheck = python -c "import shutil; print(shutil.which('tee'))"
if ($pythonCheck -and $pythonCheck -ne "None") {
Write-Host " Python found tee at: $pythonCheck" -ForegroundColor Green
} else {
Write-Host " Python shutil.which() did not find tee" -ForegroundColor Red
}

Write-Host ""

# Method 6: Try adding Git for Windows to PATH if it exists
Write-Host "Method 6: Adding Git for Windows to PATH temporarily" -ForegroundColor Yellow
$gitPaths = @(
"C:\Program Files\Git\usr\bin",
"C:\Program Files (x86)\Git\usr\bin"
)

$addedToPath = $false
foreach ($gitPath in $gitPaths) {
if (Test-Path $gitPath) {
Write-Host " Found Git directory: $gitPath" -ForegroundColor Green
$env:PATH = "$gitPath;$env:PATH"
$teeCheck = python -c "import shutil; print(shutil.which('tee'))"
if ($teeCheck -and $teeCheck -ne "None") {
Write-Host " tee is now available at: $teeCheck" -ForegroundColor Green
$addedToPath = $true
break
}
}
}

if (-not $addedToPath) {
# Restore original PATH if we didn't find tee
$env:PATH = $originalPath
Write-Host " Could not add Git for Windows tee to PATH" -ForegroundColor Red
}

Write-Host ""
Write-Host "========== SUMMARY ==========" -ForegroundColor Cyan
if ($whereResult -or $getCommandResult -or $found -or ($pythonCheck -and $pythonCheck -ne "None") -or $addedToPath) {
Write-Host "tee command is available" -ForegroundColor Green
Write-Host ""
Write-Host "The test_stdio_context_manager_exiting test should run." -ForegroundColor Green
if ($addedToPath) {
Write-Host ""
Write-Host "Note: Git for Windows tee was added to PATH for this session." -ForegroundColor Yellow
Write-Host "To make this permanent, add this to your PowerShell profile:" -ForegroundColor Yellow
Write-Host " `$env:PATH = `"C:\Program Files\Git\usr\bin;`$env:PATH`"" -ForegroundColor Cyan
}
} else {
Write-Host "tee command is NOT available" -ForegroundColor Red
Write-Host ""
Write-Host "The test_stdio_context_manager_exiting test will be SKIPPED." -ForegroundColor Yellow
Write-Host ""
Write-Host "To install tee on Windows, you can:" -ForegroundColor Cyan
Write-Host " 1. Install Git for Windows (includes tee in Git Bash)"
Write-Host " 2. Install WSL (Windows Subsystem for Linux)"
Write-Host " 3. Install MSYS2 or Cygwin"
Write-Host " 4. Use PowerShell's Tee-Object cmdlet (different syntax)"
}

# Restore original PATH
$env:PATH = $originalPath
152 changes: 152 additions & 0 deletions scripts/windows-debug/diagnose-environment.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
#!/usr/bin/env pwsh
# Script to diagnose environment differences that might cause CI flakiness
# Usage: .\diagnose-environment.ps1

Write-Host "Diagnosing Windows environment for stdio test issues..." -ForegroundColor Cyan
Write-Host ""

# System Information
Write-Host "=== SYSTEM INFORMATION ===" -ForegroundColor Yellow
Write-Host "Windows Version:"
(Get-CimInstance Win32_OperatingSystem).Version
Write-Host "Windows Build:"
(Get-ItemProperty "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion").DisplayVersion
Write-Host ""

# Python Information
Write-Host "=== PYTHON INFORMATION ===" -ForegroundColor Yellow
Write-Host "Python Version:"
python --version
Write-Host ""
Write-Host "Python Build Info:"
python -c "import sys; print(f'Version: {sys.version}')"
python -c "import sys; print(f'Platform: {sys.platform}')"
python -c "import sys; print(f'Windows Version: {sys.getwindowsversion()}')"
Write-Host ""

# Check subprocess configuration
Write-Host "=== SUBPROCESS CONFIGURATION ===" -ForegroundColor Yellow
python -c @"
import subprocess
import sys
print(f'CREATE_NO_WINDOW available: {hasattr(subprocess, "CREATE_NO_WINDOW")}')
print(f'CREATE_NEW_PROCESS_GROUP available: {hasattr(subprocess, "CREATE_NEW_PROCESS_GROUP")}')
print(f'Windows subprocess startup info: {hasattr(subprocess, "STARTUPINFO")}')

# Check handle inheritance defaults
import os
print(f'\nHandle inheritance (os.O_NOINHERIT): {hasattr(os, "O_NOINHERIT")}')

# Check asyncio event loop
import asyncio
try:
loop = asyncio.get_event_loop_policy()
print(f'Event loop policy: {type(loop).__name__}')
except:
print('Event loop policy: Unable to determine')
"@
Write-Host ""

# Check for Job Objects support
Write-Host "=== JOB OBJECTS SUPPORT ===" -ForegroundColor Yellow
python -c @"
try:
import win32job
import win32api
print('pywin32 available: Yes')
print(f'win32job version: {win32job.__file__}')

# Try to create a job object
try:
job = win32job.CreateJobObject(None, '')
win32api.CloseHandle(job)
print('Job Object creation: Success')
except Exception as e:
print(f'Job Object creation: Failed - {e}')
except ImportError:
print('pywin32 available: No (Job Objects not available)')
"@
Write-Host ""

# Check process limits
Write-Host "=== PROCESS LIMITS ===" -ForegroundColor Yellow
python -c @"
import os
import psutil
proc = psutil.Process(os.getpid())
print(f'Open handles: {proc.num_handles()}')
print(f'Open files: {len(proc.open_files())}')

# Check system-wide limits
print(f'Total processes: {len(psutil.pids())}')
"@
Write-Host ""

# Check security software
Write-Host "=== SECURITY SOFTWARE ===" -ForegroundColor Yellow
Get-CimInstance -Namespace "root\SecurityCenter2" -ClassName AntiVirusProduct -ErrorAction SilentlyContinue |
Select-Object displayName, productState | Format-Table
Write-Host ""

# Test rapid process creation
Write-Host "=== RAPID PROCESS CREATION TEST ===" -ForegroundColor Yellow
Write-Host "Testing rapid tee process creation/destruction..."
$testScript = @'
import time
import subprocess
import sys

failures = 0
times = []

for i in range(20):
start = time.time()
try:
# Create process with same flags as stdio client
proc = subprocess.Popen(
['tee'],
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
creationflags=getattr(subprocess, 'CREATE_NO_WINDOW', 0)
)
proc.stdin.close()
proc.wait(timeout=0.5)
elapsed = time.time() - start
times.append(elapsed)
except Exception as e:
failures += 1
print(f" Iteration {i+1}: FAILED - {e}")

if (i+1) % 5 == 0:
avg_time = sum(times) / len(times) if times else 0
print(f" Completed {i+1}/20 (avg: {avg_time*1000:.1f}ms)")

print(f"\nFailures: {failures}/20")
if times:
print(f"Average time: {sum(times)/len(times)*1000:.1f}ms")
print(f"Max time: {max(times)*1000:.1f}ms")
'@

python -c $testScript
Write-Host ""

# Environment variables that might affect subprocess
Write-Host "=== RELEVANT ENVIRONMENT VARIABLES ===" -ForegroundColor Yellow
@("COMSPEC", "PATH", "PYTHONPATH", "PYTHONASYNCIODEBUG") | ForEach-Object {
$value = [Environment]::GetEnvironmentVariable($_)
if ($value) {
Write-Host "$_`:"
Write-Host " $value" -ForegroundColor Gray
}
}
Write-Host ""

Write-Host "=== DIAGNOSIS COMPLETE ===" -ForegroundColor Cyan
Write-Host ""
Write-Host "Share this output when reporting the flakiness issue." -ForegroundColor Yellow
Write-Host "Key differences to look for between local and CI:" -ForegroundColor Yellow
Write-Host " - Windows version/build" -ForegroundColor Gray
Write-Host " - Python build details" -ForegroundColor Gray
Write-Host " - Security software presence" -ForegroundColor Gray
Write-Host " - Process creation timing" -ForegroundColor Gray
59 changes: 59 additions & 0 deletions scripts/windows-debug/setup-environment.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#!/usr/bin/env pwsh
# Script to set up the environment for running stdio tests on Windows
# This adds Git for Windows tools to PATH if available
# Usage: . .\setup-environment.ps1 (note the dot-sourcing)

Write-Host "Setting up environment for stdio tests..." -ForegroundColor Cyan
Write-Host ""

# Check if Git for Windows is installed
$gitPaths = @(
"C:\Program Files\Git\usr\bin",
"C:\Program Files (x86)\Git\usr\bin"
)

$gitFound = $false
$gitPath = ""

foreach ($path in $gitPaths) {
if (Test-Path $path) {
$gitPath = $path
$gitFound = $true
break
}
}

if ($gitFound) {
Write-Host "Found Git for Windows at: $gitPath" -ForegroundColor Green

# Add to PATH
$env:PATH = "$gitPath;$env:PATH"

# Verify tee is available
$teeCheck = python -c "import shutil; print(shutil.which('tee'))"
if ($teeCheck -and $teeCheck -ne "None") {
Write-Host "Successfully added tee to PATH: $teeCheck" -ForegroundColor Green
Write-Host ""
Write-Host "Environment is ready for stdio tests!" -ForegroundColor Green
Write-Host ""
Write-Host "You can now run the test scripts or individual tests:" -ForegroundColor Cyan
Write-Host " .\test-stdio-flakiness-200-runs.ps1"
Write-Host " .\test-stdio-flakiness-until-failure.ps1"
Write-Host " .\test-stdio-verbose-debug.ps1"
Write-Host ""
Write-Host "Or run individual tests with:" -ForegroundColor Cyan
Write-Host " uv run pytest tests/client/test_stdio.py::test_stdio_context_manager_exiting -v -o addopts="""""
} else {
Write-Host "WARNING: Git path was added but tee is still not available" -ForegroundColor Yellow
}
} else {
Write-Host "Git for Windows not found in standard locations." -ForegroundColor Red
Write-Host ""
Write-Host "Please install Git for Windows from: https://gitforwindows.org/" -ForegroundColor Yellow
Write-Host "Or manually add the Git usr\bin directory to your PATH." -ForegroundColor Yellow
}

Write-Host ""
Write-Host "Note: This only affects the current PowerShell session." -ForegroundColor Gray
Write-Host "To make changes permanent, add to your PowerShell profile:" -ForegroundColor Gray
Write-Host ' $env:PATH = "C:\Program Files\Git\usr\bin;$env:PATH"' -ForegroundColor Cyan
Loading
Loading