|  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
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
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please !
Your email address:
Solve the problem:
13 + 10 = ?
Subscribe to this entry?

 [2015-03-03 15:19 UTC] tfm-phpnet at earth dot li
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.


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


AllCommentsChangesGit/SVN commitsRelated reports
 [2015-03-05 21:07 UTC]
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 (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.


 [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

Have you the same issue with PHP 7.0+ ?

The new version 0.2.1 is now available.
 [2019-01-03 06:30 UTC]
-Assigned To: +Assigned To: jbnahan
 [2019-01-03 06:33 UTC]
This feature has been added into the next version 0.4.0.
 [2019-01-03 06:33 UTC]
-Status: Assigned +Status: Closed
PHP Copyright © 2001-2023 The PHP Group
All rights reserved.
Last updated: Tue Oct 03 03:01:24 2023 UTC