Project

General

Profile

Actions

Bug #88197

closed

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

Added by Markus Klein about 5 years ago. Updated almost 5 years ago.

Status:
Closed
Priority:
Must have
Assignee:
Category:
Locking / Session Handling
Target version:
-
Start date:
2019-04-24
Due date:
% Done:

100%

Estimated time:
TYPO3 Version:
8
PHP Version:
Tags:
Complexity:
hard
Is Regression:
Sprint Focus:

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.


Files

Before Patch.mp4 (815 KB) Before Patch.mp4 Before the patch - one always fails Markus Klein, 2019-04-24 18:56
After Patch.mp4 (610 KB) After Patch.mp4 After the patch Markus Klein, 2019-04-24 18:56
Actions #1

Updated by Gerrit Code Review about 5 years ago

  • Status changed from New to Under Review

Patch set 1 for branch master of project Packages/TYPO3.CMS has been pushed to the review server.
It is available at https://review.typo3.org/c/Packages/TYPO3.CMS/+/60531

Actions #3

Updated by Gerrit Code Review about 5 years ago

Patch set 2 for branch master of project Packages/TYPO3.CMS has been pushed to the review server.
It is available at https://review.typo3.org/c/Packages/TYPO3.CMS/+/60531

Actions #4

Updated by Markus Klein about 5 years ago

I found out with this test script, that the behaviour of fopen seems to be specific to Docker for Windows, when the file is located on a host-mounted folder.

This test-script runs without problems on Linux machines, but crashes with the error above within a Docker container on Windows.

$h = fopen('foo.txt', 'c');
if (!$h) echo "went wrong";

unlink('foo.txt');

$h = fopen('foo.txt', 'c');
if (!$h) echo "went wrong too";

echo "the end";
Actions #5

Updated by Gerrit Code Review about 5 years ago

Patch set 3 for branch master of project Packages/TYPO3.CMS has been pushed to the review server.
It is available at https://review.typo3.org/c/Packages/TYPO3.CMS/+/60531

Actions #6

Updated by Gerrit Code Review about 5 years ago

Patch set 1 for branch 9.5 of project Packages/TYPO3.CMS has been pushed to the review server.
It is available at https://review.typo3.org/c/Packages/TYPO3.CMS/+/60561

Actions #7

Updated by Gerrit Code Review about 5 years ago

Patch set 1 for branch TYPO3_8-7 of project Packages/TYPO3.CMS has been pushed to the review server.
It is available at https://review.typo3.org/c/Packages/TYPO3.CMS/+/60562

Actions #8

Updated by Markus Klein about 5 years ago

  • Status changed from Under Review to Resolved
  • % Done changed from 0 to 100
Actions #9

Updated by Benni Mack almost 5 years ago

  • Status changed from Resolved to Closed
Actions

Also available in: Atom PDF