php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #72265 session exception
Submitted: 2016-05-26 02:37 UTC Modified: 2016-05-26 09:33 UTC
From: wangyuan at lianlife dot com Assigned:
Status: Not a bug Package: Session related
PHP Version: 5.6.21 OS: linux 5.4 x64
Private report: No CVE-ID: None
 [2016-05-26 02:37 UTC] wangyuan at lianlife dot com
Description:
------------
I can't catch the exception that is thrown from the write method in the sess class, but other methods can work properly, such as open (), read () and other.

I'm not sure PHP itself is designed, or is it a bug.

Run result in the console : 


Test script:
---------------
class sess extends SessionHandler {
    public function read($session_id) {
        throw new Exception('read');
    }
    public function write($session_id, $session_data) {
        throw new Exception('write');
    }
}

try {
    $handler = new sess();
    session_set_save_handler($handler, true);
    session_start();
    $_SESSION['name'] = 'php';
    $_SESSION['gender'] = 'female';
    echo $_SESSION['name'];
} catch (Exception $e) {
    echo '<pre>';
    print_r($e);
    echo '</pre>';
}

Expected result:
----------------
The following information should be catch, but it is displayed as "excetpion Uncaught"


2016/05/26 10:17:37 [error] 13474#0: *19551 FastCGI sent in stderr: "PHP message: PHP Fatal error:  Uncaught exception 'Exception' with message '0jib891huoct6e4lpl5i3f9tt7name|s:8:"wangyuan";gender|s:4:"male";' in /data/web/ndl/test.php:11
Stack trace:
#0 [internal function]: sess->write('0jib891huoct6e4...', 'name|s:8:"wangy...')
#1 [internal function]: session_write_close()
#2 {main}
  thrown in /data/web/ndl/test.php on line 11" while reading response header from upstream, client: 10.24.18.243, server: ndl.lianlife.com, request: "GET /test.php HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "10.20.33.33:81"


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2016-05-26 03:25 UTC] requinix@php.net
-Status: Open +Status: Feedback
 [2016-05-26 03:25 UTC] requinix@php.net
I see the read exception being caught fine. With that commented out, the write exception cannot be caught normally because that happens at script shutdown - outside the try/catch block. If I close the session manually with session_write_close() at the end of the try block then the write exception gets caught as expected.

Does that match what you are seeing?
 [2016-05-26 08:54 UTC] wangyuan at lianlife dot com
-Status: Feedback +Status: Closed
 [2016-05-26 08:54 UTC] wangyuan at lianlife dot com
Thank you very much. 


try {
    session_set_save_handler($handler, false);
    session_start();
    .....
    session_write_close();   // <- close the session manually, I can catch the exception in catch block
    .....
} catch (Exception $e) {
    print_r($e);
}
 [2016-05-26 09:33 UTC] requinix@php.net
-Status: Closed +Status: Not a bug
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Apr 19 15:01:28 2024 UTC