Skip to content

Bug: Debug Toolbar History::setFiles() throws ErrorException on unlink() when concurrent requests prune the same history file #10786

Closed

Description

@Terrorboy

PHP Version

8.5

CodeIgniter4 Version

4.7.6

CodeIgniter4 Installation Method

Composer (using codeigniter4/appstarter)

Which operating systems have you tested for this bug?

Linux

Which server did you use?

fpm-fcgi

Environment

development

Database

MariaDB 11.8

What happened?

With the Debug Toolbar enabled (CI_ENVIRONMENT = development), we intermittently get this in the log:

CRITICAL - 2026-11-19 16:54:30 --> ErrorException: unlink(WRITEPATH/debugbar/debugbar_1795074759.544353.json): No such file or directory

CodeIgniter\Debug\Toolbar\Collectors\History::setFiles() deletes the oldest history files when there are more than $maxHistory:

foreach (array_reverse($filenames) as $filename) {
    $counter++;

    // Oldest files will be deleted
    if ($limit >= 0 && $counter > $limit) {
        unlink($filename);

        continue;
    }

setFiles() runs every time the toolbar data is requested (?debugbar_time=..., via Toolbar::format()). When a page fires several requests at almost the same time (e.g. htmx / fetch calls on an admin dashboard), two PHP-FPM workers can run setFiles() concurrently. Both glob() the same list, both decide the same oldest file is over the limit, the first one deletes it, and the second unlink() fails with a warning. In development the warning is converted to an ErrorException, so it is logged as CRITICAL.

The same code is still in the current develop branch.

Steps to Reproduce

  1. Enable the Debug Toolbar (CI_ENVIRONMENT = development, toolbar filter in after), keep $maxHistory = 20 (default).
  2. Make sure there are already more than 20 files in WRITEPATH/debugbar/.
  3. Open a page that sends several requests in parallel (or open the same page in several tabs at once), so that the toolbar data (?debugbar_time=...) is loaded by multiple PHP-FPM workers at nearly the same moment.
  4. Repeat a few times — occasionally one request logs ErrorException: unlink(...debugbar_*.json): No such file or directory.

It is a race condition, so it does not happen on every request (for us about 1–2 times per day of normal work).

Expected Output

Pruning old history files should be safe under concurrent requests: if the file has already been removed by another request, it should simply be skipped, without a warning / ErrorException.

Anything else?

A possible fix is to ignore files that are already gone, e.g.:

if ($limit >= 0 && $counter > $limit) {
    if (is_file($filename)) {
        @unlink($filename);
    }

    continue;
}

(is_file() alone still leaves a small window between the check and unlink(), so suppressing or catching the warning is probably needed as well.)

Related but different: #9504 / #9506 ($maxHistory = 0).

Activity

  1. added
    bug
    on Nov 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions