# potential shutdown order issue

- Thread: https://externals.io/message/38265
- Original archive: https://news-web.php.net/php.internals/38265
- Started by Gregory Beaver on 2008-06-16T02:55:40+00:00
- Messages: 7
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #38265 — Gregory Beaver — 2008-06-16T02:55:40+00:00

URL: https://externals.io/message/38265#38265

Hi,

I'm getting errors of hashtable already destroyed when running
phpMyAdmin with PHP 5.3, and (of course), thinking this is a phar issue,
I've traced through and found a problem in the shutdown order. 
Basically, php_request_shutdown() calls zend_deactivate() which calls
zend_destroy_rsrc_list(&EG(regular_list)).  On the next line,
zend_post_deactivate_modules() is called, which steps through the module
list and unloads all the dynamically loaded modules.  If one of these
modules (like mysqli) declares a resource type, then in
module_destructor() zend_clean_module_rsrc_dtors() is called, which
calls zend_clean_modules_rsrc_dtors_cb() (zend_list.c:253).

This function then walks over EG(regular_list), which had been
previously destroyed.

I think this issue could be fixed by moving the
zend_destroy_rsrc_list(&EG(regular_list)) call after the module
shutdown.  I've attached a patch demonstrating the principle (this fixes
the hashtable destroyed message and I don't get anything bad like a
segfault).

Could someone with more brains double-check this one?  I think it can be
reproduced with a debug build using mysqli loaded dynamically and any
script that uses mysqli, but I've only reproduced it with phpMyAdmin.

Thanks,
Greg

---

## #39020 — Lukas Kahwe Smith — 2008-07-16T20:07:57+00:00

URL: https://externals.io/message/38265#39020 · In reply to: #38265

Hi,

Does anyone agree that there is an issue to fix here? Messing with the  
shutdown order is probably the last thing any RM would like to see  
happening ..

regards,
Lukas



> Hi,
>
> I'm getting errors of hashtable already destroyed when running
> phpMyAdmin with PHP 5.3, and (of course), thinking this is a phar  
> issue,
> I've traced through and found a problem in the shutdown order.
> Basically, php_request_shutdown() calls zend_deactivate() which calls
> zend_destroy_rsrc_list(&EG(regular_list)).  On the next line,
> zend_post_deactivate_modules() is called, which steps through the  
> module
> list and unloads all the dynamically loaded modules.  If one of these
> modules (like mysqli) declares a resource type, then in
> module_destructor() zend_clean_module_rsrc_dtors() is called, which
> calls zend_clean_modules_rsrc_dtors_cb() (zend_list.c:253).
>
> This function then walks over EG(regular_list), which had been
> previously destroyed.
>
> I think this issue could be fixed by moving the
> zend_destroy_rsrc_list(&EG(regular_list)) call after the module
> shutdown.  I've attached a patch demonstrating the principle (this  
> fixes
> the hashtable destroyed message and I don't get anything bad like a
> segfault).
>
> Could someone with more brains double-check this one?  I think it  
> can be
> reproduced with a debug build using mysqli loaded dynamically and any
> script that uses mysqli, but I've only reproduced it with phpMyAdmin.
>
> Thanks,
> Greg
> Index: Zend/zend.c
> ===================================================================
> RCS file: /repository/ZendEngine2/zend.c,v
> retrieving revision 1.308.2.12.2.35.2.18
> diff -u -r1.308.2.12.2.35.2.18 zend.c
> --- Zend/zend.c	29 Apr 2008 08:15:16 -0000	1.308.2.12.2.35.2.18
> +++ Zend/zend.c	16 Jun 2008 02:53:00 -0000
> @@ -901,7 +901,8 @@
> 		shutdown_compiler(TSRMLS_C);
> 	} zend_end_try();
>
> -	zend_destroy_rsrc_list(&EG(regular_list) TSRMLS_CC);
> +	/* shutdown order issue */
> +	/* zend_destroy_rsrc_list(&EG(regular_list) TSRMLS_CC); */
>
> #ifdef ZEND_DEBUG
> 	if (GC_G(gc_enabled) && !CG(unclean_shutdown)) {
> Index: main/main.c
> ===================================================================
> RCS file: /repository/php-src/main/main.c,v
> retrieving revision 1.640.2.23.2.57.2.22
> diff -u -r1.640.2.23.2.57.2.22 main.c
> --- main/main.c	21 May 2008 15:55:31 -0000	1.640.2.23.2.57.2.22
> +++ main/main.c	16 Jun 2008 02:53:02 -0000
> @@ -1527,6 +1527,8 @@
> 		zend_post_deactivate_modules(TSRMLS_C);
> 	} zend_end_try();
>
> +	zend_destroy_rsrc_list(&EG(regular_list) TSRMLS_CC);
> +
> 	/* 9. SAPI related shutdown (free stuff) */
> 	zend_try {
> 		sapi_deactivate(TSRMLS_C);
>
> -- 
> 
> 

Lukas Kahwe Smith
mls@pooteeweet.org

---

## #39025 — Stanislav Malyshev — 2008-07-16T20:32:36+00:00

URL: https://externals.io/message/38265#39025 · In reply to: #38265

Hi!

> zend_destroy_rsrc_list(&EG(regular_list)).  On the next line,
> zend_post_deactivate_modules() is called, which steps through the module
> list and unloads all the dynamically loaded modules.  If one of these

 `dl()` is really troublesome...

> I think this issue could be fixed by moving the
> zend_destroy_rsrc_list(&EG(regular_list)) call after the module
> shutdown.  I've attached a patch demonstrating the principle (this fixes
> the hashtable destroyed message and I don't get anything bad like a
> segfault).

