Skip to content

Improve performance - #9186

Open
caleeli wants to merge 3 commits into
developfrom
epic/FOUR-33902-2617
Open

caleeli wants to merge 3 commits into
developfrom
epic/FOUR-33902-2617

Conversation

@caleeli

@caleeli caleeli commented Nov 19, 2026

Copy link
Copy Markdown
Contributor

Issue & Reproduction Steps

Describe the issue this ticket solves and describe how to reproduce the issue (please attach any fixtures used to reproduce the issue).

Solution

  • List the changes you've introduced to solve the issue.

How to Test

Describe how to test that this solution works.

Related Tickets & Packages

  • Link to any related FOUR tickets, PRDs, or packages

Code Review Checklist

  • I have pulled this code locally and tested it on my instance, along with any associated packages.
  • This code adheres to ProcessMaker Coding Guidelines.
  • This code includes a unit test or an E2E test that tests its functionality, or is covered by an existing test.
  • This solution fixes the bug reported in the original ticket.
  • This solution does not alter the expected output of a component in a way that would break existing Processes.
  • This solution does not implement any breaking changes that would invalidate documentation or cause existing Processes to fail.
  • This solution has been tested with enterprise packages that rely on its functionality and does not introduce bugs in those packages.
  • This code does not duplicate functionality that already exists in the framework or in ProcessMaker.
  • This ticket conforms to the PRD associated with this part of ProcessMaker.

@cursor

cursor Bot commented Nov 19, 2026 •

Copy link
Copy Markdown

PR Summary

Medium Risk
Settings correctness now depends on epoch/invalidation timing across workers and tenants; Redis outages force DB-only paths. Race-handling in resolve() and post-commit observer logic should be validated under concurrent updates.

Overview
Adds a process-local L1 settings cache (SettingMemoryCache) coordinated across Octane/queue workers via a shared Redis epoch, so repeated Setting::byKey() calls avoid extra Redis/DB round-trips within a request while staying fresh after updates.

SettingCacheManager now resolves settings through L1 → Redis → DB (resolve()), wraps stored values in an envelope so cached null is not a miss, and bumps or republishes the epoch on set, delete, clear, and pattern clears. When the epoch cannot be read, reads fail closed to the DB. SettingObserver defers epoch publication until saved() (after commit) to avoid racing pre-commit reloads, with optional epoch bumps on hard deletes.

Lifecycle hooks call SettingMemoryCache::beginRequest() on Octane request reset and Queue::before so each HTTP job re-syncs the epoch without sticking stale L1 maps. Feature coverage is expanded with SettingMemoryCacheTest.

Reviewed by Cursor Bugbot for commit 13d40b1. Bugbot is set up for automated code reviews on this repo. Configure here.

@cursor cursor Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Stale Bugbot comment from a previous run.

@cursor
cursor Bot requested review from nolanpro and rodriquelca November 19, 2026 20:11
@@ -135,7 +181,14 @@ public function getOrCache(string $key, callable $callback): mixed
*/
public function set(string $key, mixed $value, null|int|\DateInterval $ttl = null): bool

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🔒 Agentic Security Review

Severity: MEDIUM

Description: Security-setting saves advance the shared L1 epoch in SettingObserver::saving before the SQL update commits, and SettingCacheManager::set() later stores the committed model in Redis without advancing the epoch again. A concurrent Octane worker that calls Setting::byKey in that window syncs to the new epoch, misses Redis, loads the still-old row, and keeps it in its process-local L1. On later requests the epochs match, so syncEpoch leaves that map in place and the worker keeps enforcing the pre-commit value. This sticks for session-control.ip_restriction and session-control.device_restriction (Setting::configByKey on every web request), for session.lifetime (SettingsConfigRepository always reloads it through byKey), and for sso.default.login when package-auth is installed. password-policies.* read through config() is memoized in SettingsConfigRepository::$items after the first lookup, so that path is not what makes the epoch bug sticky.

Impact: After an admin tightens session IP/device restrictions, idle timeout, or the default SSO login, Octane workers that resolved that key between the pre-commit epoch bump and the post-commit cache fill keep applying the previous weaker policy on the public web stack until some later setting write bumps the epoch. Users who land on those workers bypass the updated session or login control. Winning the race is timing-dependent; once won, the stale L1 entry persists across requests because epoch equality is treated as proof the memory map matches Redis.

Remediation: Publish the epoch only after the committed setting is the value stored in Redis: move the bump from the saving() invalidate path to the post-commit set() (or a saved() hook) after put(). Do not let resolve() cache a loader result under an epoch that was already advanced for an uncommitted write; a put of the pre-commit row that lands after set() must not become the value later workers treat as current. Keep invalidation and the committed fill atomic so a concurrent byKey cannot repopulate L1 with the old row at the epoch set() just published.

} catch (Throwable $e) {
Log::info('SettingMemoryCache: failed to read epoch: ' . $e->getMessage());

return self::$localEpochs[self::currentTenantKey()] ?? 0;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🔒 Agentic Security Review

Severity: MEDIUM

Description: On a warm Octane worker, a failed Redis epoch read is reported as the worker's own local epoch. syncEpoch then treats L1 as fresh and login/session code keeps enforcing the last in-memory authentication settings for the rest of the outage.

Impact: A public login request or an authenticated session request on that worker keeps the last L1 values for 2FA, login-attempt limit, default SSO, IP/device session restrictions, and session lifetime, including a more permissive value than the database. Before this L1 layer, the settings Redis repository get threw on connection loss, so the read failed instead of being treated as fresh. The new catch adds trust of process-local auth policy when the freshness oracle is unavailable. Cold workers still fail closed: with no local epoch, the catch returns 0, null !== 0 clears L1, and the following repository get throws.

Remediation: Do not return the local epoch from a failed remote read. Rethrow so the setting read fails closed, or skip L1 and the Redis get for that request and call the DB loader. Clearing L1 alone is not enough: resolve() still calls cacheManager->get before the loader, and that get throws while Redis is down.

@decisions-sonarqube

Copy link
Copy Markdown

Quality Gate failed Quality Gate failed

Failed conditions
1 New issue
1 New Maintainability Issues (required ≤ 0)

See analysis details on SonarQube

Catch issues before they fail your Quality Gate with our IDE extension SonarQube for IDE SonarQube for IDE

@gproly gproly left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Finding — Setting memory cache can serve stale values outside Octane

beginRequest() runs only when Laravel Octane fires RequestTerminated. Traditional PHP workers (one process per request lifecycle without that listener) never reset the per-request sync flag, so the in-memory settings map is not checked against Redis again after the first access in that worker process.

Example: Worker process A loads Setting::byKey('primary-color') and keeps the value in memory. An administrator updates that setting on another server; the observer invalidates Redis and increments the shared epoch. The next HTTP request handled by worker A still returns the old color because epoch synchronization is skipped.

Expected: Each new HTTP request should re-check the shared epoch (or equivalent) and drop the in-memory map when it changes, on every deployment mode, not only under Octane.

Secondary risk: The same Setting model instance can be reused across requests on a long-lived worker; in-place changes on one request can affect the next without a database save.

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Cursor Bugbot has reviewed your changes using high effort and found 3 potential issues.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit 13d40b1. Configure here.

}
} else {
$this->writeStoreEntry($key, $value);
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Stale resolve can overwrite Redis

High Severity

resolve() re-checks the epoch before writing Redis, but the write itself is not guarded. A concurrent saved()/set() can bump the epoch and publish a committed value in the gap between forceResyncEpoch() and writeStoreEntry(), so a loader that still holds a pre-commit row overwrites L2 under the new epoch. Later workers then hit that stale envelope and never go back to the database until the setting is saved again.

Additional Locations (1)
Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit 13d40b1. Configure here.

if (method_exists($settingCache, 'publishEpoch')) {
$settingCache->publishEpoch();
}
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Epoch publish runs before commit

Medium Severity

The new saved() hook comments that it publishes the L1 epoch after the row is committed, but it listens to Eloquent saved without afterCommit. Inside a wrapping DB::transaction(), that still runs before commit, so concurrent byKey() loaders can refill Redis with the pre-commit row after the epoch bump.

Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit 13d40b1. Configure here.

self::$localEpochs[$tenantKey] = $newEpoch;
self::$epochUnreliable[$tenantKey] = false;
self::$syncedThisRequest[$tenantKey] = true;
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Failed epoch bump marked reliable

Medium Severity

bumpEpoch() always sets epochUnreliable to false and syncedThisRequest to true, even when incrementRemoteEpoch() fails both INCR and SET. The publishing worker then treats L1/L2 as trustworthy although other workers never saw an epoch change and keep serving stale memory.

Additional Locations (1)
Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit 13d40b1. Configure here.

@caleeli
caleeli requested a review from gproly November 20, 2026 20:36
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