Skip to content

Commit 418ee9b

Browse files
committed
feedback, check for AllocConsole error
1 parent 4eb4c19 commit 418ee9b

File tree

3 files changed

+13
-11
lines changed

3 files changed

+13
-11
lines changed

cmd/opampsupervisor/main_windows.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,13 @@ var (
2525
func run() error {
2626
// always allocate a console in case we're running as service
2727
if err := allocConsole(); err != nil {
28-
return fmt.Errorf("alloc console: %w", err)
28+
if !errors.Is(err, windows.ERROR_ACCESS_DENIED) {
29+
// Per https://learn.microsoft.com/en-us/windows/console/allocconsole#remarks
30+
// AllocConsole fails with this error when there's already a console attached, such as not being ran as service
31+
// ignore this error and only return other errors
32+
return fmt.Errorf("alloc console: %w", err)
33+
}
34+
2935
}
3036
defer func() {
3137
_ = freeConsole()
@@ -38,10 +44,6 @@ func run() error {
3844
// Per https://learn.microsoft.com/en-us/windows/win32/api/winsvc/nf-winsvc-startservicectrldispatchera#return-value
3945
// this means that the process is not running as a service, so run interactively.
4046

41-
// manually free console before running interactively
42-
if err = freeConsole(); err != nil {
43-
return fmt.Errorf("free console: %w", err)
44-
}
4547
return runInteractive()
4648
}
4749
return fmt.Errorf("failed to start supervisor: %w", err)

cmd/opampsupervisor/supervisor/supervisor_windows.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,18 +28,18 @@ func (ws *windowsService) Execute(args []string, requests <-chan svc.ChangeReque
2828
// The first argument supplied to service.Execute is the service name. If this is
2929
// not provided for some reason, raise a relevant error to the system event log
3030
if len(args) == 0 {
31-
return false, uint32(windows.ERROR_INVALID_SERVICENAME) // 1213: ERROR_INVALID_SERVICENAME
31+
return false, uint32(windows.ERROR_INVALID_SERVICENAME)
3232
}
3333

3434
elog, err := openEventLog(args[0])
3535
if err != nil {
36-
return false, uint32(windows.ERROR_EVENTLOG_CANT_START) // 1501: ERROR_EVENTLOG_CANT_START
36+
return false, uint32(windows.ERROR_EVENTLOG_CANT_START)
3737
}
3838

3939
changes <- svc.Status{State: svc.StartPending}
4040
if err = ws.start(elog); err != nil {
4141
_ = elog.Error(3, fmt.Sprintf("failed to start service: %v", err))
42-
return false, uint32(windows.ERROR_EXCEPTION_IN_SERVICE) // 1064: ERROR_EXCEPTION_IN_SERVICE
42+
return false, uint32(windows.ERROR_EXCEPTION_IN_SERVICE)
4343
}
4444
changes <- svc.Status{State: svc.Running, Accepts: svc.AcceptStop | svc.AcceptShutdown}
4545

@@ -56,7 +56,7 @@ func (ws *windowsService) Execute(args []string, requests <-chan svc.ChangeReque
5656

5757
default:
5858
_ = elog.Error(3, fmt.Sprintf("unexpected service control request #%d", req.Cmd))
59-
return false, uint32(windows.ERROR_INVALID_SERVICE_CONTROL) // 1052: ERROR_INVALID_SERVICE_CONTROL
59+
return false, uint32(windows.ERROR_INVALID_SERVICE_CONTROL)
6060
}
6161
}
6262

cmd/opampsupervisor/supervisor/supervisor_windows_service_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ const (
2828
//
2929
// To test locally:
3030
// * Build the supervisor and collector binaries:
31-
// - go build
31+
// - cd cmd/opampsupervisor; go build
3232
// - make otelcontribcol
3333
//
3434
// * Install the Windows service
@@ -70,7 +70,7 @@ func TestSupervisorAsService(t *testing.T) {
7070
}, 10*time.Second, 500*time.Millisecond)
7171

7272
// verify supervisor service started & healthy
73-
// Read the events from the otelcorecol source and check that they were emitted after the service
73+
// Read the events from the opampsupervisor source and check that they were emitted after the service
7474
// command started. This is a simple validation that the messages are being logged on the
7575
// Windows event log.
7676
cmd := exec.Command("wevtutil.exe", "qe", "Application", "/c:1", "/rd:true", "/f:RenderedXml", "/q:*[System[Provider[@Name='opampsupervisor']]]")

0 commit comments

Comments
 (0)