10000 Fix race condition in StreamHandler directory creation with locking by TheJoeSchr · Pull Request #1967 · Seldaek/monolog · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Fix race condition in StreamHandler directory creation with locking #1967

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 23 commits into
base: main
Choose a base branch
from

Conversation

TheJoeSchr
Copy link

This PR introduces a locking mechanism for directory creation in StreamHandler when useLocking is enabled. This prevents race conditions when multiple processes (e.g., Laravel queue workers using a 'single' log stack) try to create the
same log directory simultaneously. The handler now creates a lock file in the parent directory, acquires an exclusive lock, creates the target directory, and then releases the lock.

Testing this fix:

To manually test this behavior, especially in a concurrent environment like Laravel queues:

  1. Ensure useLocking is true for your StreamHandler configuration.
  2. Set up a scenario where multiple queue jobs are dispatched rapidly.
  3. Ensure these jobs log to the same file path, and the log directory does not exist before the jobs run.
  4. Observe that the directory is created successfully without errors from concurrent mkdir attempts, and all jobs log as expected.

New unit tests in StreamHandlerTest.php also cover the createDir method's locking behavior.

@TheJoeSchr TheJoeSchr marked this pull request as draft May 17, 2025 19:43
@TheJoeSchr TheJoeSchr marked this pull request as ready for review May 17, 2025 20:28
@TheJoeSchr TheJoeSchr marked this pull request as draft May 18, 2025 13:37
@TheJoeSchr TheJoeSchr marked this pull request as ready for review May 18, 2025 15:35
TheJoeSchr added 17 commits May 18, 2025 18:41
… types differently on Windows and non-Windows systems
…hExponentialRandomizedRetries` => `attemptOperationWithExponentialRandomizedRetries`, Fix return type, Update PHPDoc
…()::attemptOperationWithExponentialRandomizedRetries()`
…StreamHandler()::attemptOperationWithExponentialRandomizedRetries()`
…, first try to `fopen()` with read/write and then read only.

Otherwise fails on already existing directories
…tialRandomizedRetries()` for stream locking
@TheJoeSchr TheJoeSchr force-pushed the fix/useLocking-also-lock-directory branch from 3f3f84a to 06a2ca4 Compare May 18, 2025 16:54
@TheJoeSchr
Copy link
Author
TheJoeSchr commented May 18, 2025

If the lock can't be acquired, an exponential and randomized step-off with retries is implemented. This now also gets used for acquiring locks in write()

@TheJoeSchr TheJoeSchr force-pushed the fix/useLocking-also-lock-directory branch from df02e8d to 3c2f101 Compare May 19, 2025 09:43
@TheJoeSchr TheJoeSchr force-pushed the fix/useLocking-also-lock-directory branch from 3c2f101 to ef86492 Compare May 19, 2025 09:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant
0