Files
kandev/apps/backend/internal/common/ptyexec/pty_windows_test.go
T
João Salavisa c5c591b60b refactor(pty): collapse duplicated PTY and cmdline helpers into common/ptyexec (#2457)
Co-authored-by: Kandev Agent <agent@kandev.dev>
Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-08-09 01:03:13 +01:00

35 lines
1.2 KiB
Go

//go:build windows
package ptyexec
import (
"os/exec"
"testing"
)
// TestWindowsPTY_DoubleCloseSafe pins down the sync.Once guard on
// windowsPTY.Close. The upstream UserExistsError/conpty library has no internal
// synchronization, so a second Close would double-free the underlying Windows
// handles and trigger STATUS_HEAP_CORRUPTION (0xC0000374) — the crash from issue
// #894. Both consumers of this package close from two directions (see the
// windowsPTY doc comment), so the guard has to hold here.
func TestWindowsPTY_DoubleCloseSafe(t *testing.T) {
cmd := exec.Command("cmd.exe", "/c", "exit", "0")
pty, err := Start(cmd, 80, 24)
if err != nil {
t.Fatalf("Start: %v", err)
}
firstErr := pty.Close()
if firstErr != nil {
t.Fatalf("first Close: %v", firstErr)
}
// The second Close must not panic and must not double-free. Assert on the
// memoized value rather than just nil-ness: sync.Once means the second call
// has to report whatever the first one did, so comparing the two is what
// actually distinguishes "gated" from "ran twice and happened to succeed".
if secondErr := pty.Close(); secondErr != firstErr {
t.Errorf("second Close returned %v, want memoized %v", secondErr, firstErr)
}
}