But many resource dtors may need some action from modules they belong 
to. If modules are already past shutdown, that may be a problem.
If we must spend time on supporting `dl()`, then I'd propose to fix module 
dtor so that it wouldn't try to access EG(regular_list) - if it happens 
as you described, it doesn't need it anyway.
-- 
Stanislav Malyshev, Zend Software Architect
stas@zend.com   http://www.zend.com/
(408)253-8829   MSN: stas@zend.com

---

## #39026 — Greg Beaver — 2008-07-17T01:29:13+00:00

URL: https://externals.io/message/38265#39026 · In reply to: #39025

Stanislav Malyshev wrote:
> Hi!
>
> > […]
>
> `dl()` is really troublesome...
>
> > […]
>
> But many resource dtors may need some action from modules they belong 
> to. If modules are already past shutdown, that may be a problem.
> If we must spend time on supporting `dl()`, then I'd propose to fix 
> module dtor so that it wouldn't try to access EG(regular_list) - if it 
> happens as you described, it doesn't need it anyway. 
Hi,

This has nothing to do with `dl()`.  mysqli was loaded in php.ini via
extension=mysqli.so

I can't test the proposed fix until August, but it sounds like it would 
also fix the issue without messing with shutdown.

Greg

---

## #39037 — Stanislav Malyshev — 2008-07-17T07:02:46+00:00

URL: https://externals.io/message/38265#39037 · In reply to: #39026

Hi!

> This has nothing to do with `dl()`.  mysqli was loaded in php.ini via
> extension=mysqli.so

Oh, I missed that point. Anyway, it sounds logical that all resources 
should be freed before modules are unloaded, not the reverse.

-- 
Stanislav Malyshev, Zend Software Architect
stas@zend.com   http://www.zend.com/
(408)253-8829   MSN: stas@zend.com

---

## #40028 — Lukas Kahwe Smith — 2008-08-20T20:51:35+00:00

URL: https://externals.io/message/38265#40028 · In reply to: #39026

> Stanislav Malyshev wrote:
> > […]
> Hi,
>
> This has nothing to do with `dl()`.  mysqli was loaded in php.ini via
> extension=mysqli.so
>
> I can't test the proposed fix until August, but it sounds like it  
> would also fix the issue without messing with shutdown.


@Dmitry/Stas .. could you guys look into this issue?
@Greg: can you provide any further context information?

regards,
Lukas Kahwe Smith
mls@pooteeweet.org

---

## #40051 — Dmitry Stogov — 2008-08-21T13:19:56+00:00

URL: https://externals.io/message/38265#40051 · In reply to: #40028

I missed the problem. How can I reproduce it?

Thanks. Dmitry.

Lukas Kahwe Smith wrote:
> 
> 
> 
> > […]
> 
> 
> @Dmitry/Stas .. could you guys look into this issue?
> @Greg: can you provide any further context information?
> 
> regards,
> Lukas Kahwe Smith
> mls@pooteeweet.org
