Skip to content
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

x/sys/windows/svc/mgr: add Service.RecoveryActionsOnNonCrashFailures and Service.SetRecoveryActionsOnNonCrashFailures #59016

Closed
craig65535 opened this issue Mar 13, 2023 · 14 comments
Labels
NeedsFix The path to resolution is known, but the work has not been done. OS-Windows Proposal Proposal-Accepted
Milestone

Comments

@craig65535
Copy link

craig65535 commented Mar 13, 2023

Proposal is at #59016 (comment)


What version of Go are you using (go version)?

$ go version
1.19.5

Does this issue reproduce with the latest release?

Yes

What operating system and processor architecture are you using (go env)?

windows/amd64

What did you do?

Created a Windows service that requires recovery actions (such as restarting) that are executed when the service has exited with an error but has not crashed.

With a *mgr.Service (s), I attempted s.SetRecoveryActions and found that it did not support setting this flag.

What did you expect to see?

s.SetRecoveryActions should include the failure actions flag.

This is supported by ChangeServiceConfig2 but not the wrapper that exists in Service.SetRecoveryActions.

What did you see instead?

s.SetRecoveryActions can only set the recovery actions themselves, and the time at which each is active.

Proposal details

See #59016 (comment) where we propose adding API to access RecoveryActionsOnNonCrashFailures flag.

@gopherbot gopherbot added this to the Unreleased milestone Mar 13, 2023
@craig65535
Copy link
Author

craig65535 commented Mar 13, 2023

I am able to work around this by calling windows.ChangeServiceConfig2() directly:

import (
	"github.com/winlabs/gowin32/wrappers"
	"golang.org/x/sys/windows"
	"golang.org/x/sys/windows/svc"
	"golang.org/x/sys/windows/svc/mgr"
)

...

	flag := wrappers.SERVICE_FAILURE_ACTIONS_FLAG{
		FailureActionsOnNonCrashFailures: 1,
	}
	err := windows.ChangeServiceConfig2(s.Handle, windows.SERVICE_CONFIG_FAILURE_ACTIONS_FLAG, (*byte)(unsafe.Pointer(&flag)))
	...

However, it seems like this should also be in the mgr package.

@cherrymui
Copy link
Member

Does this require changing the signature of SetRecoveryActions, or adding a new method?

cc @golang/windows

@cherrymui cherrymui added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Mar 17, 2023
@craig65535
Copy link
Author

You could do either of those.SetRecoveryActions currently only takes a list of failure actions (passing SERVICE_CONFIG_FAILURE_ACTIONS to ChangeServiceConfig2), and there's no provision to support any of the other options provided by ChangeServiceConfig2 which are mainly single values and not lists. So, either it would need to change, or a new function could be added.

@alexbrainman
Copy link
Member

Hello @craig65535

If you want to have access to the failure actions flag, we can add 2 new methods similar to, for example:

a@spot:~/src/golang.org/x/sys/windows/svc/mgr$ GOOS=windows go doc Service.SetRebootMessage                           
package mgr // import "."

func (s *Service) SetRebootMessage(msg string) error
    SetRebootMessage sets service s reboot message. If msg is "", the reboot
    message is deleted and no message is broadcast.

a@spot:~/src/golang.org/x/sys/windows/svc/mgr$ GOOS=windows go doc Service.RebootMessage
package mgr // import "."

func (s *Service) RebootMessage() (string, error)
    RebootMessage is broadcast to server users before rebooting in response to
    the ComputerReboot service controller action.

a@spot:~/src/golang.org/x/sys/windows/svc/mgr$

Would that work for you?

I did not need this functionality ever. But others contributed code for whatever service parameters they required. I doubt we could support all combinations that are supported by ChangeServiceConfig2 in a general and simple way. So I expect some users to call windows.ChangeServiceConfig2 directly. But it would be nice to have most common parameters supported.

Alex

@craig65535
Copy link
Author

@alexbrainman Yes, that sounds perfect.

@alexbrainman
Copy link
Member

@alexbrainman Yes, that sounds perfect.

Would you like to try to add that code yourself?

Alex

@craig65535
Copy link
Author

Sure, I can give this one a shot as well.

@craig65535
Copy link
Author

golang/sys#157

@gopherbot
Copy link

Change https://go.dev/cl/484896 mentions this issue: windows/svc/mgr: add Service.RecoveryActionsOnNonCrashFailures

@alexbrainman
Copy link
Member

I propose to add these two methods

// SetRecoveryActionsOnNonCrashFailures sets the failure actions flag. If the
// flag is set to false, recovery actions will only be performed if the service
// terminates without reporting a status of SERVICE_STOPPED. If the flag is set
// to true, recovery actions are also perfomed if the service stops with a
// nonzero exit code.
func (s *Service) SetRecoveryActionsOnNonCrashFailures(flag bool) error


// RecoveryActionsOnNonCrashFailures returns the current value of the failure
// actions flag. If the flag is set to false, recovery actions will only be
// performed if the service terminates without reporting a status of
// SERVICE_STOPPED. If the flag is set to true, recovery actions are also
// perfomed if the service stops with a nonzero exit code.
func (s *Service) RecoveryActionsOnNonCrashFailures() (bool, error)

to the golang.org/x/sys/windows/svc/mgr.Service struct. New methods will allow users set and read SERVICE_FAILURE_ACTIONS_FLAG for the service.

We already have similar Service.RebootMessage / Service.SetRebootMessage, Service.RecoveryActions / Service.SetRecoveryActions and few others.

It is possible to achieve this functionality by calling Windows API directly. See #59016 (comment) for an example. But we decided since we already have similar attributes handled, maybe we can add RecoveryActionsOnNonCrashFailures flag to that list.

Thank you for consideration.

Alex

@alexbrainman alexbrainman changed the title x/sys/windows/svc/mgr: Service.SetRecoveryActions should support failure actions flag proposal: golang.org/x/sys/windows/svc/mgr: add Service.RecoveryActionsOnNonCrashFailures and Service.SetRecoveryActionsOnNonCrashFailures Apr 22, 2023
@seankhliao seankhliao changed the title proposal: golang.org/x/sys/windows/svc/mgr: add Service.RecoveryActionsOnNonCrashFailures and Service.SetRecoveryActionsOnNonCrashFailures proposal: x/sys/windows/svc/mgr: add Service.RecoveryActionsOnNonCrashFailures and Service.SetRecoveryActionsOnNonCrashFailures May 13, 2023
@alexbrainman
Copy link
Member

@bcmills

Just want to make sure that this proposal is not forgotten. I labelled the issue as proposal about month ago. I hope I did not make mistake somewhere.

Thank you for your help.

Alex

@rsc
Copy link
Contributor

rsc commented May 24, 2023

This proposal has been added to the active column of the proposals project
and will now be reviewed at the weekly proposal review meetings.
— rsc for the proposal review group

@rsc
Copy link
Contributor

rsc commented May 31, 2023

Based on the discussion above, this proposal seems like a likely accept.
— rsc for the proposal review group

@rsc
Copy link
Contributor

rsc commented Jun 7, 2023

No change in consensus, so accepted. 🎉
This issue now tracks the work of implementing the proposal.
— rsc for the proposal review group

@rsc rsc changed the title proposal: x/sys/windows/svc/mgr: add Service.RecoveryActionsOnNonCrashFailures and Service.SetRecoveryActionsOnNonCrashFailures x/sys/windows/svc/mgr: add Service.RecoveryActionsOnNonCrashFailures and Service.SetRecoveryActionsOnNonCrashFailures Jun 7, 2023
@dmitshur dmitshur added NeedsFix The path to resolution is known, but the work has not been done. and removed NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. labels Jun 17, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
NeedsFix The path to resolution is known, but the work has not been done. OS-Windows Proposal Proposal-Accepted
Projects
Status: Accepted
Development

Successfully merging a pull request may close this issue.

6 participants