Skip to content

[Feature Request] Option to terminate Service and trigger recovery actions #1563

@LincolnPuzey

Description

@LincolnPuzey

I am using the win32serviceutil.ServiceFramework class to write a Windows Service.

I have a use-case where I would like my service to terminate in such a way that the recovery actions (e.g. restart service) are triggered.

By default these recovery actions are triggered when the service terminates without reporting a status of SERVICE_STOPPED.

I have tried overriding svcRun and raising an exception or calling sys.exit(), eg

def SvcRun(self):
    self.ReportServiceStatus(win32service.SERVICE_RUNNING)

    # ideally this would cause the service process to end without any further calls to SetServiceStatus(),
    # so the recovery actions are triggered.
    raise Exception()

but pywin32 sets the service status to SERVICE_STOPPED in its cleanup actions after calling SvcRun.
https://github.com/mhammond/pywin32/blob/master/win32/src/PythonService.cpp#L907

Ideally there would be some way for SvcRun to flag that this cleanup should not be done.

My current workaround is to call os.kill(os.getpid(), signal.SIGABRT), which avoids the cleanup. But I would like to avoid such a nuclear option.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions