Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions sapi/fpm/fpm/fpm_request.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,13 @@ void fpm_request_accepting(void)
return;
}

int idle = proc->request_stage != FPM_REQUEST_ACCEPTING;
proc->request_stage = FPM_REQUEST_ACCEPTING;
proc->tv = now;
fpm_scoreboard_proc_release(proc);

/* idle++, active-- */
fpm_scoreboard_update_commit(1, -1, 0, 0, 0, 0, 0, 0, FPM_SCOREBOARD_ACTION_INC, NULL);
/* Repeated accept attempts leave an already idle worker unchanged. */
fpm_scoreboard_update_commit(idle, -idle, 0, 0, 0, 0, 0, 0, FPM_SCOREBOARD_ACTION_INC, NULL);
}

void fpm_request_reading_headers(void)
Expand All @@ -82,6 +83,7 @@ void fpm_request_reading_headers(void)
return;
}

int active = proc->request_stage == FPM_REQUEST_ACCEPTING;
proc->request_stage = FPM_REQUEST_READING_HEADERS;
proc->tv = now;
proc->accepted = now;
Expand All @@ -98,8 +100,8 @@ void fpm_request_reading_headers(void)
proc->content_length = 0;
fpm_scoreboard_proc_release(proc);

/* idle--, active++, request++ */
fpm_scoreboard_update_commit(-1, 1, 0, 0, 1, 0, 0, 0, FPM_SCOREBOARD_ACTION_INC, NULL);
/* Reused connections stay active, but every request is counted. */
fpm_scoreboard_update_commit(-active, active, 0, 0, 1, 0, 0, 0, FPM_SCOREBOARD_ACTION_INC, NULL);
}

void fpm_request_info(void)
Expand Down
78 changes: 78 additions & 0 deletions sapi/fpm/tests/gh14212-status-keepalive-counters.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
--TEST--
FPM: GH-14212 (Status counters remain accurate over fresh and persistent FastCGI connections)
--EXTENSIONS--
sockets
--SKIPIF--
<?php include "skipif.inc"; ?>
--FILE--
<?php

require_once "tester.inc";

$cfg = <<<EOT
[global]
error_log = {{FILE:LOG}}
[fresh]
listen = {{ADDR[fresh]}}
pm = static
pm.max_children = 1
pm.status_path = /status
[reused]
listen = {{ADDR[reused]}}
pm = static
pm.max_children = 1
pm.status_path = /status
EOT;

$tester = new FPM\Tester($cfg);
$tester->start();
$tester->expectLogStartNotices();

try {
foreach (['fresh' => false, 'reused' => true] as $pool => $keepAlive) {
$pid = null;
for ($i = 1; $i <= 5; $i++) {
$response = $tester->request(
address: '{{ADDR[' . $pool . ']}}',
query: 'json&full',
uri: '/status',
connKeepAlive: $keepAlive,
socketKeepAlive: $keepAlive,
);
$status = json_decode($response->getBody('application/json'), true, flags: JSON_THROW_ON_ERROR);
if (count($status['processes']) !== 1) {
throw new Exception('Expected one actual worker');
}
$currentPid = $status['processes'][0]['pid'];
if ($pid !== null && $currentPid !== $pid) {
throw new Exception('Worker changed during the test');
}
$pid = $currentPid;
foreach (['active processes', 'total processes', 'max active processes'] as $field) {
if ($status[$field] !== 1) {
throw new Exception("Request $i: $field is {$status[$field]}, expected 1");
}
}
if ($status['idle processes'] !== 0) {
throw new Exception('Expected no idle workers while serving the status request');
}
if ($status['accepted conn'] !== $i) {
throw new Exception("Request $i: accepted conn is {$status['accepted conn']}, expected $i");
}
}
}
} finally {
$tester->terminate();
$tester->expectLogTerminatingNotices();
$tester->close();
}

?>
Done
--EXPECT--
Done
--CLEAN--
<?php
require_once "tester.inc";
FPM\Tester::clean();
?>
Loading