Files
wehub-resource-sync f99010fae1
CI / lint (push) Failing after 1s
CI / frontend (push) Failing after 1s
CI / scripts (push) Failing after 1s
CI / Go Test (ubuntu-latest) (push) Failing after 0s
CI / frontend-node-25 (push) Failing after 1s
CI / docs (push) Failing after 0s
CI / coverage (push) Failing after 0s
CI / e2e (push) Failing after 0s
Docker / build-and-push (push) Failing after 1s
CI / integration (push) Failing after 4m43s
CI / Go Test (windows-latest) (push) Has been cancelled
CI / Desktop Unit Tests (Windows) (push) Has been cancelled
Desktop Artifacts / Desktop Build (Linux (arm64)) (push) Has been cancelled
Desktop Artifacts / Desktop Build (Linux) (push) Has been cancelled
Desktop Artifacts / Desktop Build (Windows) (push) Has been cancelled
Desktop Artifacts (macOS) / Desktop Build (macOS (aarch64)) (push) Has been cancelled
Desktop Artifacts (macOS) / Desktop Build (macOS (x86_64)) (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 12:30:36 +08:00

74 lines
2.3 KiB
Go

//go:build windows
package main
import (
"fmt"
"os/exec"
"unsafe"
"golang.org/x/sys/windows"
)
// jobCaddyGuard holds a job-object handle. While the server process keeps it
// open, the job lives; when the server exits and the OS closes its handles, the
// job's KILL_ON_JOB_CLOSE limit terminates every process in it, including Caddy.
type jobCaddyGuard struct {
job windows.Handle
}
func (g jobCaddyGuard) Close() error {
return windows.CloseHandle(g.job)
}
// newCaddyGuard assigns the started Caddy process to a job object configured to
// kill its processes when the last handle to the job closes. The server holds
// that handle for its lifetime, so when the server is terminated -- including
// the uncatchable TerminateProcess that `serve stop` issues on Windows -- the
// OS tears down the job and the managed Caddy child with it, instead of leaving
// Caddy holding the public port. On any failure it returns a no-op guard so the
// caller can keep running with the prior (leak-prone) behavior.
func newCaddyGuard(cmd *exec.Cmd) (caddyGuard, error) {
if cmd == nil || cmd.Process == nil {
return noopCaddyGuard{}, fmt.Errorf("caddy process not started")
}
job, err := windows.CreateJobObject(nil, nil)
if err != nil {
return noopCaddyGuard{}, fmt.Errorf("creating job object: %w", err)
}
info := windows.JOBOBJECT_EXTENDED_LIMIT_INFORMATION{
BasicLimitInformation: windows.JOBOBJECT_BASIC_LIMIT_INFORMATION{
LimitFlags: windows.JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE,
},
}
if _, err := windows.SetInformationJobObject(
job,
windows.JobObjectExtendedLimitInformation,
uintptr(unsafe.Pointer(&info)),
uint32(unsafe.Sizeof(info)),
); err != nil {
_ = windows.CloseHandle(job)
return noopCaddyGuard{}, fmt.Errorf("configuring job object: %w", err)
}
proc, err := windows.OpenProcess(
windows.PROCESS_SET_QUOTA|windows.PROCESS_TERMINATE,
false,
uint32(cmd.Process.Pid),
)
if err != nil {
_ = windows.CloseHandle(job)
return noopCaddyGuard{}, fmt.Errorf("opening caddy process: %w", err)
}
defer func() { _ = windows.CloseHandle(proc) }()
if err := windows.AssignProcessToJobObject(job, proc); err != nil {
_ = windows.CloseHandle(job)
return noopCaddyGuard{}, fmt.Errorf("assigning caddy to job: %w", err)
}
return jobCaddyGuard{job: job}, nil
}