spiral / framework

High-Performance PHP Framework
https://spiral.dev
MIT License
1.82k stars 89 forks source link

Fix concurrent writing and reading on workers boot #1137

Closed wapmorgan closed 2 months ago

wapmorgan commented 2 months ago

Set a shared lock on a file in Memoty::loadData() to prevent reading an empty file while writing it in concurrent worker/process

Q A
Bugfix? ✔️
Breaks BC?
New feature?
Issues Can get an empty file when file_put_contents in \Spiral\Boot\Memory::saveData=>\Spiral\Files\Files::write created, but not completed write to file
Docs PR spiral/docs

Got this problem when 2 jobs consumers concurrently started writing/reading runtime/cache/cycle.php file. First consumer writing schema to the file, second consumers find out that files exists and read it, but it still was empty = so read empty schema.

wapmorgan commented 2 months ago

Also another option is to remove LOCK_NB and remain only LOCK_SH to force current thread to wait until it will be able to make shared lock. In current MR's code current thread will build schema again and write it to file again.

roxblnfk commented 2 months ago

@wapmorgan hi, the first commit was OK. Could you revert the last change?