大网数据广告活动
服务器运维问答

Why does Debian's PHP sessionclean script touch all open files first?

· 52CE 我爱测

I'm studying /usr/lib/php/sessionclean at the moment. There is one thing I don't fully understand. What is the purpose of touching all opened session files before cleaning up? I understand that this prevents the deletion of expir…

问题原文

原帖链接 · user2323470 · CC BY-SA 4.0

I'm studying /usr/lib/php/sessionclean at the moment. There is one thing I don't fully understand. What is the purpose of touching all opened session files before cleaning up? I understand that this prevents the deletion of expired sessions that are currently open but have not yet been written to. But isn't this still a race condition? What if a PHP process opens an expired session file between the touch and the delete commands?

已采纳解答

原帖链接 · John Mahowald · CC BY-SA 4.0

Session files that are older than the maximum lifetime, at the time the find command evaluates them, will be deleted.

Files are updated at the latest when the PHP script finishes execution. However, perhaps it is not really fair to kill a session for being idle while it is executing. Thus, the touch updates the modify time on session files which have a php process currently executing.

Yes, there is a race condition of a php process starting after the touch, resuming a too old session, and getting deleted because it did not finish before the find did garbage collection. The session already existed for its full lifetime, probably several minutes. Missing an extension in a fraction of a second window isn't a big deal.


The alternative, PHP's built in implementation, has a 1% or so chance of running garbage collection on execution. For low volume sites, it might not trigger reliably.

Plus, the external script allows locking down the security on the session directory, which is why Debian maintainers did it that way.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2019-02-19)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。