The unserialization has failed! - During archive job

Hello there,

I’m facing an issue that I can’t pin down to the root cause. During archiving I get the following error message

ERROR [2024-08-07 12:02:10] 1315761  Got invalid response from API request: ?module=API&method=CoreAdminHome.archiveReports&idSite=13&period=year&date=2024-01-01&format=json&trigger=archivephp. 
Response was '{"result":"error","message":"The unserialization has failed! - in plugin Actions.
#0 \/var\/www\/<url>_4.16.1\/piwik\/core\/ArchiveProcessor\/Loader.php(289): Piwik\\ArchiveProcessor\\PluginsArchiver-&gt;callAggregateAllPlugins() 
#1 \/var\/www\/<url>_4.16.1\/piwik\/core\/ArchiveProcessor\/Loader.php(181): Piwik\\ArchiveProcessor\\Loader-&gt;prepareAllPluginsArchive()
#2 \/var\/www\/<url>_4.16.1\/piwik\/core\/ArchiveProcessor\/Loader.php(164): Piwik\\ArchiveProcessor\\Loader-&gt;insertArchiveData()
#3 \/var\/www\/<url>_4.16.1\/piwik\/core\/ArchiveProcessor\/Loader.php(104): Piwik\\ArchiveProcessor\\Loader-&gt;prepareArchiveImpl()
#4 \/var\/www\/<url>_4.16.1\/piwik\/core\/Context.php(75): Piwik\\ArchiveProcessor\\Loader-&gt;Piwik\\ArchiveProcessor\\{closure}()
#5 \/var\/www\/<url>_4.16.1\/piwik\/core\/ArchiveProcessor\/Loader.php(101): Piwik\\Context::changeIdSite()
#6 \/var\/www\/<url>_4.16.1\/piwik\/plugins\/CoreAdminHome\/API.php(294): Piwik\\ArchiveProcessor\\Loader-&gt;prepareArchive()
#7 [internal function]: Piwik\\Plugins\\CoreAdminHome\\API-&gt;archiveReports()
#8 \/var\/www\/<url>_4.16.1\/piwik\/core\/API\/Proxy.php(244): call_user_func_array()
#9 \/var\/www\/<url>_4.16.1\/piwik\/core\/Context.php(28): Piwik\\API\\Proxy-&gt;Piwik\\API\\{closure}()
#10 \/var\/www\/<url>_4.16.1\/piwik\/core\/API\/Proxy.php(154): Piwik\\Context::executeWithQueryParameters()
#11 \/var\/www\/<url>_4.16.1\/piwik\/core\/API\/Request.php(267): Piwik\\API\\Proxy-&gt;call()
#12 \/var\/www\/<url>_4.16.1\/piwik\/plugins\/API\/Controller.php(45): Piwik\\API\\Request-&gt;process()
#13 [internal function]: Piwik\\Plugins\\API\\Controller-&gt;index()
#14 \/var\/www\/<url>_4.16.1\/piwik\/core\/FrontController.php(637): call_user_func_array()
#15 \/var\/www\/<url>_4.16.1\/piwik\/core\/FrontController.php(169): Piwik\\FrontController-&gt;doDispatch()
#16 \/var\/www\/<url>_4.16.1\/piwik\/core\/dispatch.php(32): Piwik\\FrontController-&gt;dispatch()
#17 \/var\/www\/<url>_4.16.1\/piwik\/index.php(25): require_once('...')
#18 \/var\/www\/<url>_4.16.1\/piwik\/core\/CliMulti\/RequestCommand.php(79): require_once('...')
#19 \/var\/www\/<url>_4.16.1\/piwik\/vendor\/symfony\/console\/Symfony\/Component\/Console\/Command\/Command.php(257): Piwik\\CliMulti\\RequestCommand-&gt;execute()
#20 \/var\/www\/<url>_4.16.1\/piwik\/vendor\/symfony\/console\/Symfony\/Component\/Console\/Application.php(874): Symfony\\Component\\Console\\Command\\Command-&gt;run()
#21 \/var\/www\/<url>_4.16.1\/piwik\/vendor\/symfony\/console\/Symfony\/Component\/Console\/Application.php(195): Symfony\\Component\\Console\\Application-&gt;doRunCommand()
#22 \/var\/www\/<url>_4.16.1\/piwik\/core\/Console.php(108): Symfony\\Component\\Console\\Application-&gt;doRun()
#23 [internal function]: Piwik\\Console-&gt;originDoRun()
#24 \/var\/www\/<url>_4.16.1\/piwik\/core\/Console.php(147): call_user_func()
#25 \/var\/www\/<url>_4.16.1\/piwik\/core\/Access.php(670): Piwik\\Console-&gt;Piwik\\{closure}()
#26 \/var\/www\/<url>_4.16.1\/piwik\/core\/Console.php(145): Piwik\\Access::doAsSuperUser()
#27 \/var\/www\/<url>_4.16.1\/piwik\/core\/Console.php(87): Piwik\\Console-&gt;doRunImpl()
#28 \/var\/www\/<url>_4.16.1\/piwik\/vendor\/symfony\/console\/Symfony\/Component\/Console\/Application.php(126): Piwik\\Console-&gt;doRun()
#29 \/var\/www\/<url>_4.16.1\/piwik\/console(32): Symfony\\Component\\Console\\Application-&gt;run()
#30 {main}, caused by: The unserialization has failed!
#0 \/var\/www\/<url>_4.16.1\/piwik\/core\/DataTable.php(1461): Piwik\\DataTable-&gt;unserializeRows()
#1 \/var\/www\/<url>_4.16.1\/piwik\/core\/DataTable.php(1936): Piwik\\DataTable-&gt;addRowsFromSerializedArray()
#2 \/var\/www\/<url>_4.16.1\/piwik\/core\/ArchiveProcessor.php(400): Piwik\\DataTable::fromSerializedArray()
#3 \/var\/www\/<url>_4.16.1\/piwik\/core\/ArchiveProcessor.php(375): Piwik\\ArchiveProcessor-&gt;getAggregatedDataTableMapFromBlobs()
#4 \/var\/www\/<url>_4.16.1\/piwik\/core\/ArchiveProcessor.php(232): Piwik\\ArchiveProcessor-&gt;aggregateDataTableRecord()
#5 \/var\/www\/<url>_4.16.1\/piwik\/plugins\/Actions\/Archiver.php(642): Piwik\\ArchiveProcessor-&gt;aggregateDataTableRecords()
#6 \/var\/www\/<url>_4.16.1\/piwik\/core\/Plugin\/Archiver.php(108): Piwik\\Plugins\\Actions\\Archiver-&gt;aggregateMultipleReports()
#7 \/var\/www\/<url>_4.16.1\/piwik\/core\/ArchiveProcessor\/PluginsArchiver.php(173): Piwik\\Plugin\\Archiver-&gt;callAggregateMultipleReports()
#8 \/var\/www\/<url>_4.16.1\/piwik\/core\/ArchiveProcessor\/Loader.php(289): Piwik\\ArchiveProcessor\\PluginsArchiver-&gt;callAggregateAllPlugins()
#9 \/var\/www\/<url>_4.16.1\/piwik\/core\/ArchiveProcessor\/Loader.php(181): Piwik\\ArchiveProcessor\\Loader-&gt;prepareAllPluginsArchive()
#10 \/var\/www\/<url>_4.16.1\/piwik\/console(32): Symfony\\Component\\Console\\Application->run()\n
#38 {main}"}'
INFO [2024-08-07 12:02:10] 1315761  Skipped Archiving website id 13, period = year, date = 2024-01-01, segment = '', 0 visits found. Time elapsed: 6.819s

I found the following command to reproduce the error

sudo -u www-data /usr/bin/php8.2 -q  /var/www/<url>_4.16.1/piwik/console climulti:request -q --matomo-domain='<url>' --superuser 'module=API&method=CoreAdminHome.archiveReports&idSite=13&period=year&date=2024-01-01&format=json&trigger=archivephp' 

I traced the execution with xdebug and found

423.4460  502076712 -> Piwik\DataAccess\ArchiveSelector::uncompress($data ='x<9C><EC><FD>i<8F><E2>X<B6>\006\n<FF><97><96><EE><F9><F2><CA>՞<87>.<B5><8E>\030\rx\000\f<B6><C1>_<90>\r<C6><F3><80>m0<E6><AA><FF><FB><85> <D8>63TFd<D7>{2K<AA><AC><A8><C8>`;<BC>ֳ<E6>a<EB><FF>b<98>^?<FD><BF>ο<B0>?<D3>^?a0I<FE><EB>\037<FA><BF>><BE>\001<FF><A9><FF>\v;}<81><90><FF><FA>^?<D3>^?\021<FF><FA><87><AF>\033<A6><FF><8F><FD>Ϣ<F8><BF><FE><F1>υcB~d<A6><EB>Ђ<9C>4<83><BC>(IL/<FB>ǟο<D0>ÿ<C4><FE>\017<E4><F0>\025v<F8>\003<C1>\016ߣ0<EA><F0>\r<F8><CF><C5><FE><E4><FD>\027ȟ<CE><F1>\v<F4>O<F1><F0>\037<EC>O<E3><E3><FF>\021\004<9C><C0><9C><8E>A\017^?A^?\034<83>\034<CF><FA><F8>.<FA><F9><<84>8}A<9F>><81><90>\037<DF><FA><CF><FE><AB><C3>k\034<DE>\002<FB><D7>?<D6><C9><C7>;<D0>Կ<FE>agY<9C><FE><EB><9F><FF><CC><F3><FC>\017OO\022<C7>ܿ<81><9E><9A>Ih<FE><B1>0<FF>y<FA>\026<E4><84><CB>(<FD><A7><BB><B6><CC>p\021GI<A6><FB><FF>\\<AD><9D><DD>}\022<FC><E7>@S<F1><F4>䒒<F0>%%<C9>\a<94><FC>_<DD>շ<B3>e<94>\004<FF>F<FE>g<96>\'z\034<9B><C9><C7><FF><EB>ٿ\027<C9>:<D6><FD><D9><E1>GN\024\'<E1>Ӌ<93>̉<E2>\bL<BC>N<F0><E3>\001<87>_\024=<A2>B<FF>\027u<F8><E2><F0>w<F8><C7>C<8E><A7>\034N<C7>1<FA><F8><C3><FB>o<FD>\001<E3>8q:\004<FC>\022<F8><91>)<87><8F><FC><E7><F0><D9><CA>a<C8><C5>a\bBU\016<83><E9><EB>Ð<CA>a<FF><B9><CD>P\004<FD>><8E><BE>Ί<92><F3><E8>\031<E7><A9>K<CE><D3>{<C6>/\023<DD>2!<E4><C4>@\n?<BD><EE><91>\002G\006R<E4><EB>\034<A4><F0><93>$<E0>\'<FC><E3>\'<9E>~<FC>.gl<C0>.y\n<C3>\0256`$\006W<FF>!<AF><98><82><9D>q\0309<E3>0zq6<C6><C0>4Re2<81>^3\031=;<F0>\f^?<D4><C5>y'...) /var/www/<url>_4.16.1/piwik/core/DataAccess/ArchiveSelector.php:497
423.4460  502076712 > gzuncompress($data ='x<9C><EC><FD>i<8F><E2>X<B6>\006\n<FF><97><96><EE><F9><F2><CA>՞<87>.<B5><8E>\030\rx\000\f<B6><C1>_<90>\r<C6><F3><80>m0<E6><AA><FF><FB><85> <D8>63TFd<D7>{2K<AA><AC><A8><C8>`;<BC>ֳ<E6>a<EB><FF>b<98>^?<FD><BF>ο<B0>?<D3>^?a0I<FE><EB>\037<FA><BF>><BE>\001<FF><A9><FF>\v;}<81><90><FF><FA>^?<D3>^?\021<FF><FA><87><AF>\033<A6><FF><8F><FD>Ϣ<F8><BF><FE><F1>υcB~d<A6><EB>Ђ<9C>4<83><BC>(IL/<FB>ǟο<D0>ÿ<C4><FE>\017<E4><F0>\025v<F8>\003<C1>\016ߣ0<EA><F0>\r<F8><CF><C5><FE><E4><FD>\027ȟ<CE><F1>\v<F4>O<F1><F0>\037<EC>O<E3><E3><FF>\021\004<9C><C0><9C><8E>A\017^?A^?\034<83>\034<CF><FA><F8>.<FA><F9><<84>8}A<9F>><81><90>\037<DF><FA><CF><FE><AB><C3>k\034<DE>\002<FB><D7>?<D6><C9><C7>;<D0>Կ<FE>agY<9C><FE><EB><9F><FF><CC><F3><FC>\017OO\022<C7>ܿ<81><9E><9A>Ih<FE><B1>0<FF>y<FA>\026<E4><84><CB>(<FD><A7><BB><B6><CC>p\021GI<A6><FB><FF>\\<AD><9D><DD>}\022<FC><E7>@S<F1><F4>䒒<F0>%%<C9>\a<94><FC>_<DD>շ<B3>e<94>\004<FF>F<FE>g<96>\'z\034<9B><C9><C7><FF><EB>ٿ\027<C9>:<D6><FD><D9><E1>GN\024\'<E1>Ӌ<93>̉<E2>\bL<BC>N<F0><E3>\001<87>_\024=<A2>B<FF>\027u<F8><E2><F0>w<F8><C7>C<8E><A7>\034N<C7>1<FA><F8><C3><FB>o<FD>\001<E3>8q:\004<FC>\022<F8><91>)<87><8F><FC><E7><F0><D9><CA>a<C8><C5>a\bBU\016<83><E9><EB>Ð<CA>a<FF><B9><CD>P\004<FD>><8E><BE>Ί<92><F3><E8>\031<E7><A9>K<CE><D3>{<C6>/\023<DD>2!<E4><C4>@\n?<BD><EE><91>\002G\006R<E4><EB>\034<A4><F0><93>$<E0>\'<FC><E3>\'<9E>~<FC>.gl<C0>.y\n<C3>\0256`$\006W<FF>!<AF><98><82><9D>q\0309<E3>0zq6<C6><C0>4Re2<81>^3\031=;<F0>\f^?<D4><C5>y'...) /var/www/<url>_4.16.1/piwik/core/DataAccess/ArchiveSelector.php:340
423.6420  502076800 -> Piwik\ErrorHandler::errorHandler($errno = 2, $errstr = 'gzuncompress(): data error', $errfile = '/var/www/<url>_4.16.1/piwik/core/DataAccess/ArchiveSelector.php', $errline = 340) /var/www/<url>_4.16.1/piwik/core/DataAccess/ArchiveSelector.php:340
423.6421  502076800 -> Piwik\ErrorHandler::createLogMessage($errno = 2, $errstr = 'gzuncompress(): data error', $errfile = '/var/www/<url>_4.16.1/piwik/core/DataAccess/ArchiveSelector.php', $errline = 340) /var/www/<url>_4.16.1/piwik/core/ErrorHandler.php:137
423.6421  502076800 -> class_exists($class = 'Piwik\\Version') /var/www/<url>_4.16.1/piwik/core/ErrorHandler.php:195
423.6421  502077024 -> Piwik\ErrorHandler::getErrNoString($errno = 2) /var/www/<url>_4.16.1/piwik/core/ErrorHandler.php:198
423.6421  502077024 -> sprintf($format = '%s(%d): %s - %s - Matomo 4.16.1 - Please report this message in the Matomo forums: https://forum.matomo.org (please do a search first as it might have been reported already)', ...$values = variadic(0 => '/var/www/<url>_4.16.1/piwik/core/DataAccess/ArchiveSelector.php', 1 => 340, 2 => 'Warning', 3 => 'gzuncompress(): data error')) /var/www/<url>_4.16.1/piwik/core/ErrorHandler.php:194
423.6421  502076800 -> error_reporting() /var/www/<url>_4.16.1/piwik/core/ErrorHandler.php:140
423.6421  502076712 -> Piwik\Archive\Chunk->isRecordNameAChunk($recordName = 'Actions_actions_url_chunk_0_99') /var/www/<url>_4.16.1/piwik/core/DataAccess/ArchiveSelector.php:498
423.6422  502076712 -> Piwik\Archive\Chunk->getEndPosOfChunkAppendix($recordName = 'Actions_actions_url_chunk_0_99') /var/www/<url>_4.16.1/piwik/core/Archive/Chunk.php:77
423.6422  502076712 -> Piwik\Archive\Chunk->getAppendix() /var/www/<url>_4.16.1/piwik/core/Archive/Chunk.php:134
423.6422  502076712 -> strpos($haystack = 'Actions_actions_url_chunk_0_99', $needle = '_chunk_') /var/www/<url>_4.16.1/piwik/core/Archive/Chunk.php:134
423.6422  502076712 -> Piwik\Archive\Chunk->getAppendix() /var/www/<url>_4.16.1/piwik/core/Archive/Chunk.php:140
423.6422  502076712 -> strlen($string = '_chunk_') /var/www/<url>_4.16.1/piwik/core/Archive/Chunk.php:140
423.6422  502076712 -> substr($string = 'Actions_actions_url_chunk_0_99', $offset = 26) /var/www/<url>_4.16.1/piwik/core/Archive/Chunk.php:84
423.6422  502076744 -> Piwik\Archive\Chunk->isChunkRange($blobId = '0_99') /var/www/<url>_4.16.1/piwik/core/Archive/Chunk.php:86
423.6422  502076744 -> explode($separator = '_', $string = '0_99') /var/www/<url>_4.16.1/piwik/core/Archive/Chunk.php:91
423.6422  502076992 -> count($value = [0 => '0', 1 => '99']) /var/www/<url>_4.16.1/piwik/core/Archive/Chunk.php:93
423.6422  502076992 -> is_numeric($value = '0') /var/www/<url>_4.16.1/piwik/core/Archive/Chunk.php:93
423.6422  502076992 -> is_numeric($value = '99') /var/www/<url>_4.16.1/piwik/core/Archive/Chunk.php:93
423.6422  502076712 -> Piwik\Common::safe_unserialize($string = FALSE, $allowedClasses = ???, $rethrow = ???) /var/www/<url>_4.16.1/piwik/core/DataAccess/ArchiveSelector.php:500
423.6422  502077088 -> unserialize($data = FALSE, $options = ['allowed_classes' => FALSE]) /var/www/<url>_4.16.1/piwik/core/Common.php:300
423.6422  502057232 -> is_array($value = FALSE) /var/www/<url>_4.16.1/piwik/core/DataAccess/ArchiveSelector.php:501
423.6423  500000584 -> Piwik\ArchiveProcessor->getSubtableIdFromBlobName($recordName = 'Actions_actions_url_chunk_0_99') /var/www/<url>_4.16.1/piwik/core/ArchiveProcessor.php:398
423.6423  500000584 -> explode($separator = '_', $string = 'Actions_actions_url_chunk_0_99') /var/www/<url>_4.16.1/piwik/core/ArchiveProcessor.php:465
423.6423  500000992 -> end($array = [0 => 'Actions', 1 => 'actions', 2 => 'url', 3 => 'chunk', 4 => '0', 5 => '99']) /var/www/<url>_4.16.1/piwik/core/ArchiveProcessor.php:466
423.6423  500000992 -> is_numeric($value = '99') /var/www/<url>_4.16.1/piwik/core/ArchiveProcessor.php:468
423.6423  500000616 -> Piwik\DataTable::fromSerializedArray($data = FALSE) /var/www/<url>_4.16.1/piwik/core/ArchiveProcessor.php:400
423.6423  500000936 -> Piwik\DataTable->__construct() /var/www/<url>_4.16.1/piwik/core/DataTable.php:1935
423.6423  500000936 -> Piwik\DataTable\Manager::getInstance() /var/www/<url>_4.16.1/piwik/core/DataTable.php:350
423.6423  500000936 -> Piwik\DataTable\Manager->addTable($table = class Piwik\DataTable { protected $rows = []; protected $currentId = NULL; protected $depthLevel = 0; protected $indexNotUpToDate = TRUE; protected $rebuildIndexContinuously = FALSE; protected $tableSortedBy = FALSE; protected $queuedFilters = []; protected $disabledFilters = []; protected $rowsCountBeforeLimitFilter = 0; protected $enableRecursiveSort = FALSE; protected $enableRecursiveFilters = FALSE; protected $rowsIndexByLabel = []; protected $summaryRow = NULL; protected $totalsRow = NULL; protected $metadata = []; protected $maximumAllowedRows = 0 }) /var/www/<url>_4.16.1/piwik/core/DataTable.php:350
423.6423  500001632 -> Piwik\DataTable->addRowsFromSerializedArray($serialized = FALSE) /var/www/<url>_4.16.1/piwik/core/DataTable.php:1936
423.6423  500001632 -> Piwik\DataTable->unserializeRows($serialized = FALSE) /var/www/<url>_4.16.1/piwik/core/DataTable.php:1461
423.6423  500001632 -> str_replace($search = [0 => 'O:39:"Piwik\\DataTable\\Row\\DataTableSummaryRow"', 1 => 'O:19:"Piwik\\DataTable\\Row"', 2 => 'O:36:"Piwik_DataTable_Row_DataTableSummary"', 3 => 'O:19:"Piwik_DataTable_Row"'], $replace = 'O:29:"Piwik_DataTable_SerializedRow"', $subject = FALSE) /var/www/<url>_4.16.1/piwik/core/DataTable.php:1434
423.6423  500001632 -> Piwik\Common::safe_unserialize($string = '', $allowedClasses = [0 => 'Piwik\\DataTable\\Row', 1 => 'Piwik\\DataTable\\Row\\DataTableSummaryRow', 2 => 'Piwik_DataTable_SerializedRow'], $rethrow = ???) /var/www/<url>_4.16.1/piwik/core/DataTable.php:1435
423.6423  500002008 -> unserialize($data = '', $options = ['allowed_classes' => [0 => 'Piwik\\DataTable\\Row', 1 => 'Piwik\\DataTable\\Row\\DataTableSummaryRow', 2 => 'Piwik_DataTable_SerializedRow']]) /var/www/<url>_4.16.1/piwik/core/Common.php:300
423.6424  500017960 -> Exception->__construct($message = 'The unserialization has failed!') /var/www/<url>_4.16.1/piwik/core/DataTable.php:1442

