-
Notifications
You must be signed in to change notification settings - Fork 63
NamedTemporaryFile usage #783
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
base: master
Are you sure you want to change the base?
Conversation
gprofiler/utils/fs.py
Outdated
|
||
# try creating & writing | ||
try: | ||
os.makedirs(path, 0o755, exist_ok=True) | ||
test_script.write_text("#!/bin/sh\nexit 0") | ||
test_script.chmod(0o755) | ||
test_script = NamedTemporaryFile(dir=path, suffix=".sh") |
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.
I think that to have NamedTemporaryFile
actually remove the file, you need __exit__
to be called. So the usage should be:
with NamedTemporaryFile(...) as test_script:
do function logic
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.
I wrote a simple test to check it and it gets closed everytime, even with "sys.exit(1) between these lines:
tf = tempfile.NamedTemporaryFile(dir="/home/kitter/Documents/june-gprof/my-gprof-fork/gprofiler", suffix=".tester")
with open(tf.name, "w") as f:
f.write("my-tester")
print(Path(tf.name).read_text())
time.sleep(5)
but I changed it accordingly anyway :)
gprofiler/profilers/perf.py
Outdated
# always read its stderr | ||
# using read1() which performs just a single read() call and doesn't read until EOF | ||
# (unlike Popen.communicate()) | ||
assert self._process is not None and self._process.stderr is not None | ||
logger.debug(f"{self._log_name} run output", perf_stderr=self._process.stderr.read1()) # type: ignore |
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.
Are these related?
gprofiler/profilers/perf.py
Outdated
run_process( | ||
[perf_path(), "inject", "--jit", "-o", str(inject_data), "-i", str(perf_data)], | ||
) | ||
perf_data = Path(inject_data.name) |
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.
This creates a problematic flow:
perf_data = temporary file
- Exception is raised during
perf script
finally
runs and tries to unlinkperf_data
which is already unlinked.
I suggest you use a 3rd variable (like perf_script_input
), then set it to either perf_data
OR inject_data
, and only unlink perf_data
as inject_data
is automatically removed.
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.
Done reviewing.
Please link the PR to the relevant issue.
Description
Related Issue
Motivation and Context
How Has This Been Tested?
Screenshots
Checklist: