php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #69170 win32service exits cleanly if script dies
Submitted: 2015-03-03 15:19 UTC Modified: 2019-01-03 06:33 UTC
Votes:2
Avg. Score:5.0 ± 0.0
Reproduced:2 of 2 (100.0%)
Same Version:1 (50.0%)
Same OS:2 (100.0%)
From: tfm-phpnet at earth dot li Assigned: jbnahan (profile)
Status: Closed Package: win32service (PECL)
PHP Version: 5.5.22 OS: Windows
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: tfm-phpnet at earth dot li
New email:
PHP Version: OS:

 

 [2015-03-03 15:19 UTC] tfm-phpnet at earth dot li
Description:
------------
If the PHP script dies during execution then the PHP_RSHUTDOWN_FUNCTION reports that the service has STOPPED in the normal way with:

	SVCG(st).dwCurrentState = SERVICE_STOPPED;
	SetServiceStatus(SVCG(sh), &SVCG(st));

This means that the builtin windows service recovery options such as "Restart the Service" do not fire (because the exit is not considered to be a failure).

I have made a patch (which I've been using without issues for a couple of years) which allows you to configure for an ungraceful exit by either passing an extra optional parameter to win32_start_service_ctrl_dispatcher or calling this function:

/* {{{ proto bool win32_set_service_exit_mode(bool gracefulExit)
   Set (and get) the exit mode of the service, when set to true the service
   will shut down gracefuly when PHP exits, when set to false it will not shut
   down gracefuly, this will mean that the service will count as having failed 
   and the recovery action will be run */ 
static PHP_FUNCTION(win32_set_service_exit_mode)

If your service wants to exit cleanly it can call win32_set_service_exit_mode(true) to tell it that the shutdown should be clean.



Patches

win32service.tfmGracefulExit_php5.5.patch (last revision 2015-03-03 15:20 UTC by tfm-phpnet at earth dot li)

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2015-03-05 21:07 UTC] rquadling@php.net
I would consider that the default behaviour is a clean shutdown. If there is a way to know if the script fatals (unhandled exception, fatal error, parse error) then that could be used to indicate the ungraceful exit during PHP_RSHUTDOWN_FUNCTION.

Looking at https://msdn.microsoft.com/en-us/library/windows/desktop/ms685996(v=vs.85).aspx (not sure if this is the latest version), setting dwWin32ExitCode to ERROR_SERVICE_SPECIFIC_ERROR and then setting dwServiceSpecificExitCode to something appropriate (not sure what at this stage), would seem the better way to go as the service specific error code can be documented and that then be available to Windows for its own error reporting (Service xxx failed with service specific error code yyy).



Unfortunately, I'm not on a windows environment anymore and don't have a license for a VM.


I've asked for some help on detecting fatals on php internals and have pointed them here.

Regards,

Richard.
 [2015-04-24 03:20 UTC] minh at flowconnect dot com
We experienced the same issue, initially we try exit(1) to hopefully invoke the service control manager but then realised php does not return that the same like in C exit(-1).

Please fix this because this bug makes the feature unusable.
 [2017-05-02 21:11 UTC] jean-baptiste at nahan dot fr
Hi,

Have you the same issue with PHP 7.0+ ?

The new version 0.2.1 is now available.
 [2019-01-03 06:30 UTC] jbnahan@php.net
-Assigned To: +Assigned To: jbnahan
 [2019-01-03 06:33 UTC] jbnahan@php.net
This feature has been added into the next version 0.4.0.
 [2019-01-03 06:33 UTC] jbnahan@php.net
-Status: Assigned +Status: Closed
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Apr 18 01:01:28 2024 UTC