Make WordPress Core

Opened 9 years ago

Closed 9 years ago

Last modified 9 years ago

#33524 closed defect (bug) (fixed)

json_encode giving invalid UTF-8 sequence followed by maxed CPU's

Reported by: thehrisworld's profile thehrisworld Owned by: pento's profile pento
Milestone: 4.4 Priority: normal
Severity: normal Version: 4.1
Component: General Keywords: needs-testing has-patch
Focuses: Cc:

Description

Linux (latest version and updates from AWS)
Apache 2.2.31
PHP 5.3.29
MySQL 5.5.42

RE:
PHP Warning: json_encode(): Invalid UTF-8 sequence in argument in /var/www/html/wp-includes/functions.php

The above error couldn't be resolved until we rolled back to WP 4.2.4 from 4.3 -- the lines of code involved (2655-2667):


function wp_json_encode( $data, $options = 0, $depth = 512 ) {
	/*
	 * json_encode() has had extra params added over the years.
	 * $options was added in 5.3, and $depth in 5.5.
	 * We need to make sure we call it with the correct arguments.
	 */
	if ( version_compare( PHP_VERSION, '5.5', '>=' ) ) {
		$args = array( $data, $options, $depth );
	} elseif ( version_compare( PHP_VERSION, '5.3', '>=' ) ) {
		$args = array( $data, $options );
	} else {
		$args = array( $data );
	}

We would receive the error notice several times in the httpd error_log, even with all plugins deactivated (even moved them to a tmp directory just to be sure). This was accompanied by our CPU's maxing out followed by crashing. Truthfully, we have no clue if this is the cause however our actions noted above stopped the error as well as stabilized the server.

We have setup a test environment but have not completed it yet to see if this error persists with the latest version for all four systems (LAMP)...

Please advise what to do next -- again, we believe our site is stabilized with the rollback to WP 4.2.4...

Attachments (1)

33524.diff (524 bytes) - added by pento 9 years ago.

Download all attachments as: .zip

Change History (11)

#1 @pento
9 years ago

  • Keywords reporter-feedback added
  • Milestone Awaiting Review deleted
  • Version 4.3 deleted

Thanks for the bug report, @thehrisworld!

wp_json_encode() (and related functions) haven't changed since it was introduced in WordPress 4.1, so presumably this behaviour is being caused by something further up the call stack.

Please let us know if you're able to reproduce this behaviour in your test environment.

#2 @thehrisworld
9 years ago

Thanks, good to know -- since stepping back a version, our server has been maintaining 6-40% with occasional expected spikes.

And looking at the logs, lo and behold the error notice is still there... harrumph for us! :-)

Our httpd error_log since the last restart (19 hours ago from this post)...

[Mon Aug 24 02:21:11 2015] [error] [client 203.190.36.79] client sent HTTP/1.1 request without hostname (see RFC2616 section 14.23): /w00tw00t.at.ISC.SANS.DFind:)
[Mon Aug 24 08:30:44 2015] [error] [client 173.245.56.161] PHP Warning:  json_encode(): Invalid UTF-8 sequence in argument in /var/www/html/wp-includes/functions.php on line 2669
[Mon Aug 24 08:30:45 2015] [error] [client 173.245.56.161] PHP Warning:  json_encode(): Invalid UTF-8 sequence in argument in /var/www/html/wp-includes/functions.php on line 2669
[Mon Aug 24 08:30:45 2015] [error] [client 173.245.56.161] PHP Warning:  json_encode(): Invalid UTF-8 sequence in argument in /var/www/html/wp-includes/functions.php on line 2669
[Mon Aug 24 11:56:25 2015] [error] [client 173.245.56.161] PHP Warning:  json_encode(): Invalid UTF-8 sequence in argument in /var/www/html/wp-includes/functions.php on line 2669
[Mon Aug 24 11:56:27 2015] [error] [client 173.245.56.161] PHP Warning:  json_encode(): Invalid UTF-8 sequence in argument in /var/www/html/wp-includes/functions.php on line 2669
[Mon Aug 24 13:56:55 2015] [error] [client 91.200.13.64] client denied by server configuration: /var/www/html/wp-content/plugins/hello.php, referer: http://54.84.79.85/
[Mon Aug 24 13:56:55 2015] [error] [client 91.200.13.64] client denied by server configuration: /var/www/html/wp-content/218.php, referer: http://54.84.79.85/
[Mon Aug 24 13:56:56 2015] [error] [client 91.200.13.64] client denied by server configuration: /var/www/html/wp-content/uploads/shwso.php, referer: http://54.84.79.85/
[Mon Aug 24 15:00:26 2015] [error] [client 188.114.98.46] PHP Warning:  json_encode(): Invalid UTF-8 sequence in argument in /var/www/html/wp-includes/functions.php on line 2669, referer: http://www.thehrisworld.com/wp-admin/
[Mon Aug 24 15:00:43 2015] [error] [client 188.114.98.46] PHP Warning:  json_encode(): Invalid UTF-8 sequence in argument in /var/www/html/wp-includes/functions.php on line 2669, referer: http://www.thehrisworld.com/wp-admin/plugins.php
[root@ip-172-31-34-115 ~]#

[And now we have a new message concerning 91.200.13.64 which is iis.net -- more homework....]

Not mentioned originally: we have minimized the plugins and are activating only 3 an hour or so JUST to be sure they are not the culprits...

Will advise as soon as anything else changes or is discovered...

#3 @thehrisworld
9 years ago

  • Keywords needs-testing added; reporter-feedback removed

@pento
9 years ago

#4 @pento
9 years ago

  • Keywords has-patch added
  • Milestone set to 4.4
  • Version set to 4.1

Ah, I see. It looks like this is an expected warning - you're getting a warning for the first json_encode() call (which checks to see if it can safely encode), but not for the second one, which happens after the data has been sanitised.

I think it's reasonable for us to just suppress this warning, as the point of wp_json_encode() is to handle the situations that cause that warning to occur.

Could I get you to try 33524.diff, and see if that removes the warning?

#5 @thehrisworld
9 years ago

Commented the original line out, inserted the line you provided...
the json error disappeared and now has an error for a missing table...
THAT I can handle on MySQL...

Just went through this exercise with the original json line...
-- Moved all the plugins to a tmp folder, installed 10 random plugins, still had json errors...
-- Deactivated and removed all plugins...
-- Changed theme to WP 2015, installed the same 10 plugins, no json errors...
-- Changed theme or original, json errors appeared immediately (refreshing the plugins pages does it)...
We will be replacing the theme if the maker doesn't have a solution... (Elegant Themes; Divi)

Will keep the json line you provided in place which should be reverted once the new theme is in place...

Everything is good here, do I need to close this or do you?

#6 @pento
9 years ago

Thanks for testing, @thehrisworld - I'll commit the fix shortly. :-)

#7 @pento
9 years ago

  • Owner set to pento
  • Resolution set to fixed
  • Status changed from new to closed

In 33747:

When wp_json_encode() calls json_encode(), the latter will generate warnings if the string contains non-UTF-8 characters. No-one likes warnings, so we need to do something about that.

The good news is, the point of wp_json_encode() is to handle those non-UTF-8 characters. It'll totally just fix them up, no problem.

Anyway, we can just ignore those warnings.

Fixes #33524.

#8 @thehrisworld
9 years ago

Thanks Gary!

#9 @thehrisworld
9 years ago

  • Keywords close added

#10 @DrewAPicture
9 years ago

  • Keywords close removed
Note: See TracTickets for help on using tickets.