Unfortunately this doesn’t help me narrowing down the root cause. I guess that there is some buggy tracking data in the database, or something like that. As it only affects this siteid and only the year period during archiving.

Does anybody have an idea how I can narrow this down?

Does anybody have a clue about this?

Hi @dwe
Can you check if you have any error (seem to be related to your error) in the Matomo (web application) error log ifle?
Maybe the archiving process takes too much memory. Try this:

Hi @heurteph-ei ,
thank you for your reply. Yes I’m seeing the exact same error message in the Matomo logfile. I could paste it, but since it’s identicalt I don’t think this would help.

The Matomo instance is only tracking 17 websites. We’re archiving all websites every hour. I checked resources for RAM issues, but there’s no bottleneck.

However I increased RAM limits for PHP to 4GB to make sure and I tried setting “–max-websites-to-process=5”. Unfortunately it didn’t help.

The affected website only has 2 million visits in 2024 so far. A much larger website with 14 million visits, is archived without any issues. The issue occurs only during processing the whole year (&date=2024-01-01). That’s why my gut tells me, that there is corrupt data breaking the archive process.

But I’m having no clue at all.

Same here.

This …

php -d memory_limit=2048M -d max_execution_time=900 -d max_input_vars=5000 ./console core:archive --force-all-websites --url=https://domain

