Skip to content

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

Open
wants to merge 14 commits into
base: master
Choose a base branch
from

Conversation

pfilipko1
Copy link
Contributor

@pfilipko1 pfilipko1 commented Apr 24, 2023

Description

Related Issue

Motivation and Context

How Has This Been Tested?

Screenshots

Checklist:

  • I have read the CONTRIBUTING document.
  • I have updated the relevant documentation.
  • I have added tests for new logic.

@pfilipko1 pfilipko1 changed the title WIP: NamedTemporaryFile usage NamedTemporaryFile usage Apr 28, 2023

# 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")
Copy link
Contributor

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

Copy link
Contributor Author

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 :)

Comment on lines 341 to 345
# 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
Copy link
Contributor

Choose a reason for hiding this comment

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

Are these related?

run_process(
[perf_path(), "inject", "--jit", "-o", str(inject_data), "-i", str(perf_data)],
)
perf_data = Path(inject_data.name)
Copy link
Contributor

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:

  1. perf_data = temporary file
  2. Exception is raised during perf script
  3. finally runs and tries to unlink perf_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.

Copy link
Contributor

@Jongy Jongy left a 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.

@pfilipko1 pfilipko1 requested a review from Jongy May 4, 2023 16:20
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.

2 participants