Page MenuHomePhabricator

Premature access to service container
Open, HighPublic

Description

Seems mw phpunit tests result in some deprecated logspam after rMW47bb958f039c: Deprecate premature instantiation of services. from T153256: Unable to overwrite services using MediaWikiServices hook

03:02:02 PHP Deprecated:  Premature access to service container [Called from AbstractContent::getContentHandlerFactory in /workspace/src/includes/content/AbstractContent.php at line 103] in /workspace/src/includes/debug/MWDebug.php on line 376
03:02:02 PHP Deprecated:  Premature access to service 'HookContainer' [Called from MediaWiki\MediaWikiServices::getInstance in /workspace/src/includes/MediaWikiServices.php at line 243] in /workspace/src/includes/debug/MWDebug.php on line 376
03:02:02 PHP Deprecated:  Premature access to service 'ObjectFactory' [Called from Wikimedia\Services\ServiceContainer::{closure} in /workspace/src/includes/ServiceWiring.php at line 497] in /workspace/src/includes/debug/MWDebug.php on line 376
03:02:02 PHP Deprecated:  Premature access to service 'ContentHandlerFactory' [Called from AbstractContent::getContentHandlerFactory in /workspace/src/includes/content/AbstractContent.php at line 103] in /workspace/src/includes/debug/MWDebug.php on line 376
03:02:02 PHP Deprecated:  Premature access to service 'MainConfig' [Called from Wikimedia\Services\ServiceContainer::{closure} in /workspace/src/includes/ServiceWiring.php at line 271] in /workspace/src/includes/debug/MWDebug.php on line 376
03:02:02 PHP Deprecated:  Premature access to service 'ConfigFactory' [Called from Wikimedia\Services\ServiceContainer::{closure} in /workspace/src/includes/ServiceWiring.php at line 696] in /workspace/src/includes/debug/MWDebug.php on line 376
03:02:02 PHP Deprecated:  Premature access to service 'BootstrapConfig' [Called from Wikimedia\Services\ServiceContainer::{closure} in /workspace/src/includes/ServiceWiring.php at line 249] in /workspace/src/includes/debug/MWDebug.php on line 376

Partial list of extensions to fix:

  • ArticleRatings
  • FundraisingEmailUnsubscribe
  • GoogleLogin

Event Timeline

DannyS712 updated the task description. (Show Details)

I'm confused - does this happen in production? Or does it make tests fail on the extension repos? If so, which tests?

I'm confused - does this happen in production? Or does it make tests fail on the extension repos? If so, which tests?

It cause logspam on various core patches in CI

https://gerrit.wikimedia.org/r/c/mediawiki/core/+/655134

https://integration.wikimedia.org/ci/job/wmf-quibble-core-vendor-mysql-php72-docker/45491/consoleFull

I munged some things so I could run phpunit on an instance in deployment-prep and got the following errors there (but not in a local install): P14230

GoogleLogin now errors with session_id(): Cannot change session id when headers already sent; is that distinct from this, or the same issue?

GoogleLogin now errors with session_id(): Cannot change session id when headers already sent; is that distinct from this, or the same issue?

It's much probably the same. That's one of the common errors that can happen in presence of PHP warnings/notices. In this case, triggering the deprecation warning causes PHP to output the deprecation text, together with some headers, thus making calls like session_id() fail. As a partial confirmation, the last error from https://integration.wikimedia.org/ci/job/quibble-vendor-mysql-php72-noselenium-docker/62789/console says:

Cannot modify header information - headers already sent by (output started at /workspace/src/includes/debug/MWDebug.php:376)

and in fact, MWDebug.php:376 is inside the sendRawDeprecated method.

Can you amend the messages to include a list of callers? In my developer environment the log is full of logpsam like below and it doesn't help figuring out what is causing it:

[11-Mar-2021 10:24:11 UTC] PHP Deprecated:  Premature access to service container [Called from Hooks::run in /home/developer/mediawiki/workdir/includes/Hooks.php at line 135] in /home/developer/mediawiki/workdir/includes/debug/MWDebug.php on line 376
[11-Mar-2021 10:24:11 UTC] PHP Deprecated:  Premature access to service 'HookContainer' [Called from MediaWiki\MediaWikiServices::getInstance in /home/developer/mediawiki/workdir/includes/MediaWikiServices.php at line 249] in /home/developer/mediawiki/workdir/includes/debug/MWDebug.php on line 376
[11-Mar-2021 10:24:11 UTC] PHP Deprecated:  Premature access to service 'ObjectFactory' [Called from Wikimedia\Services\ServiceContainer::{closure} in /home/developer/mediawiki/workdir/includes/ServiceWiring.php at line 530] in /home/developer/mediawiki/workdir/includes/debug/MWDebug.php on line 376
[11-Mar-2021 10:24:11 UTC] PHP Deprecated:  Premature access to service 'StatsdDataFactory' [Called from ObjectCache::newFromParams in /home/developer/mediawiki/workdir/includes/objectcache/ObjectCache.php at line 149] in /home/developer/mediawiki/workdir/includes/debug/MWDebug.php on line 376
[11-Mar-2021 10:24:11 UTC] PHP Deprecated:  Premature access to service 'MainConfig' [Called from Wikimedia\Services\ServiceContainer::{closure} in /home/developer/mediawiki/workdir/includes/ServiceWiring.php at line 1392] in /home/developer/mediawiki/workdir/includes/debug/MWDebug.php on line 376
[11-Mar-2021 10:24:11 UTC] PHP Deprecated:  Premature access to service 'ConfigFactory' [Called from Wikimedia\Services\ServiceContainer::{closure} in /home/developer/mediawiki/workdir/includes/ServiceWiring.php at line 729] in /home/developer/mediawiki/workdir/includes/debug/MWDebug.php on line 376
[11-Mar-2021 10:24:11 UTC] PHP Deprecated:  Premature access to service 'BootstrapConfig' [Called from Wikimedia\Services\ServiceContainer::{closure} in /home/developer/mediawiki/workdir/includes/ServiceWiring.php at line 272] in /home/developer/mediawiki/workdir/includes/debug/MWDebug.php on line 376
[11-Mar-2021 10:24:11 UTC] PHP Deprecated:  Premature access to service container [Called from Hooks::run in /home/developer/mediawiki/workdir/includes/Hooks.php at line 135] in /home/developer/mediawiki/workdir/includes/debug/MWDebug.php on line 376
[11-Mar-2021 10:24:11 UTC] PHP Deprecated:  Premature access to service 'HookContainer' [Called from MediaWiki\MediaWikiServices::getInstance in /home/developer/mediawiki/workdir/includes/MediaWikiServices.php at line 249] in /home/developer/mediawiki/workdir/includes/debug/MWDebug.php on line 376
[11-Mar-2021 10:24:11 UTC] PHP Deprecated:  Premature access to service 'ObjectFactory' [Called from Wikimedia\Services\ServiceContainer::{closure} in /home/developer/mediawiki/workdir/includes/ServiceWiring.php at line 530] in /home/developer/mediawiki/workdir/includes/debug/MWDebug.php on line 376
[11-Mar-2021 10:24:11 UTC] PHP Deprecated:  Premature access to service 'StatsdDataFactory' [Called from ObjectCache::newFromParams in /home/developer/mediawiki/workdir/includes/objectcache/ObjectCache.php at line 149] in /home/developer/mediawiki/workdir/includes/debug/MWDebug.php on line 376
[11-Mar-2021 10:24:11 UTC] PHP Deprecated:  Premature access to service 'MainConfig' [Called from Wikimedia\Services\ServiceContainer::{closure} in /home/developer/mediawiki/workdir/includes/ServiceWiring.php at line 1392] in /home/developer/mediawiki/workdir/includes/debug/MWDebug.php on line 376
[11-Mar-2021 10:24:11 UTC] PHP Deprecated:  Premature access to service 'ConfigFactory' [Called from Wikimedia\Services\ServiceContainer::{closure} in /home/developer/mediawiki/workdir/includes/ServiceWiring.php at line 729] in /home/developer/mediawiki/workdir/includes/debug/MWDebug.php on line 376
[11-Mar-2021 10:24:11 UTC] PHP Deprecated:  Premature access to service 'BootstrapConfig' [Called from Wikimedia\Services\ServiceContainer::{closure} in /home/developer/mediawiki/workdir/includes/ServiceWiring.php at line 272] in /home/developer/mediawiki/workdir/includes/debug/MWDebug.php on line 376

I did this locally and got:

Premature access to service container from require/require_once/ExtensionRegistry->loadFromQueue/ExtensionRegistry->exportExtractedData/SemanticMediaWiki::initExtension/SMW\Setup::initExtension/SMW\Setup::initConnectionProviders/SMW\MediaWiki\MwCollaboratorFactory->newConnectionProvider/SMW\Services\ServicesFactory->getSettings/Onoi\CallbackContainer\CallbackContainerBuilder->singleton/Onoi\CallbackContainer\CallbackContainerBuilder->getReturnValueFromSingletonFor/Onoi\CallbackContainer\CallbackContainerBuilder->getReturnValueFromCallbackHandlerFor/call_user_func_array/SMW\Services\SharedServicesContainer->SMW\Services\{closure}/SMW\Settings->loadFromGlobals/SMW\MediaWiki\HookDispatcher->onSettingsBeforeInitializationComplete/Hooks::run/MediaWiki\MediaWikiServices::getInstance [Called from Hooks::run in /home/developer/mediawiki/workdir/includes/Hooks.php at line 135] in /home/developer/mediawiki/workdir/includes/debug/MWDebug.php on line 376
Premature access to service container from require/require_once/ExtensionRegistry->loadFromQueue/ExtensionRegistry->exportExtractedData/SemanticMediaWiki::initExtension/SMW\Setup::initExtension/SMW\Setup::initConnectionProviders/SMW\Elastic\ElasticFactory->newConnectionProvider/SMW\Services\ServicesFactory->getCache/Onoi\CallbackContainer\CallbackContainerBuilder->singleton/Onoi\CallbackContainer\CallbackContainerBuilder->getReturnValueFromSingletonFor/Onoi\CallbackContainer\CallbackContainerBuilder->getReturnValueFromCallbackHandlerFor/call_user_func_array/SMW\Services\SharedServicesContainer->SMW\Services\{closure}/SMW\CacheFactory->newMediaWikiCompositeCache/SMW\CacheFactory->newMediaWikiCache/ObjectCache::getInstance/ObjectCache::newFromId/ObjectCache::newFromParams/MediaWiki\MediaWikiServices::getInstance [Called from ObjectCache::newFromParams in /home/developer/mediawiki/workdir/includes/objectcache/ObjectCache.php at line 149] in /home/developer/mediawiki/workdir/includes/debug/MWDebug.php on line 376
Premature access to service container from SemanticMediaWiki::initExtension/SMW\Setup::initExtension/SMW\Setup::initConnectionProviders/SMW\Elastic\ElasticFactory->newConnectionProvider/SMW\Services\ServicesFactory->getCache/Onoi\CallbackContainer\CallbackContainerBuilder->singleton/Onoi\CallbackContainer\CallbackContainerBuilder->getReturnValueFromSingletonFor/Onoi\CallbackContainer\CallbackContainerBuilder->getReturnValueFromCallbackHandlerFor/call_user_func_array/SMW\Services\SharedServicesContainer->SMW\Services\{closure}/SMW\CacheFactory->newMediaWikiCompositeCache/SMW\CacheFactory->newMediaWikiCache/ObjectCache::getInstance/ObjectCache::newFromId/ObjectCache::newFromParams/ObjectCache::newAnything/ObjectCache::getInstance/ObjectCache::newFromId/ObjectCache::newFromParams/MediaWiki\MediaWikiServices::getInstance [Called from ObjectCache::newFromParams in /home/developer/mediawiki/workdir/includes/objectcache/ObjectCache.php at line 149] in /home/developer/mediawiki/workdir/includes/debug/MWDebug.php on line 376

So SMW needs to be added to the list of extensions to fix?

Can you amend the messages to include a list of callers? In my developer environment the log is full of logpsam like below and it doesn't help figuring out what is causing it:

I'd think that logging a stack trace is up to the logging code. Including the stack trace in the deprecation message itself is probably not a good idea. And including just the immediate caller is probably not very useful.

Have you tried running PHPUnit tests? In tests, the deprecation would throw and record a stack trace.

In any case, the first suspect in my mind would be code running from a MediaWikiServices hook handler.

I'm afraid running tests locally is a lost cause if you have anything "non-standard":

developer@dev:~/mediawiki/workdir/tests/phpunit (master)$ make safe
php phpunit.php --configuration /home/developer/mediawiki/workdir/tests/phpunit/suite.xml  --exclude-group Broken,Destructive,Stub
Using PHP 7.3.19-1~deb10u1
make: *** [Makefile:50: safe] Error 255

I fixed PHP Fatal error: Interface 'League\OAuth2\Server\Repositories\AccessTokenRepositoryInterface' not found. After that I got this:

developer@dev:~/mediawiki/workdir/tests/phpunit (master)$ make safe
php phpunit.php --configuration /home/developer/mediawiki/workdir/tests/phpunit/suite.xml  --exclude-group Broken,Destructive,Stub
Using PHP 7.3.19-1~deb10u1
[82a44739b8ed756297be0bc2] [no req]   Error: Class 'SMW\Tests\JsonTestCaseScriptRunner' not found
Backtrace:
from /home/developer/mediawiki/workdir/extensions/SemanticMediaWiki/tests/phpunit/Benchmark/BenchmarkJsonScriptRunnerTest.php(18)
#0 /home/developer/mediawiki/workdir/vendor/phpunit/phpunit/src/Util/FileLoader.php(66): include_once()
#1 /home/developer/mediawiki/workdir/vendor/phpunit/phpunit/src/Util/FileLoader.php(54): PHPUnit\Util\FileLoader::load(string)
#2 /home/developer/mediawiki/workdir/vendor/phpunit/phpunit/src/Framework/TestSuite.php(384): PHPUnit\Util\FileLoader::checkAndLoad(string)
#3 /home/developer/mediawiki/workdir/vendor/phpunit/phpunit/src/Framework/TestSuite.php(482): PHPUnit\Framework\TestSuite->addTestFile(string)
#4 /home/developer/mediawiki/workdir/tests/phpunit/suites/ExtensionsTestSuite.php(31): PHPUnit\Framework\TestSuite->addTestFiles(array)
#5 /home/developer/mediawiki/workdir/tests/phpunit/suites/ExtensionsTestSuite.php(43): ExtensionsTestSuite->__construct()
#6 [internal function]: ExtensionsTestSuite::suite(string)
#7 /home/developer/mediawiki/workdir/vendor/phpunit/phpunit/src/Framework/TestSuite.php(463): ReflectionMethod->invoke(NULL, string)
#8 /home/developer/mediawiki/workdir/vendor/phpunit/phpunit/src/Util/Configuration.php(1061): PHPUnit\Framework\TestSuite->addTestFile(string)
#9 /home/developer/mediawiki/workdir/vendor/phpunit/phpunit/src/Util/Configuration.php(907): PHPUnit\Util\Configuration->getTestSuite(DOMElement, array)
#10 /home/developer/mediawiki/workdir/vendor/phpunit/phpunit/src/TextUI/Command.php(964): PHPUnit\Util\Configuration->getTestSuiteConfiguration(string)
#11 /home/developer/mediawiki/workdir/vendor/phpunit/phpunit/src/TextUI/Command.php(203): PHPUnit\TextUI\Command->handleArguments(array)
#12 /home/developer/mediawiki/workdir/tests/phpunit/phpunit.php(75): PHPUnit\TextUI\Command->run(array, boolean)
#13 /home/developer/mediawiki/workdir/maintenance/doMaintenance.php(112): PHPUnitMaintClass->execute()
#14 /home/developer/mediawiki/workdir/tests/phpunit/phpunit.php(134): require_once(string)
#15 {main}
make: *** [Makefile:50: safe] Error 255

I tried to fix that in the same way by adding the extensions' composer.json in composer.local.json, but got this:

Your requirements could not be resolved to an installable set of packages.

  Problem 1
    - remove mediawiki/core dev-master|remove mediawiki/semantic-media-wiki dev-master
    - don't install mediawiki/semantic-media-wiki dev-master|remove mediawiki/core dev-master
    - Installation request for mediawiki/core dev-master -> satisfiable by mediawiki/core[dev-master].
    - Installation request for mediawiki/semantic-media-wiki dev-master#fb5a0a8775d69649b0edd667d1c7719a595b22b7 -> satisfiable by mediawiki/semantic-media-wiki[dev-master].

I gave up. To be fair, I expected to see a flood of failures, but not being able to run tests at all was a bit surprising.

If I had this problem with an extension, I'd just hack the code that triggers the warning to write a stack trace to a file, wait until I had captured a few, and then remove the hack again...

I see no good way to help with that from the core code side. As I said, whether a stack trace is logged or not is up to the logging backend. It can probably be configured somehow.

Just got this here. I know I'm not supposed to use $wgTitle, but I'm not sure what to do here. Pointers would be helpful.

Switch your test from MediaWikiUnitTestCase to MediaWikiIntegrationTestCase. MediaWikiUnitTestCase is specifically designed to prohibit access to global state, Title is all about global state, so you can't use Title in MediaWikiUniteTestCase.

I noticed these warnings this week when using a container to test an un-installed mw. The installation page had deprecation warnings (because of PHP.ini settings). It seems like this is not a good thing, but also a different bug.

Switch your test from MediaWikiUnitTestCase to MediaWikiIntegrationTestCase

... and move it out of the unit subdirectory.

Thanks, that worked!