-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Catch and ignore errors writing JSON files. #3288
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -839,6 +839,10 @@ def write_cache(id: str, path: str, tree: MypyFile, | |
old_interface_hash: str, manager: BuildManager) -> str: | ||
"""Write cache files for a module. | ||
|
||
Note that this mypy's behavior is still correct when any given | ||
write_cache() call is replaced with a no-op, so error handling | ||
code that bails without writing anything is okay. | ||
|
||
Args: | ||
id: module ID | ||
path: module path | ||
|
@@ -882,6 +886,7 @@ def write_cache(id: str, path: str, tree: MypyFile, | |
except OSError as err: | ||
manager.log("Cannot get stat for {}: {}".format(path, err)) | ||
# Remove apparently-invalid cache files. | ||
# (This is purely an optimization.) | ||
for filename in [data_json, meta_json]: | ||
try: | ||
os.remove(filename) | ||
|
@@ -897,12 +902,26 @@ def write_cache(id: str, path: str, tree: MypyFile, | |
data_mtime = os.path.getmtime(data_json) | ||
manager.trace("Interface for {} is unchanged".format(id)) | ||
else: | ||
with open(data_json_tmp, 'w') as f: | ||
f.write(data_str) | ||
f.write('\n') | ||
data_mtime = os.path.getmtime(data_json_tmp) | ||
os.replace(data_json_tmp, data_json) | ||
manager.trace("Interface for {} has changed".format(id)) | ||
try: | ||
with open(data_json_tmp, 'w') as f: | ||
f.write(data_str) | ||
f.write('\n') | ||
os.replace(data_json_tmp, data_json) | ||
data_mtime = os.path.getmtime(data_json) | ||
except os.error as err: | ||
# Most likely the error is the replace() call | ||
# (see https://github.com/python/mypy/issues/3215). | ||
manager.log("Error writing data JSON file {}".format(data_json_tmp)) | ||
# Let's continue without writing the meta file. Analysis: | ||
# If the replace failed, we've changed nothing except left | ||
# behind an extraneous temporary file; if the replace | ||
# worked but the getmtime() call failed, the meta file | ||
# will be considered invalid on the next run because the | ||
# data_mtime field won't match the data file's mtime. | ||
# Both have the effect of slowing down the next run a | ||
# little bit due to an out-of-date cache file. | ||
return interface_hash | ||
|
||
mtime = st.st_mtime | ||
size = st.st_size | ||
|
@@ -922,12 +941,18 @@ def write_cache(id: str, path: str, tree: MypyFile, | |
} | ||
|
||
# Write meta cache file | ||
with open(meta_json_tmp, 'w') as f: | ||
if manager.options.debug_cache: | ||
json.dump(meta, f, indent=2, sort_keys=True) | ||
else: | ||
json.dump(meta, f) | ||
os.replace(meta_json_tmp, meta_json) | ||
try: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Like above, maybe move this to a separate function? |
||
with open(meta_json_tmp, 'w') as f: | ||
if manager.options.debug_cache: | ||
json.dump(meta, f, indent=2, sort_keys=True) | ||
else: | ||
json.dump(meta, f) | ||
os.replace(meta_json_tmp, meta_json) | ||
except os.error as err: | ||
# Most likely the error is the replace() call | ||
# (see https://github.com/python/mypy/issues/3215). | ||
# The next run will simply find the cache entry out of date. | ||
manager.log("Error writing meta JSON file {}".format(meta_json_tmp)) | ||
|
||
return interface_hash | ||
|
||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Suggested refactoring: Move the try statement to a separate utility function to make the flow of the enclosing function easier to follow. Currently the function is too long to fit entirely on my screen, which makes code reviews harder.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's unfortunate that the error handling (including comments) is longer than the main logic of the function, but I'm hesitant about breaking it up into smaller bits. Maybe it'll fit on your screen if I move the comments elsewhere (e.g. to the function's docstring)? Anyway, we can iterate on that after the merge.