Skip to content

T16976 findfirst segfault memory cache limit#16988

Open
Lucifer07 wants to merge 5 commits into
phalcon:5.0.xfrom
Lucifer07:T16976-findfirst-segfault-memory-cache-limit
Open

T16976 findfirst segfault memory cache limit#16988
Lucifer07 wants to merge 5 commits into
phalcon:5.0.xfrom
Lucifer07:T16976-findfirst-segfault-memory-cache-limit

Conversation

@Lucifer07

@Lucifer07 Lucifer07 commented May 5, 2026

Copy link
Copy Markdown

Hello!

In raising this pull request, I confirm the following:

Small description of change:

Running findFirst in a high-iteration loop (e.g. 5M iterations) causes a segmentation fault instead of a standard PHP Allowed memory size exhausted error. Two fixes applied:

  1. Phalcon\Mvc\Model\Query::parse() — Added FIFO eviction on internalPhqlCache: when cache exceeds 1024 entries, the oldest 512 are dropped via array_slice, preventing unbounded memory growth from dynamic PHQL conditions.
  2. Phalcon\Db\Result\PdoResult::__destruct() — Added destructor that calls closeCursor() on the PDOStatement and nullifies resource references, preventing accumulation of unreleased prepared statements in tight loops. Wrapped in try/catch for safe PHP shutdown order.

Thanks

Lucifer07 added 2 commits May 5, 2026 11:33
- Add destructor to PdoResult to close PDOStatement and nullify resources, preventing segmentation faults.
- Introduce cache eviction mechanism in Query to limit memory growth during high-iteration loops.
- Add tests for PdoResult destructor and Query cache eviction to ensure proper resource management.

Assisted-by: GitHub Copilot
…y growth in high-iteration loops

Assisted-by: GitHub Copilot
@niden

niden commented May 5, 2026

Copy link
Copy Markdown
Member

@Lucifer07 I apologize I did not post in our documentation early enough regarding AI development so you did not know.

I would kindly ask you to redo this PR using instructions from here:

https://docs.phalcon.io/5.12/ai-development/

In short, every commit assisted by an AI has to have it in the commit message.

Apologies again.

@Lucifer07 Lucifer07 force-pushed the T16976-findfirst-segfault-memory-cache-limit branch from 14f596f to cb376fd Compare May 5, 2026 15:27
try {
this->pdoStatement->closeCursor();
} catch \Exception {
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There has to be some code in the catch branch to alert the developer that an exception has happened. The way this is now, it will not only work in this case (fixing this bug) but it will also suppress any other exception that might happen unrelated to this fix.

null,
true
);
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add the 1024 and 512 as constants in this file with a docblock of what they represent

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would change that to be 75% instead of 50%. It does reduce the internal cache but not halving it which could in theory have a bigger impact.

use Phalcon\Tests\Support\Traits\DiTrait;

/**
* @issue https://github.com/phalcon/cphalcon/issues/16955

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one goes in the test i.e. method

/**
* Tests that PdoResult::__destruct calls closeCursor and nullifies resources.
*
* @author Phalcon Team <team@phalcon.io>

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one is your name - we didn't do this work.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same with the rest of the tests in this file

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants