Issue Bug Closed

#88197Request ends with 500 error and PHP warning due to locking issue

Markus Klein 04/24/19, 18:44 updated 05/07/19, 12:10

Description

This report is about the FileLockStrategy using the PHP function fopen, flock, etc. internally.

Error message:

Uncaught TYPO3 Exception: #1476107295: PHP Warning: fopen(/var/www/html/typo3temp/var/locks/flock_48a1497b04a7d176d681a71a0f262d73): failed to open stream: No such file or directory in /var/www/html/typo3/sysext/core/Classes/Locking/FileLockStrategy.php line 94

Running two requests with high speed against the same page causes one of the two requests to end up with a HTTP 500 response with the above error message in the log.

The actual use case was to have a frontend application running XHR requests against a defined page, which is defined purely in TypoScript.

It might be extremely complicated to reproduce the problem, but reading the code with the following trace in mind, reveals the logic solution.

  1. Request 1 (R1) hits the server
  2. Request 2 (R2) hits the server
  3. R1: acquires accessLock
  4. R1: acquires pageLock
  5. R2: acquires accessLock
  6. R2: tries pageLock -> is locked -> retry later
  7. R2: releases accessLock
  8. R1: … works along
  9. R2: .. retries the last 3 steps until
  10. R1: acquires accessLock
  11. R1: releases pageLock
  12. R1: destroys pageLock (deleting the underlying file)
  13. R1: releases accessLock
  14. R2: acquires accessLock
  15. R2: tries pageLock -> FAILED!

The problem is that in step 6 the file handle for the underlying lock file is never closed.
Hence, subsequent tries to fopen() the same file again (which has been deleted meanwhile) in the last step, fails with the above error.

The behaviour of PHP opening a file that has been successfully opened before (but got invalid in between) is not documented as far as my research got.

The solution is as easy as closing the opened file again, if the lock couldn’t be acquired on it for whatever reason.

Reviews 3