Page MenuHomePhabricator

EventBus::send incorrectly assumes that JSON response can be converted to an associative array
Closed, ResolvedPublicPRODUCTION ERROR

Description

Error
message
[{reqId}] {exception_url}   PHP Warning: json_decode() expects parameter 1 to be string, array given
trace
from /srv/mediawiki/php-1.43.0-wmf.15/includes/json/FormatJson.php(151)
#0 [internal function]: MWExceptionHandler::handleError(integer, string, string, integer, array)
#1 /srv/mediawiki/php-1.43.0-wmf.15/includes/json/FormatJson.php(151): json_decode(array, boolean)
#2 /srv/mediawiki/php-1.43.0-wmf.15/extensions/EventBus/includes/EventBus.php(390): MediaWiki\Json\FormatJson::decode(array, boolean)
#3 /srv/mediawiki/php-1.43.0-wmf.15/extensions/EventBus/includes/Adapters/JobQueue/JobQueueEventBus.php(120): MediaWiki\Extension\EventBus\EventBus->send(array, integer)
#4 /srv/mediawiki/php-1.43.0-wmf.15/includes/jobqueue/JobQueue.php(380): MediaWiki\Extension\EventBus\Adapters\JobQueue\JobQueueEventBus->doBatchPush(array, integer)
#5 /srv/mediawiki/php-1.43.0-wmf.15/includes/jobqueue/JobQueue.php(352): JobQueue->batchPush(array, integer)
#6 /srv/mediawiki/php-1.43.0-wmf.15/includes/jobqueue/JobQueueGroup.php(157): JobQueue->push(array)
#7 /srv/mediawiki/php-1.43.0-wmf.15/extensions/IPInfo/src/Rest/Handler/IPInfoHandler.php(212): JobQueueGroup->push(array)
#8 /srv/mediawiki/php-1.43.0-wmf.15/extensions/IPInfo/src/Rest/Handler/IPInfoHandler.php(182): MediaWiki\IPInfo\Rest\Handler\IPInfoHandler->logAccess(MediaWiki\User\User, string, string)
#9 /srv/mediawiki/php-1.43.0-wmf.15/includes/Rest/SimpleHandler.php(40): MediaWiki\IPInfo\Rest\Handler\IPInfoHandler->run(integer)
#10 /srv/mediawiki/php-1.43.0-wmf.15/includes/Rest/Module/Module.php(380): MediaWiki\Rest\SimpleHandler->execute()
#11 /srv/mediawiki/php-1.43.0-wmf.15/includes/Rest/Module/Module.php(269): MediaWiki\Rest\Module\Module->executeHandler(MediaWiki\IPInfo\Rest\Handler\RevisionHandler)
#12 /srv/mediawiki/php-1.43.0-wmf.15/includes/Rest/Router.php(477): MediaWiki\Rest\Module\Module->execute(string, MediaWiki\Rest\RequestFromGlobals)
#13 /srv/mediawiki/php-1.43.0-wmf.15/includes/Rest/Router.php(446): MediaWiki\Rest\Router->doExecute(string, MediaWiki\Rest\RequestFromGlobals)
#14 /srv/mediawiki/php-1.43.0-wmf.15/includes/Rest/EntryPoint.php(209): MediaWiki\Rest\Router->execute(MediaWiki\Rest\RequestFromGlobals)
#15 /srv/mediawiki/php-1.43.0-wmf.15/includes/MediaWikiEntryPoint.php(200): MediaWiki\Rest\EntryPoint->execute()
#16 /srv/mediawiki/php-1.43.0-wmf.15/rest.php(39): MediaWiki\MediaWikiEntryPoint->run()
#17 /srv/mediawiki/w/rest.php(3): require(string)
#18 {main}

Details

Request URL
/w/rest.php/ipinfo/v0/revision/X?dataContext=infobox&language=en

Event Timeline

Dreamy_Jazz renamed this task from IPInfo REST API raises json_decode warning to EventBus::send incorrectly assumes that response will always be convertible to an array.Jul 30 2024, 7:50 PM
Dreamy_Jazz renamed this task from EventBus::send incorrectly assumes that response will always be convertible to an array to EventBus::send incorrectly assumes that JSON response can be converted to an associative array.

I think this warning was recently introduced in work for T363587: [Event Platform] Instrument EventBus with prometheus MW Statslib.

I think this is just a warning log message, not an uncaught exception, right? The code is checking if the FormatJson::decode fails, but I guess json_decode is logging a warning anyway?

Looking at FormatJson docs, perhaps we should change to use FormatJson::parse instead?

I think this is just a warning log message, not an uncaught exception, right? The code is checking if the FormatJson::decode fails, but I guess json_decode is logging a warning anyway?

Yes, this is just a warning message. However, it does cause logstash spam and they are registered in logstash as an ERROR level so should be solved (even if that is just providing a more descriptive error message).

Relevant code was last added/changed in https://gerrit.wikimedia.org/r/c/mediawiki/extensions/EventBus/+/1049831 for T363587: [Event Platform] Instrument EventBus with prometheus MW Statslib, \cc @gmodena @Ottomata

I think this is just a warning log message, not an uncaught exception, right? The code is checking if the FormatJson::decode fails, but I guess json_decode is logging a warning anyway?

Looking at FormatJson docs, perhaps we should change to use FormatJson::parse instead?

No, parsing isn't failing. EventBus is passing an array instead of a JSON string. It's a type error.

$event = FormatJson::decode( $failurePayload['event'], true );
$streamName = $event['meta']['stream'] ?? self::STREAM_UNKNOWN_NAME;

The warning is saying that $failurePayload['event'] is an array, not a JSON string. decode() returns null for this kind of error. null can in theory be a genuine JSON value, but in practice this is only ever parsing JSON objects, so we can safely assume null is an error.

I don't think this is about handling parse errors. That would be equivalant to removing this part of the instrumentation. The thing you're looking to parse, is presumably somewhere else. This function takes a JSON string. Another possibility, is that it was already parsed and you can't / don't need to parse it again?

Thank you Timo! I haven't had time to look deeply yet. I think you are right!

Change #1060156 had a related patch set uploaded (by Ottomata; author: Ottomata):

[mediawiki/extensions/EventBus@master] EventBus send - Fix json_decode bug in error metric handling

https://gerrit.wikimedia.org/r/1060156

Change #1060156 merged by jenkins-bot:

[mediawiki/extensions/EventBus@master] EventBus send - Fix json_decode bug in error metric handling

https://gerrit.wikimedia.org/r/1060156

Ottomata claimed this task.