'How to solve the Session lock/performance problem of Codeigniter 4 when session_write_close() does not work?

I'm having an serious issue in my project (Codeigniter 4).

Suddenly, it started to show some strong peaks and the PHP process are locked for some seconds or minutes (even with a small number of users logged in).

The closest of a solution I've found it was to apply
session_write_close(); after anywhere I call session (there are some heavy AJAX).

This is what my server says it is taking time:

script_filename = //home/treineipassei/www/index.php
[0x00007f8480616d10] flock() /home/treineipassei/ci4/system/Session/Handlers/FileHandler.php:135
[0x00007f84806168a0] read() /home/treineipassei/ci4/system/Session/Session.php:894
[0x00007f8480616850] session_start() /home/treineipassei/ci4/system/Session/Session.php:894
[0x00007f84806167b0] startSession() /home/treineipassei/ci4/system/Session/Session.php:238
[0x00007f84806162a0] start() /home/treineipassei/ci4/system/Config/Services.php:577
[0x00007f8480616060] session() /home/treineipassei/ci4/system/Config/BaseService.php:189
[0x00007f8480615ef0] getSharedInstance() /home/treineipassei/ci4/system/Config/Services.php:563
[0x00007f8480615cd0] session() /home/treineipassei/ci4/app/Controllers/Pessoa.php:9
[0x00007f8480615bd0] __construct() /home/treineipassei/ci4/system/CodeIgniter.php:802
[0x00007f8480615ac0] createController() /home/treineipassei/ci4/system/CodeIgniter.php:403
[0x00007f84806154f0] handleRequest() /home/treineipassei/ci4/system/CodeIgniter.php:320
[0x00007f8480615230] run() /home/treineipassei/www/index.php:47


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source