Skip to content
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

[FIX] Permission error when saving settings #2147

Merged
merged 5 commits into from
Apr 20, 2017
Merged

[FIX] Permission error when saving settings #2147

merged 5 commits into from
Apr 20, 2017

Conversation

jerneju
Copy link
Contributor

@jerneju jerneju commented Mar 28, 2017

Issue

Sometimes settings cannot be saved due to file permission issues.
https://sentry.io/biolab/orange3/issues/242461786/

Description of changes

try block

Includes
  • Code changes
  • Tests
  • Documentation

@jerneju
Copy link
Contributor Author

jerneju commented Mar 28, 2017

@codecov-io
Copy link

codecov-io commented Mar 28, 2017

Codecov Report

Merging #2147 into master will increase coverage by 4.52%.
The diff coverage is 100%.

@@            Coverage Diff            @@
##           master   #2147      +/-   ##
=========================================
+ Coverage   67.78%   72.3%   +4.52%     
=========================================
  Files         319     319              
  Lines       54940   54962      +22     
=========================================
+ Hits        37240   39740    +2500     
+ Misses      17700   15222    -2478

janezd
janezd previously requested changes Mar 31, 2017
Copy link
Contributor

@janezd janezd left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Besides addressing the other comments, add tests. Patch open to raise the permission error, and write_default_file to raise other three errors. Also patch the function that you will use for showing the dialog with a mock, and check whether the mock is called.

If somebody later changes this function to do things differently, the test may fail because of being too specific. There's nothing wrong with that, as it will force her/him to rewrite the tests.

else:
settings_file.close()
except PermissionError: # pragma: no cover
pass
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any reason for not having one try with two except statements here?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@astaric, I suppose that we should warn the user that settings were not saved, using a critical warning dialog? We'd do it just once per session.

This also goes for the exceptions caught above.

"""
_, settings_file = mkstemp(suffix='.ini')
handler = SettingsHandler()
handler._get_settings_filename = lambda: settings_file # pylint: disable=W0212
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use verbose warning names when disabling them.

_, settings_file = mkstemp(suffix='.ini')
handler = SettingsHandler()
handler._get_settings_filename = lambda: settings_file # pylint: disable=W0212
from sys import version_info
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Imports go to the top of the file.

cannot be closed. Tests if error is handled.
GH-2147
"""
_, settings_file = mkstemp(suffix='.ini')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

where is this file deleted?

with patch(patch_target, side_effect=IOError):
handler.write_defaults()
with patch(patch_target, side_effect=pickle.PicklingError):
handler.write_defaults()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could also check that the file gets deleted (as it contains invalid settings).

else:
settings_file.close()
except PermissionError:
pass
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be a good idea to log the problem as it looks like this will happen every time.

@astaric astaric self-assigned this Apr 7, 2017
@astaric astaric added this to the 3.4.2 milestone Apr 7, 2017
@astaric
Copy link
Member

astaric commented Apr 7, 2017

Some tests fail on windows:

======================================================================
ERROR: test_write_PermissionError (Orange.widgets.tests.test_settings_handler.SettingHandlerTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "C:\Users\appveyor\testenv\lib\site-packages\Orange\widgets\tests\test_settings_handler.py", line 82, in test_write_PermissionError
    os.remove(settings_file)
PermissionError: [WinError 32] The process cannot access the file because it is being used by another process: 'C:\\Users\\appveyor\\AppData\\Local\\Temp\\1\\tmpi03y3agg.ini'
======================================================================
FAIL: test_write_EOF_IO_Pickling_Errors (Orange.widgets.tests.test_settings_handler.SettingHandlerTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "C:\Users\appveyor\testenv\lib\site-packages\Orange\widgets\tests\test_settings_handler.py", line 99, in test_write_EOF_IO_Pickling_Errors
    self.assertFalse(os.path.exists(settings_file))
AssertionError: True is not false
----------------------------------------------------------------------
Ran 634 tests in 69.664s
FAILED (failures=1, errors=1, skipped=5)

Copy link
Member

@astaric astaric left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Failing tests indicate that something weird is going on. Try not to "fix" tests with try: except, as that defeats the purpose of the test.

If you take a look at the error message given by a failing test, it states:

PermissionError: [WinError 32] The process cannot access the file because it is being used by another process ...

This indicates that a file could not be deleted as it is still open. Figure out why (who opened it) and properly close it. Should fix both of the problems.

else "builtins.open"
with patch(patch_target, side_effect=PermissionError):
handler.write_defaults()
try:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not a way to solve the problem. If remove sometimes fails, figure out why and solve the cause.

handler.write_defaults()
with patch(patch_target, side_effect=pickle.PicklingError):
handler.write_defaults()
if system() != "Windows":
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here, figure out why the files does not get deleted on windows and then solve the cause of the problem.

@jerneju jerneju changed the title [FIX] Permission error when saving settings [WIP][FIX] Permission error when saving settings Apr 14, 2017
@astaric astaric modified the milestones: 3.4.2, future Apr 19, 2017
@jerneju jerneju changed the title [WIP][FIX] Permission error when saving settings [FIX] Permission error when saving settings Apr 20, 2017
@astaric
Copy link
Member

astaric commented Apr 20, 2017

I have simplified the tests a bit and hopefully fixed the problem that caused these tests not to be run on python3.5 on travis.

@astaric astaric merged commit 29835e2 into biolab:master Apr 20, 2017
@jerneju jerneju deleted the file-save-permission branch April 20, 2017 13:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants