Skip to content

The cross-harness, self-improving memory layer for AI agents. Join our community: https://discord.com/invite/zdNChS2fBu

service: a forwarder that will not stop is ended and deleted

shukieshahf3f7f7055580
diff --git a/cli/beacon/internal/endpoint/service/forwarder.go b/cli/beacon/internal/endpoint/service/forwarder.go
index 2bf3be57..2452c2f7 100644
--- a/cli/beacon/internal/endpoint/service/forwarder.go
+++ b/cli/beacon/internal/endpoint/service/forwarder.go
@@ -189,7 +189,7 @@ func (m ForwarderManager) UnitPath() (string, error) {
func (m ForwarderManager) RemoveUnits() {
if m.resolvedKind() == KindWindowsService {
if m.Supported() {
- _ = removeService(m.spec().WindowsService, m.stopTimeout())
+ _ = removeService(m.spec().WindowsService, m.stopTimeout(), true)
}
return
}
@@ -287,7 +287,7 @@ func (m ForwarderManager) Load() error {
case KindWindowsService:
// A restart, not a start, so a rewritten config takes effect, then a wait for RUNNING: the
// SCM reports a start as accepted long before Vector has loaded its config.
- if err := restartService(spec.WindowsService, m.stopTimeout()); err != nil {
+ if err := restartService(spec.WindowsService, m.stopTimeout(), true); err != nil {
return err
}
return waitForServiceRunning(spec.WindowsService, forwarderStartTimeout)
@@ -343,7 +343,7 @@ func (m ForwarderManager) Unload() error {
if !m.Supported() {
return nil
}
- return removeService(spec.WindowsService, m.stopTimeout())
+ return removeService(spec.WindowsService, m.stopTimeout(), true)
default:
return nil
}
diff --git a/cli/beacon/internal/endpoint/service/windows.go b/cli/beacon/internal/endpoint/service/windows.go
index 1d015dd8..d66b9df9 100644
--- a/cli/beacon/internal/endpoint/service/windows.go
+++ b/cli/beacon/internal/endpoint/service/windows.go
@@ -105,14 +105,14 @@ func (b windowsBackend) unload(userMode bool) error {
if !b.available() {
return nil
}
- return removeService(WindowsServiceName, serviceStopTimeout)
+ return removeService(WindowsServiceName, serviceStopTimeout, false)
}
func (b windowsBackend) restart(userMode bool) error {
if !b.available() {
return fmt.Errorf("%s", b.unsupportedReason())
}
- return restartService(WindowsServiceName, serviceStopTimeout)
+ return restartService(WindowsServiceName, serviceStopTimeout, false)
}
func (b windowsBackend) status(userMode bool) Status {
diff --git a/cli/beacon/internal/endpoint/service/windows_other.go b/cli/beacon/internal/endpoint/service/windows_other.go
index 3b4f0474..9b8e3263 100644
--- a/cli/beacon/internal/endpoint/service/windows_other.go
+++ b/cli/beacon/internal/endpoint/service/windows_other.go
@@ -35,9 +35,9 @@ func serviceCurrent(scmService) (bool, error) { return false, errNotWindows("ins
// removeService is the one exception, and deliberately so: uninstall and repair call unload
// speculatively on every backend, and failing there would block removing an endpoint that was
// never a Windows service to begin with.
-func removeService(string, time.Duration) error { return nil }
+func removeService(string, time.Duration, bool) error { return nil }
-func restartService(string, time.Duration) error { return errNotWindows("restart") }
+func restartService(string, time.Duration, bool) error { return errNotWindows("restart") }
func queryService(name string) Status {
return Status{
diff --git a/cli/beacon/internal/endpoint/service/windows_scm.go b/cli/beacon/internal/endpoint/service/windows_scm.go
index 630eb160..ca5855ac 100644
--- a/cli/beacon/internal/endpoint/service/windows_scm.go
+++ b/cli/beacon/internal/endpoint/service/windows_scm.go
@@ -254,7 +254,13 @@ func waitForServiceRunning(name string, timeout time.Duration) error {
}
// removeService stops and deletes the registration, treating an absent service as success.
-func removeService(name string, stopTimeout time.Duration) error {
+//
+// terminateHung ends the service's process when it will not stop, and deletes the registration
+// anyway. The collector never sets it: a wedged collector is reported, not hidden. A forwarder does,
+// because Vector hangs in START_PENDING when it fails to start, and a service in that state refuses
+// the stop request outright, which would otherwise leave it registered, running and holding
+// vector.exe. Ending Vector loses nothing: its read position is saved only for what Azure accepted.
+func removeService(name string, stopTimeout time.Duration, terminateHung bool) error {
return withManager(func(m *mgr.Mgr) error {
s, err := m.OpenService(name)
if err != nil {
@@ -264,7 +270,12 @@ func removeService(name string, stopTimeout time.Duration) error {
defer s.Close()
if err := stopAndWait(s, name, stopTimeout); err != nil {
- return err
+ if !terminateHung {
+ return err
+ }
+ if killErr := terminateServiceProcess(s, name, stopTimeout); killErr != nil {
+ return errors.Join(err, killErr)
+ }
}
if err := s.Delete(); err != nil && !errors.Is(err, windows.ERROR_SERVICE_MARKED_FOR_DELETE) {
return fmt.Errorf("delete the %s service: %w", name, err)
@@ -322,6 +333,27 @@ func stopAndWait(s *mgr.Service, name string, timeout time.Duration) error {
return fmt.Errorf("the %s service did not stop within %s", name, timeout)
}
+// terminateServiceProcess ends the process behind a service that would not stop, and waits for it
+// to exit so its image is released. A service with no process left is already what we want.
+func terminateServiceProcess(s *mgr.Service, name string, timeout time.Duration) error {
+ status, err := s.Query()
+ if err != nil {
+ return fmt.Errorf("query the %s service: %w", name, err)
+ }
+ if status.State == svc.Stopped || status.ProcessId == 0 {
+ return nil
+ }
+ process, err := windows.OpenProcess(windows.PROCESS_TERMINATE|windows.SYNCHRONIZE, false, status.ProcessId)
+ if err != nil {
+ return fmt.Errorf("open the %s service's process to end it: %w", name, err)
+ }
+ defer windows.CloseHandle(process)
+ if err := windows.TerminateProcess(process, 1); err != nil {
+ return fmt.Errorf("end the %s service's process: %w", name, err)
+ }
+ return waitForServiceProcess(process, name, time.Now().Add(timeout), timeout)
+}
+
// openServiceProcess returns a handle that can wait on the service's process, or 0 when there is no
// process to wait on or it cannot be opened.
func openServiceProcess(pid uint32) windows.Handle {
@@ -354,7 +386,7 @@ func waitForServiceProcess(process windows.Handle, name string, deadline time.Ti
return nil
}
-func restartService(name string, stopTimeout time.Duration) error {
+func restartService(name string, stopTimeout time.Duration, terminateHung bool) error {
if err := withManager(func(m *mgr.Mgr) error {
s, err := m.OpenService(name)
if err != nil {
@@ -363,7 +395,15 @@ func restartService(name string, stopTimeout time.Duration) error {
return errServiceNotInstalled
}
defer s.Close()
- return stopAndWait(s, name, stopTimeout)
+ if err := stopAndWait(s, name, stopTimeout); err != nil {
+ if !terminateHung {
+ return err
+ }
+ if killErr := terminateServiceProcess(s, name, stopTimeout); killErr != nil {
+ return errors.Join(err, killErr)
+ }
+ }
+ return nil
}); err != nil {
if errors.Is(err, errServiceNotInstalled) {
return startService(name)