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

Bug founded. Event 'CWebApplication.onEndRequest' attached a invalid handler 'NULL' #3158

Closed
qiansen1386 opened this issue Jan 10, 2014 · 5 comments

Comments

@qiansen1386
Copy link

When using yii1.1.15-dev on PHP5.4, get error
qq20140110091337

More discussion here:
yincart/basic#12

Sorry, talked in Chinese.

@ghost ghost assigned qiangxue Jan 10, 2014
@samdark
Copy link
Member

samdark commented Jan 10, 2014

@qiangxue this one is for you since you can get the context.

@Frostecho
Copy link

Hi,

For reproduce this error enough this code:

class SiteController extends Controller {
    public function actionIndex() {
        error_reporting(0); // Simulate production mode
        Yii::app()->setGlobalState('foo',microtime(true));
        Yii::app()->attachEventHandler('onEndRequest', array($this,'whenRequestsEnds'));
        Yii::app()->end();
    }
    public function whenRequestsEnds($event) {
        echo "request ended...";
    }
}

Simplified code with comments and links:

error_reporting(0); // Simulate production mode
$list = new CList(); // simulate CComponent::$_e variable
$list->add(0); // simulate CLogRouter::processLogs
$list->add(1); // simulate CWebApplication::saveGlobalState
$list->add(2); // simulate SiteController::whenRequestsEnds

foreach ($list as $value) { // CComponent::raiseEvent() foreach: https://github.com/yiisoft/yii/blob/1.1.14/framework/base/CComponent.php#L548
    var_dump($value); // just debug
    if($value == 1) { // if event handler is CWebApplication::saveGlobalState
        // run event handler and if state is changed then run detachEventHandler:
        // https://github.com/yiisoft/yii/blob/1.1.14/framework/base/CApplication.php#L687
        // and remove handler from the list:
        // https://github.com/yiisoft/yii/blob/1.1.14/framework/base/CComponent.php#L530
        $list->removeAt(1);
        // because the CListIterator is use reference:
        // https://github.com/yiisoft/yii/blob/1.1.14/framework/collections/CListIterator.php#L41
        // the $this->_d is now have 2 elements (values: 0 and 2, indexes: 0 and 1), but the $this->_c value is still 3:
        // https://github.com/yiisoft/yii/blob/1.1.14/framework/collections/CListIterator.php#L43
        // and for this reason the "2" index is valid here, but not existed in the $this->_d
        // https://github.com/yiisoft/yii/blob/1.1.14/framework/collections/CListIterator.php#L91
        // if the error_reporting is not 0 here the php is throw a notice error:
        // https://github.com/yiisoft/yii/blob/1.1.14/framework/collections/CListIterator.php#L72
        // Undefined offset: 2
    }
}

output:

int(0) 
int(1) 
NULL

expected output:

int(0) 
int(1) 
int(2)

(sorry my bad english)

@samdark
Copy link
Member

samdark commented Jan 10, 2014

You shouldn't use error_reporting(0); for production. Instead you should turn off display_errors and write logs.

@ghost ghost assigned qiangxue Jan 10, 2014
@cebe
Copy link
Member

cebe commented Jan 10, 2014

looks like a duplicate of #1191.

@Frostecho
Copy link

@samdark Yes, you're right. I'm sorry. I just simplified this code: https://github.com/yincart/basic/blob/master/backend.php#L3 and I wrote bad comment.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants