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

Uncaught Exception: Serialization of 'Closure' is not allowed #10

Closed
sarukuku opened this issue Mar 21, 2017 · 11 comments
Closed

Uncaught Exception: Serialization of 'Closure' is not allowed #10

sarukuku opened this issue Mar 21, 2017 · 11 comments

Comments

@sarukuku
Copy link

I'm getting this kind of error just by installing & enabling the plugin:

[21-Mar-2017 08:46:03 UTC] PHP Fatal error:  Uncaught Exception: Serialization of 'Closure' is not allowed in [].../wordpress/wp-includes/functions.php:435
Stack trace:
#0 [...]/wordpress/wp-includes/functions.php(435): serialize(Object(WP_REST_Response))
#1 [...]/wordpress/wp-includes/option.php(427): maybe_serialize(Object(WP_REST_Response))
#2 [...]/wordpress/wp-includes/option.php(730): add_option('_transient_rest...', Object(WP_REST_Response), '', 'no')
#3 [...]/dm-collection/plugins/wp-rest-api-cache/class-wp-rest-cache.php(63): set_transient('rest_cache_/wp-...', Object(WP_REST_Response), 3600)
#4 [...]/wordpress/wp-includes/class-wp-hook.php(298): WP_REST_Cache::pre_dispatch(Object(WP_REST_Response), Object(WP_REST_Server in [].../wordpress/wp-includes/functions.php on line 435

Any idea what's wrong?

@sarukuku
Copy link
Author

I looked into this a bit and it seems that you can't serialize the WP_REST_Response object. If I change the line 63 to

set_transient( $key, $result->get_data(), $timeout );

then it works. But I'm not sure if the type returned will always be an instance of WP_REST_Response object so I can't really say if this is a viable option as a fix.

@sarukuku
Copy link
Author

I created to pull request that fixes this. See #11.

@sarukuku
Copy link
Author

I did a bit of testing & prototyping related to this issue and the pull request (#11) I submitted. It turned out that it's quite hard to try to get around the closure serialization issue inside the WP_REST_Response. I couldn't find any sane way to do it.

I tracked down where the closures were that were failing the serialization and the closures ended up being the validation and sanitation functions attached to the register_rest_route() function. Changing from using closures in those calls to using local named functions circumvents the serialization problem.

Using the closures in that way wasn't even my idea but actually a recommended way of doing it while the WP API wasn't yet in the core. See f.ex. the validate_callback example here.

I suggest that we add some note about not supporting closures inside the callback functions tied to the register_rest_route() function to the readme of this. Maybe it will save time from people who also have the same problem. What do you think?

I'll also drop the PR related to this issue.

@sarukuku sarukuku mentioned this issue Mar 22, 2017
@airesvsg
Copy link
Owner

Hi @sarukuku,
Thanks for your time.
Please, send me more information about your environment like WP version, plugins, PHP version and endpoints.

Thanks

@sarukuku
Copy link
Author

Sorry about the delay. I'm using the latest WP version and the problem is repeatable with only the wp-rest-api-cache plugin installed and active. I'm using PHP 7 and a sample endpoint that causes the error could look f.ex. like this:

add_action( 'rest_api_init', function () {
	register_rest_route( 'myplugin/v1', '/author/(?P<id>\d+)', array(
		'methods' => 'GET',
		'callback' => 'my_awesome_func',
		'args' => array(
			'id' => array(
				'validate_callback' => function($param, $request, $key) {
					return is_numeric( $param );
				}
			),
		),
	) );
} );

The problem is that with PHP you can't serialize anonymous functions without tricks and the plugin relies on serialization on a level where an endpoint written like the one above would be serialized. As it includes an anonymous function it will throw an error when the plugin tries to serialize it for caching.

The way around this is to give the function a name and only reference it by name from the endpoint. It's an easy fix but I think it should be stated in the readme and handled gracefully in inside the plugin.

@wrux
Copy link

wrux commented Apr 23, 2018

It looks like this is over a year old but did this get updated? I just tested the extension and got this error.

I just disabled all plugins except REST API cache and I got this error:

[Mon Apr 23 04:32:28.782774 2018] [:error] [pid 19449] [client 115.84.94.242:50308] PHP Fatal error: Uncaught Exception: Serialization of 'Closure' is not allowed in /var/www/site/wordpress/wp-includes/functions.php:440\nStack trace:\n#0 /var/www/site/wordpress/wp-includes/functions.php(440): serialize(Object(WP_REST_Response))\n#1 /var/www/site/wordpress/wp-includes/option.php(463): maybe_serialize(Object(WP_REST_Response))\n#2 /var/www/site/wordpress/wp-includes/option.php(766): add_option('_transient_rest...', Object(WP_REST_Response), '', 'no')\n#3 /var/www/site/wordpress/wp-content/plugins/wp-rest-api-cache-master/class-wp-rest-cache.php(63): set_transient('rest_cache_/wp-...', Object(WP_REST_Response), 604800)\n#4 /var/www/site/wordpress/wp-includes/class-wp-hook.php(286): WP_REST_Cache::pre_dispatch(Object(WP_REST_Response), Object(WP_REST_Server), Object(WP_REST_Request))\n#5 /var/www/site/wordpress/wp-includes/plugin.php(203): WP_Hook->apply_filters(NULL, Array)\n#6 /var/www/site/wordpress/wp-includes/rest-api/class-wp-rest-server.php(817): apply_filters('rest_pre_dispat...', NULL, Object(WP_REST_Se in /var/www/site/wordpress/wp-includes/functions.php on line 440

@sarukuku
Copy link
Author

@wrux Just don’t use anonymous/inline functions and you should be fine.

@wrux
Copy link

wrux commented Apr 24, 2018

This was using the default WP rest API on the latest version

@XedinUnknown
Copy link

Same problem.

@sarukuku
Copy link
Author

Hans someone looked if the OTB WP REST API endpoints use inline functions in validation etc.? If this is the case this plugin will not work with them. At least that's my experience.

@ashishpatel1992
Copy link

I am also having same issues in my error log!

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

No branches or pull requests

5 participants