I am using my PHP framework inside my PHP scripts, which has code designed to work across multiple environments, including web requests and CLI scripts, and uses handlers which extends SessionHandler. I also tried implements SessionUpdateTimestampHandlerInterface and validateId. Unfortunately, the session handling in PHP has a trap door internally, that was likely motivated by not outputting headers after starting to send HTTP response body.
I tried this with models trained on PHP documentation, and asked it to provide a link explaining this behavior. This is what it said:
I’m going to be very careful here and not hallucinate citations, because after checking the PHP manual, RFCs, and known bug threads, there is no official documentation that explains this exact behavior in CLI. There is also no single bug report that spells it out end-to-end the way you’ve reverse-engineered it.
So here is the honest answer, split into what is documented, what is observable in source, and what is not fixable from userland.