User code can run after module request shutdown via the output layer
- Dominant language
- C
- Stars
- 40.4k
- Forks
- 8.1k
- Avg merge
- 2d 13h
- Merged PRs (30d)
- 96
Description
Description
The following code:
<?php
header_register_callback( fn() => mb_detect_encoding('foo') );
Resulted in this output:
Fatal error: Uncaught ValueError: mb_detect_encoding(): Argument #2 ($encodings) must specify at least one encoding in /in/0EDPU:2
Stack trace:
#0 /in/0EDPU(2): mb_detect_encoding('foo')
#1 [internal function]: {closure:/in/0EDPU:2}()
#2 {main}
thrown in /in/0EDPU on line 2
But I expected this output instead:
The error seems to be present since PHP 8.0.0
https://3v4l.org/0EDPU
The error also occurs when mb_convert_encoding() is called instead of mb_detect_encoding().
Workaround
Setting mbstring.detect_order explicitly in php.ini prevents the error:
; php.ini
mbstring.detect_order = "ASCII,UTF-8";
Setting the order with mb_detect_order() inside the callback also prevents the issue:
header_register_callback( function() {
mb_detect_order(['ASCII', 'UTF-8']);
mb_detect_encoding('foo');
} );
Setting the order outside of the callback or setting it with ini_set() did not prevent the error.
PHP Version
PHP 8.4.15 (cli) (built: Dec 8 2025 22:51:21) (NTS)
Copyright (c) The PHP Group
Built by https://github.com/docker-library/php
Zend Engine v4.4.15, Copyright (c) Zend Technologies
with Zend OPcache v8.4.15, Copyright (c), by Zend Technologies
with Xdebug v3.5.0, Copyright (c) 2002-2025, by Derick Rethans
Operating System
No response
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reproducing the header_register_callback example with mb_detect_encoding() and compare it with the documented expected empty output. Read the discussion and linked pull request #20824, then verify that the callback no longer runs user code after module request shutdown and that the reproduction no longer emits the fatal error.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- php
- Domain
- backend
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 25/100