Title: Corrupt .pyc files stay on disk after failed writes · Issue #126606 · python/cpython · GitHub
Open Graph Title: Corrupt .pyc files stay on disk after failed writes · Issue #126606 · python/cpython
X Title: Corrupt .pyc files stay on disk after failed writes · Issue #126606 · python/cpython
Description: Bug report Bug description: If writing a .pyc file fails (in my case due to a file size limit imposed by ulimit), it can leave corrupt data sitting on disk. This causes a crash the second time you run the program, when the interpreter tr...
Open Graph Description: Bug report Bug description: If writing a .pyc file fails (in my case due to a file size limit imposed by ulimit), it can leave corrupt data sitting on disk. This causes a crash the second time you ...
X Description: Bug report Bug description: If writing a .pyc file fails (in my case due to a file size limit imposed by ulimit), it can leave corrupt data sitting on disk. This causes a crash the second time you ...
Opengraph URL: https://github.com/python/cpython/issues/126606
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Corrupt .pyc files stay on disk after failed writes","articleBody":"# Bug report\n\n### Bug description:\n\nIf writing a `.pyc` file fails (in my case due to a file size limit imposed by `ulimit`), it can leave corrupt data sitting on disk. This causes a crash the _second_ time you run the program, when the interpreter tries to load the corrupt `.pyc` file instead of the original `.py` file:\n\n```\nroot@e7138ea2e2b5:/mnt# python3 crashme.py --limit 1024 --import\nSetting ulimit to 1024...\nImporting a \"big\" library...\nroot@e7138ea2e2b5:/mnt# python3 crashme.py --limit 1024 --import\nSetting ulimit to 1024...\nImporting a \"big\" library...\nTraceback (most recent call last):\n File \"/mnt/crashme.py\", line 17, in \u003cmodule\u003e\n from fakelib import bigfile\n File \"\u003cfrozen importlib._bootstrap\u003e\", line 1360, in _find_and_load\n File \"\u003cfrozen importlib._bootstrap\u003e\", line 1331, in _find_and_load_unlocked\n File \"\u003cfrozen importlib._bootstrap\u003e\", line 935, in _load_unlocked\n File \"\u003cfrozen importlib._bootstrap_external\u003e\", line 991, in exec_module\n File \"\u003cfrozen importlib._bootstrap_external\u003e\", line 1124, in get_code\n File \"\u003cfrozen importlib._bootstrap_external\u003e\", line 753, in _compile_bytecode\nEOFError: marshal data too short\n```\n\nThis was a very hard bug to track down, and it seems like there's a chance to catch this and handle it gracefully. If I do a too-large write myself, I get an `OSError: [Errno 27] File too large`, and I'd imagine the interpreter can see something similar. Ideally (in my opinion - feedback appreciated!), the interpreter would notice this and then:\n- Log a warning.\n- Delete the corrupt file.\n- Set `sys.dont_write_bytecode = True` (this is the workaround I've been using).\n\nHere's the code I've been using to test this (`bigfile` can be pretty much anything, but it does seem to have to be part of a module before the interpreter will write a `.pyc` file for it).\n\n```python\nimport argparse\nimport resource\nimport tempfile\n\nparser = argparse.ArgumentParser()\nparser.add_argument('-l', '--limit', type=int)\nparser.add_argument('-i', '--import', action='store_true', dest='impoort')\nparser.add_argument('-w', '--write', type=int)\nargs = parser.parse_args()\n\nif args.limit is not None:\n print('Setting ulimit to %d...' % args.limit)\n resource.setrlimit(resource.RLIMIT_FSIZE, (args.limit, args.limit))\n\nif args.impoort:\n print('Importing a \"big\" library...')\n from fakelib import bigfile\n\nif args.write is not None:\n print('Writing a %d byte file...' % args.write)\n with tempfile.TemporaryFile() as file:\n file.write(b'a' * args.write)\n```\n\nTested in Python 3.10 on OSX and Python 3.12 in an Ubuntu 24 container.\n\n### CPython versions tested on:\n\n3.10, 3.12\n\n### Operating systems tested on:\n\nLinux, macOS\n\n\u003c!-- gh-linked-prs --\u003e\n### Linked PRs\n* gh-126627\n* gh-126809\n* gh-126810\n\u003c!-- /gh-linked-prs --\u003e\n","author":{"url":"https://github.com/xavierholt","@type":"Person","name":"xavierholt"},"datePublished":"2024-11-08T22:41:04.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":4},"url":"https://github.com/126606/cpython/issues/126606"}
| 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:bc05a167-727b-d77a-9be7-037d5aed7ba8 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | A02A:E05E1:BEAB7:FE84B:6A526B64 |
| html-safe-nonce | 242c8a75de3230a76431e25990a145e91b7e68964117fdccfca0046de78110c3 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBMDJBOkUwNUUxOkJFQUI3OkZFODRCOjZBNTI2QjY0IiwidmlzaXRvcl9pZCI6IjEwNzc3MTQwMjYyODc0OTE5NDAiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 8d7eb02ba2b8d0cdd82622a8d727349639b96fdd48eae25bc131f45b38056490 |
| hovercard-subject-tag | issue:2645311545 |
| 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/python/cpython/126606/issue_layout |
| twitter:image | https://opengraph.githubassets.com/b293983383588210dcbcacb4fc0ba29e4e9cab32bbe36523cf1dfa5ba3f10805/python/cpython/issues/126606 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/b293983383588210dcbcacb4fc0ba29e4e9cab32bbe36523cf1dfa5ba3f10805/python/cpython/issues/126606 |
| og:image:alt | Bug report Bug description: If writing a .pyc file fails (in my case due to a file size limit imposed by ulimit), it can leave corrupt data sitting on disk. This causes a crash the second time you ... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | xavierholt |
| hostname | github.com |
| expected-hostname | github.com |
| None | b9a586c06a05a7a86fc7e3f4dbd03e42f6869085879aa184aa6369456dbd50fb |
| turbo-cache-control | no-preview |
| go-import | github.com/python/cpython git https://github.com/python/cpython.git |
| octolytics-dimension-user_id | 1525981 |
| octolytics-dimension-user_login | python |
| octolytics-dimension-repository_id | 81598961 |
| octolytics-dimension-repository_nwo | python/cpython |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 81598961 |
| octolytics-dimension-repository_network_root_nwo | python/cpython |
| 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 | 07a982c1d40157c619b364352b704c3ce66bb332 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width