php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #76006 PHP engine execute the code after shutdown_function by "finally" block
Submitted: 2018-02-25 13:46 UTC Modified: 2018-02-25 13:51 UTC
From: maestroprog at gmail dot com Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: 7.2.2 OS: Ubuntu Artful
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: maestroprog at gmail dot com
New email:
PHP Version: OS:

 

 [2018-02-25 13:46 UTC] maestroprog at gmail dot com
Description:
------------
It seems I've found a curious undocumented feature of the code execution "finally" after the shutdown_function.

Test script:
---------------
<?php
register_shutdown_function(function () {
    echo 'BYE BY SHUTDOWN', PHP_EOL;

    register_shutdown_function(function () {
        echo 'BYE BY LAST SHUTDOWN', PHP_EOL;
    });
});
function shutdown(): \Generator
{
    try {
        yield;
    } finally {
        echo 'PHP SUPER SHUTDOWN', PHP_EOL;
    }
}

$generator = shutdown();
$generator->rewind();

Expected result:
----------------
Code from finally executed never, since the generator is not iterated until the end, or "finally" code executed before shutdown_function.

php test.php 
BYE BY SHUTDOWN
BYE BY LAST SHUTDOWN

Actual result:
--------------
php test.php 
BYE BY SHUTDOWN
BYE BY LAST SHUTDOWN
PHP SUPER SHUTDOWN


Code from finally block executed after ALL shutdown_functions.

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2018-02-25 13:51 UTC] kelunik@php.net
-Status: Open +Status: Not a bug
 [2018-02-25 13:51 UTC] kelunik@php.net
This is expected behavior. Generator finally blocks are always run on garbage collection in case the generator didn't finish.

You can also unset($generator); to achieve the same effect.
 [2018-02-25 13:53 UTC] kelunik@php.net
You can compare that with destructors of any other object, see https://3v4l.org/HDSbI
 [2018-02-25 14:26 UTC] maestroprog at gmail dot com
Ok, tnx, good.

But this nearly identical codes given different results:

https://3v4l.org/JQEOj

Result is:

UNSET BEFORE yield

After add:

unset($generator);

Result is: (PHP7.2)

PHP Fatal error:  Uncaught Error: Cannot yield from finally in a force-closed generator

PHP7.1 throws this exception without unset($generator);
This is a bug?
 [2018-02-25 17:42 UTC] kelunik@php.net
The result for 7.2 looks odd, could you report that as a new bug? It's different than this one.
 [2018-02-25 22:23 UTC] maestroprog at gmail dot com
Ok, done. https://bugs.php.net/bug.php?id=76007&thanks=4
 
PHP Copyright © 2001-2026 The PHP Group
All rights reserved.
Last updated: Wed Oct 07 08:00:02 2026 UTC