Title: file_put_contents() is racy · Issue #20108 · php/php-src · GitHub
Open Graph Title: file_put_contents() is racy · Issue #20108 · php/php-src
X Title: file_put_contents() is racy · Issue #20108 · php/php-src
Description: php-src/ext/standard/file.c Line 492 in 18d99ee php_stream_truncate_set_size(stream, 0); Imagine following sequence (two processes A and B writing some serialized data to file called "cache.dat" using file_put_contents(): Process A: open...
Open Graph Description: php-src/ext/standard/file.c Line 492 in 18d99ee php_stream_truncate_set_size(stream, 0); Imagine following sequence (two processes A and B writing some serialized data to file called "cache.dat" us...
X Description: php-src/ext/standard/file.c Line 492 in 18d99ee php_stream_truncate_set_size(stream, 0); Imagine following sequence (two processes A and B writing some serialized data to file called "cache.da...
Opengraph URL: https://github.com/php/php-src/issues/20108
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"file_put_contents() is racy","articleBody":"https://github.com/php/php-src/blob/18d99ee4b724f9ecd18122abacfac5767bfbfb48/ext/standard/file.c#L492\n\nImagine following sequence (two processes A and B writing some serialized data to file called \"cache.dat\" using file_put_contents():\n\n- Process A: open the cache file for writing\n- Process B: open the same cache file for writing\n- Process A: truncate the file to zero length\n- Process B: truncate the file to zero length\n- Process A: write the cached data (e.g. 1002 byte long serialized data)\n- Process B: write the cached data (e.g. 1000 byte long serialized data)\n- Process A: close the file\n- Procees B: close the file\n\nThis results in a file with 1000 bytes from Process B and 2 extra bytes from process A and you end up with `unserialize(): Extra data starting at offset 1000 of 1002 bytes` if you later try to unserialize the file.\n\nSince it seems that many developers incorrectly believe that a function called file_put_contents() would actually write a file with the given contents, I'm suggesting that PHP internal implementation should instead be (unless FILE_APPEND or LOCK_EX in flags)\n\n1. Create a new temporary file in the same directory with the target filename.\n2. Write the given data (argument to file_put_contents()) into said file.\n3. Close the file\n4. Rename the temporary file to final target filename.\n\nSince only the rename() is guaranteed to be atomic in POSIX compatible systems (and even that requires rename within the same directory), this is the only way to make sure you don't end up with mixture of two files when multiple processes are calling file_put_contents() at nearly the same time. And in case NFS is used, you cannot assume that LOCK_EX actually works so you must use rename() semantics.\n\nAnd maybe allow current behavior with some new flag (`FILE_ALLOW_RACY`?) with documentation \"Reduce syscalls to improve performance but caller must guarantee that two processes are not trying to write into the same file concurrently.\"","author":{"url":"https://github.com/mikkorantalainen","@type":"Person","name":"mikkorantalainen"},"datePublished":"2025-10-08T15:30:28Z","url":"https://github.com/php/php-src/issues/20108"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:63256362-51e9-9f00-e67d-455d6a347311 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | AF1A:2F85FF:2BDCBB:399340:6AFE5C58 |
| html-safe-nonce | 6f5f433f73c1e5f064eb6fecaad8de31b9d09e577987fc36ce78e71f7ac541cc |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBRjFBOjJGODVGRjoyQkRDQkI6Mzk5MzQwOjZBRkU1QzU4IiwidmlzaXRvcl9pZCI6IjgyMTQ3NjQ2OTkzNjM3Mjg0NzIiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 7edc7b9e2632af0f92098f5f0bac4a75a5b0fab3dfe2a9b847db76b78cede32d |
| hovercard-subject-tag | issue:3495934439 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/php/php-src/20108/issue_layout |
| twitter:image | https://opengraph.githubassets.com/7783fc6e9f6cc5266f947ad526b556af970a6f190f463b049359cc7182a4c926/php/php-src/issues/20108 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/7783fc6e9f6cc5266f947ad526b556af970a6f190f463b049359cc7182a4c926/php/php-src/issues/20108 |
| og:image:alt | php-src/ext/standard/file.c Line 492 in 18d99ee php_stream_truncate_set_size(stream, 0); Imagine following sequence (two processes A and B writing some serialized data to file called "cache.dat" us... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | mikkorantalainen |
| hostname | github.com |
| expected-hostname | github.com |
| None | b3c0f155eafc27e60db6e254d1ffff0e072e9ab3b4018a5ee518797c19d89f86 |
| turbo-cache-control | no-preview |
| go-import | github.com/php/php-src git https://github.com/php/php-src.git |
| octolytics-dimension-user_id | 25158 |
| octolytics-dimension-user_login | php |
| octolytics-dimension-repository_id | 1903522 |
| octolytics-dimension-repository_nwo | php/php-src |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 1903522 |
| octolytics-dimension-repository_network_root_nwo | php/php-src |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 4a10310af51a1bfffbedc4fd81cb582acf0cb0ff |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width