Undefined Symbol php_json_serializable_ce in Unknown on Line 0 (1.2.0 Release)
Description
When installing mongodb from pecl on debian jessie, the extension seems to install fine but is never initialized due to this error. I regularly build from pecl, but right after today's update all my build scripts broke for all my enviroments. Installing the actual extension works fine, but the extension is never loaded due to the error below.
Environment
Debootstrap Debian Jessie running PHP 5.6.27
root@b0277afcbe10:/# php -m
PHP Warning: PHP Startup: Unable to load dynamic library '/usr/lib/php5/20131226/mongodb.so' - /usr/lib/php5/20131226/mongodb.so: undefined symbol: php_json_serializable_ce in Unknown on line 0
[PHP Modules]
bcmath
bz2
calendar
Core
ctype
curl
date
dba
dom
ereg
exif
fileinfo
filter
ftp
gettext
hash
iconv
json
libxml
mbstring
mhash
mysql
mysqli
openssl
pcntl
pcre
PDO
pdo_mysql
Phar
posix
readline
Reflection
session
shmop
SimpleXML
soap
sockets
SPL
standard
sysvmsg
sysvsem
sysvshm
tokenizer
wddx
xdebug
xml
xmlreader
xmlwriter
xsl
Zend OPcache
zip
zlib
[Zend Modules]
Xdebug
Zend OPcache
root@b0277afcbe10:/# php -i | grep -E 'mongodb|libmongoc|libbson'
PHP Warning: PHP Startup: Unable to load dynamic library '/usr/lib/php5/20131226/mongodb.so' - /usr/lib/php5/20131226/mongodb.so: undefined symbol: php_json_serializable_ce in Unknown on line 0
Test Script
When trying to use the mongodb/mongodb package from Composer it throws:
( ! ) Fatal error: Class 'MongoDB\Driver\Manager' not found in /var/www/html/vendor/mongodb/mongodb/src/Client.php on line 61
Thanks for your query, could you please answer the following questions:
How are you loading the json extension?
Through a module?
Does it work if you load the json extension through the php.ini system before the mongodb extension?
What's the output of php --ini
After grinding the entire day, I've came up with a temporary fix if anyone else is having this issue.
This is due to the mongodb.so extension loading before the json extension, and because mongodb does not create its own ini file within /etc/php5/cli/conf.d/ or /etc/php5/apache2/conf.d/ and instead suggests to add extension=mongodb.so to the php.ini file, loading of the extension fails.
If you create a file called 25-mongodb.ini with
extension=mongodb.so
in it and save it within the file directories /etc/php5/cli/conf.d/ and /etc/php5/apache2/conf.d/ then the extension will work as expected.
However, I think this should be generating an ini automatically, since it is dependant on another extension with a 20- priority (if installed via a package manager).
PHP's normal build system compiles in the JSON extension, by default, and statically. With that, the mongodb.so extension should build and load just fine.
Distributions often change from PHP's normal practice, and compile extensions such as the JSON extensions as shared objects. They also provide the separate INI configuration files. When compiling and installing extensions from source, through PECL, we can't it make to create these INI files. We do however say that the mongodb extension depends on the json extension, and with PHP's normal extension loading mechanism, this should not cause a problem (but I will need to test/check that). I believe Debian (and RedHat/Fedora) change the shared object loading mechanism to use RTLD_NOW instead of the normal RTLD_LAZY, which would cause the problem here. I will also need to verify that.
In short, I am not sure whether we can do anything about that code wise, except for documenting that the mongodb extension can only be loaded after the json extension has been loaded.
Fair enough, I just figured I would document it here regardless just in case someone else runs into the issue.
I have done some testing, and it is indeed because if the distribution's patch to PHP to change its loading of shared objects from RTLD_LAZY to RTLD_NOW.
I am not sure what we can do here, as the only thing I can think of is to not compile in JsonSerializable support is it's not part of the PHP binary, but that would mean that users would lose out on functionality. Alternatively, we can try to lobby the Debian project to package the mongodb extension. Right now, they only seem to have the old php5-mongo extension, which is our legacy driver.
I would consider this a bug (albeit, deliberate) in Debian's PHP's packaging. But I think we're going to have a hard time convincing them to change it. As part of the PHP Developers Group, we've already tried I believe.
I will be adding things to the documentation to make this clearer, and close this issue. If you have any further good tips or ideas, please let us know here.
Cross-linking with a similar issue we had with the legacy PHP driver in PHP-1270. According to @remicollet's comments there, the loading strategy is one of security. RTLD_LAZY could lead to a segfault at runtime instead of load time, which we're seeing here.
@oerdnj was discussing Debian packaging in https://github.com/mongodb/mongo-php-driver/issues/453, so perhaps he can chime in here.
The blog post is up at https://derickrethans.nl/undefined-symbol.html
@derickr The Debian instructions are wrong. This should be something like:
cat << EOF > /etc/php5/mods-available/mongodb.ini
; priority=99
extension=mongodb.so
EOF
php5enmod mongodb
Cheers @oerdnj , I'll fix that. Does that enable it for all installed SAPIs?
I'm still not able to get it working, end up with the same error
PHP Warning: PHP Startup: Unable to load dynamic library '/usr/lib64/php/modules/mongodb.so' - /usr/lib64/php/modules/mongodb.so: undefined symbol: php_json_serializable_ce in Unknown on line 0
I've set the load priority right, in-fact even loaded json.so multiple times (it leads to a warning, but still) just to ensure I'm on the right track. Here's a snippet of the breaking code:
[root@rohan-server php.d]# php -v
PHP Warning: Module 'json' already loaded in Unknown on line 0
PHP Warning: Module 'json' already loaded in Unknown on line 0
PHP Warning: PHP Startup: Unable to load dynamic library '/usr/lib64/php/modules/mongodb.so' - /usr/lib64/php/modules/mongodb.so: undefined symbol: php_json_serializable_ce in Unknown on line 0
PHP 5.4.22 (cli) (built: Nov 15 2013 10:09:03) Copyright (c) 1997-2013 The PHP Group Zend Engine v2.4.0, Copyright (c) 1998-2013 Zend Technologies
with Xdebug v2.2.6, Copyright (c) 2002-2014, by Derick Rethans
[root@rohan-server php.d]#
I'm on the following setup: CentOS 6.2 Php 5.4.22 Apache 2.4
Any advice on what to try to fix next ?
PS: I really need to move the MongoDB Driver 1.2.0 as I'm getting a really weird SegFault on 1.1.9 (Towards the end of a script (after a exit() when the process' connections are closed) which has a collection()->find())
I've set the load priority right, in-fact even loaded json.so multiple times (it leads to a warning, but still) just to ensure I'm on the right track.
@rahejarohan: I would suggest recursively grepping your PHP configuration directory (e.g. /etc/php) for "json" and "mongodb" to be sure, in the event those warnings are printed out of order.
PS: I really need to move the MongoDB Driver 1.2.0 as I'm getting a really weird SegFault on 1.1.9 (Towards the end of a script (after a exit() when the process' connections are closed) which has a collection()->find())
If you can get a backtrace for that segfault, I'd appreciate it if you could open a new issue with more details (either in this GitHub repository or as a new PHPC issue in JIRA).
I have the same issue, trying to install mongodb PHP driver, on Centos 7.3 and Apache 2.4. both the json and mongodb extensions are loaded using *.ini files, and mongodb is using a name that starts with "zz_99".
Any ideas?
@yaziderman — we need to see the whole error message please.
@derick, additional notice, from a quick search, php_json_serializable_ce is part of json public API since 5.4.26 and 5.5.10 and 5.6.0, so older version are not compatible (perhaps work if everything build statically)
P.S. http://git.php.net/?p=php-src.git;a=commitdiff;h=786234d351c40a9b4808d1dd13d2f91f2f5314be
@yaziderman if you use PHP 5.4.16 from CentOS official repository, you should use php-pecl-mongodb from EPEL repository (if you use any other packaged PHP version, ask your RPM provider).
@remicollet: Thanks for pointing out that the symbol isn't in all 5.4 and 5.5 versions. I created PHPC-878 to track that and will look into creating a workaround.
This issue occurred to me when I was upgraded my PHP version. Previously I was using the PHP 5.4 and mongodb-1.2.0. When I upgraded to PHP 5.6 I got this problem. I installed mongodb-1.3.4 to fix this issue and it works fine.