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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix file deletion crash if file is already gone #6677

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
([#6659](https://github.com/mitmproxy/mitmproxy/pull/6659), @basedBaba)
* Fix a regression when leaf cert creation would fail with intermediate CAs in `ca_file`.
([#6666](https://github.com/mitmproxy/mitmproxy/pull/6666), @manselmi)
* Fix exception when file deletion fails for some reason


## 21 January 2024: mitmproxy 10.2.2
Expand Down
9 changes: 8 additions & 1 deletion mitmproxy/tools/console/master.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@
T = TypeVar("T", str, bytes)


def try_unlink(name: str) -> None:
try:
os.unlink(name)
except Exception:
Copy link
Author

Choose a reason for hiding this comment

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

Wondering whether this should only catch a FileNotFoundError so as not to hide other errors. Linting requires the except not be bare at minimum so put Exception for now

Copy link
Member

Choose a reason for hiding this comment

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

FileNotFoundError makes mose sense here I think.

return


class ConsoleMaster(master.Master):
def __init__(self, opts: options.Options) -> None:
super().__init__(opts)
Expand Down Expand Up @@ -171,7 +178,7 @@ def spawn_external_viewer(self, data, contenttype):
message="Can't start external viewer: %s" % " ".join(c)
)
# add a small delay before deletion so that the file is not removed before being loaded by the viewer
t = threading.Timer(1.0, os.unlink, args=[name])
t = threading.Timer(1.0, try_unlink, args=[name])
t.start()

def set_palette(self, *_) -> None:
Expand Down
10 changes: 10 additions & 0 deletions test/mitmproxy/tools/console/test_master.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import os
import tempfile

from mitmproxy.tools.console import master


def test_try_unlink_file_not_exists():
Copy link
Member

Choose a reason for hiding this comment

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

Can we use https://docs.pytest.org/en/7.1.x/how-to/tmp_path.html please? Cleaning up after mkdtemp is a bit annoying.

path = os.path.join(tempfile.mkdtemp(), 'something')
assert not os.path.exists(path)
master.try_unlink(path)