… results in multiple errors like this …

Error unserializing the following response from ?module=API&method=CoreAdminHome.archiveReports&id...&trigger=archivephp

I’ve added the following to /config/config.php

return [

    'observers.global' => DI\add(array(
        array('CliMulti.supportsAsync', DI\value(function (&$supportsAsync) {
            $supportsAsync = false;
        })),
    ))
];

This allowed me to determine in the log that the queries hit “502 Bad Gateway”. Of course, this cannot be unserialized.

Matomo: 5.1.2 (All Systemchecks are valid/ok)
PHP: 8.0 - 8.3 (Tested it with all versions)
MariaDB
Nginx
128GB ECC Ram

just a guess: maybe max_execution_time=900 is too short. on our server archive process can take up to 40min, so I ended using max_execution_time=0 (no limit). Also, if you have 128G Ram, why not give more to PHP? We use 32G out of 64G (this of course depends on what else you are running on that server.)

btw, if you call it like that you dont need to set max_input_vars. it specifies the maximum number of input variables that may be accepted through $_GET, $_POST, and $_COOKIE arrays. The Matomo archive cron job is executed via the CLI, not through a web server. Since it’s running from the command line, it doesn’t process $_GET, $_POST, or $_COOKIE variables, which are associated with HTTP requests.

the reason for that error could also be something like incorrect file permissions or some other server ressource problem. but maybe you try more memory and max_execution_time=0 to see if that helps.

Thanks for your reply @soardiac

The same error also occurs if I omit the additional parameters. The size of the working memory is actually also ok here, as the failure is obviously the result of a bad gateway occurring frequently during the API call, as described. There are no timeouts or anything similar.

yes, the “Bad Gateway” means that ngnix (your webserver) is not getting a valid answer from an upstream server, most likely PHP, for whatever reason. ressources limitation or timeout settings are my best guess (I am no pro about that). if not max_execution_time it could be request_terminate_timeout or something entirely unknown to me. or pm-max_childrentoo low.

or it could be a timeout config issue with nginx, like fastcgi_read_timeout or fastcgi_connect_timeout.
I just checked: we added fastcgi_read_timeout = 7100; in our nginx config. So it probably once was an issue and this helped.

btw, we also have these Apache settings added:

FcgidIOTimeout 7100
FcgidBusyTimeout 7100
FcgidMaxRequestsPerProcess 500

(7100 because our archiving runs every 2 hours.)

@soardiac
Thank you again. The problem is that there are no timeouts. The errors occur instantly, but sporadically, i.e. not with every request, but at the same time also with the same requests that sometimes work.
I have already experimented a lot with the max-execution variables and post sizes (nginx and php). The interesting thing is that it has only happened since the patch to 5.1.2 (previously 5.0.x)