Skip to content

ext/session: add recursive session cleanup for dirname > 0#21491

Open
jorgsowa wants to merge 3 commits intophp:masterfrom
jorgsowa:fix/gc-session-cleanup
Open

ext/session: add recursive session cleanup for dirname > 0#21491
jorgsowa wants to merge 3 commits intophp:masterfrom
jorgsowa:fix/gc-session-cleanup

Conversation

@jorgsowa
Copy link
Contributor

@jorgsowa jorgsowa commented Mar 22, 2026

When dirname is bigger than 0, there is no automatic cleanup of old sessions. I think it's oversight and shouldn't work like this. If the performance is the issue then lower GC probability should be set instead ignoring cleanup.

This is not security issue, because documentation clearly state the current behavior, but still should work differently from the first place.

@jorgsowa jorgsowa marked this pull request as ready for review March 22, 2026 16:51
@jorgsowa jorgsowa requested a review from Girgias as a code owner March 22, 2026 16:51
Copy link
Member

@Girgias Girgias left a comment

Choose a reason for hiding this comment

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

I don't really understand why we need to loops that are basically identical? Also I'd prefer if those new GC tests are in a subfolder (maybe even a mod_files subfolder)

@jorgsowa
Copy link
Contributor Author

I don't really understand why we need to loops that are basically identical?

  1. First loop is for checking and removing the session when remaining_depth == 0, because this is the only level where session files are stored.

    Second loop is for getting directories based on the value of dirname, becaue this is the level of nesting for the directories where session files are stored.

    But of course, we can merge both of them and check inside what the value of remaining_depth is.

  2. New tests have been moved to a new directory

Comment on lines +308 to +335
if (entry->d_name[0] == '.' &&
(entry->d_name[1] == '\0' ||
(entry->d_name[1] == '.' && entry->d_name[2] == '\0'))) {
continue;
}
if (remaining_depth == 0 && strncmp(entry->d_name, FILE_PREFIX, sizeof(FILE_PREFIX) - 1) != 0) {
continue;
}
size_t entry_len = strlen(entry->d_name);
if (ZSTR_LEN(dirname) + 1 + entry_len >= MAXPATHLEN) {
continue;
}
memcpy(buf + ZSTR_LEN(dirname) + 1, entry->d_name, entry_len);
buf[ZSTR_LEN(dirname) + 1 + entry_len] = '\0';
if (VCWD_STAT(buf, &sbuf) != 0) {
continue;
}
if (remaining_depth == 0) {
if ((now - sbuf.st_mtime) > maxlifetime) {
VCWD_UNLINK(buf);
nrdels++;
}
} else if (S_ISDIR(sbuf.st_mode)) {
zend_string *subdir = zend_string_init(buf, ZSTR_LEN(dirname) + 1 + entry_len, 0);
int n = ps_files_cleanup_dir(subdir, maxlifetime, remaining_depth - 1);
zend_string_release(subdir);
if (n >= 0) {
nrdels += n;
Copy link
Member

Choose a reason for hiding this comment

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

Can you please add comments to explain what you're doing.

The previous comments existed for a reason.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants